protected override void Generate(AlterColumnOperation operation, IModel model, MigrationCommandListBuilder builder) { base.Generate(operation, model, builder); IAnnotation comment = operation.GetAnnotations().FirstOrDefault(x => x.Name == CommentAnnotationName); if (comment is null) { comment = operation.OldColumn.GetAnnotation(CommentAnnotationName); if (comment != null) { DropTableComment(builder, operation.Schema, operation.Name); } } else { UpdateColumnComment(builder, operation.Schema, operation.Table, operation.Name, comment.Value); } }
protected override void Generate( AlterColumnOperation operation, IModel model, MigrationCommandListBuilder builder) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); var property = FindProperty(model, operation.Schema, operation.Table, operation.Name); if (operation.ComputedColumnSql != null) { var dropColumnOperation = new DropColumnOperation { Schema = operation.Schema, Table = operation.Table, Name = operation.Name }; var addColumnOperation = new AddColumnOperation { Schema = operation.Schema, Table = operation.Table, Name = operation.Name, ClrType = operation.ClrType, ColumnType = operation.ColumnType, IsUnicode = operation.IsUnicode, MaxLength = operation.MaxLength, IsRowVersion = operation.IsRowVersion, IsNullable = operation.IsNullable, DefaultValue = operation.DefaultValue, DefaultValueSql = operation.DefaultValueSql, ComputedColumnSql = operation.ComputedColumnSql, IsFixedLength = operation.IsFixedLength }; addColumnOperation.AddAnnotations(operation.GetAnnotations()); Generate(dropColumnOperation, model, builder); Generate(addColumnOperation, model, builder); return; } var valueGenerationStrategy = operation[ OracleAnnotationNames.ValueGenerationStrategy] as OracleValueGenerationStrategy?; var identity = valueGenerationStrategy == OracleValueGenerationStrategy.IdentityColumn; if (IsOldColumnSupported(model)) { var oldValueGenerationStrategy = operation.OldColumn[ OracleAnnotationNames.ValueGenerationStrategy] as OracleValueGenerationStrategy?; var oldIdentity = oldValueGenerationStrategy == OracleValueGenerationStrategy.IdentityColumn; if (oldIdentity && !identity) { DropIdentity(operation, builder); } if (operation.OldColumn.DefaultValue != null || operation.OldColumn.DefaultValueSql != null && (operation.DefaultValue == null || operation.DefaultValueSql == null)) { DropDefaultConstraint(operation.Schema, operation.Table, operation.Name, builder); } } else { if (!identity) { DropIdentity(operation, builder); } if (operation.DefaultValue == null && operation.DefaultValueSql == null) { DropDefaultConstraint(operation.Schema, operation.Table, operation.Name, builder); } } builder .Append("ALTER TABLE ") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Table, operation.Schema)) .Append(" MODIFY "); ColumnDefinition( operation.Schema, operation.Table, operation.Name, operation.ClrType, operation.ColumnType, operation.IsUnicode, operation.MaxLength, operation.IsFixedLength, operation.IsRowVersion, operation.IsNullable, operation.DefaultValue, operation.DefaultValueSql, operation.ComputedColumnSql, identity, operation, model, builder); builder.AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator); builder.EndCommand(); }
protected override void Generate( AlterColumnOperation operation, IModel model, MigrationCommandListBuilder builder) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); IEnumerable <IIndex> indexesToRebuild = null; var property = FindProperty(model, operation.Schema, operation.Table, operation.Name); if (operation.ComputedColumnSql != null) { var dropColumnOperation = new DropColumnOperation { Schema = operation.Schema, Table = operation.Table, Name = operation.Name }; if (property != null) { dropColumnOperation.AddAnnotations(_migrationsAnnotations.ForRemove(property)); } var addColumnOperation = new AddColumnOperation { Schema = operation.Schema, Table = operation.Table, Name = operation.Name, ClrType = operation.ClrType, ColumnType = operation.ColumnType, IsUnicode = operation.IsUnicode, MaxLength = operation.MaxLength, IsRowVersion = operation.IsRowVersion, IsNullable = operation.IsNullable, DefaultValue = operation.DefaultValue, DefaultValueSql = operation.DefaultValueSql, ComputedColumnSql = operation.ComputedColumnSql }; addColumnOperation.AddAnnotations(operation.GetAnnotations()); // TODO: Use a column rebuild instead indexesToRebuild = GetIndexesToRebuild(property, operation).ToList(); DropIndexes(indexesToRebuild, builder); Generate(dropColumnOperation, model, builder, terminate: false); builder.AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator); Generate(addColumnOperation, model, builder, terminate: false); builder.AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator); CreateIndexes(indexesToRebuild, builder); builder.EndCommand(); return; } var narrowed = false; if (IsOldColumnSupported(model)) { var valueGenerationStrategy = operation[ OracleAnnotationNames.ValueGenerationStrategy] as OracleValueGenerationStrategy?; var identity = valueGenerationStrategy == OracleValueGenerationStrategy.IdentityColumn; var oldValueGenerationStrategy = operation.OldColumn[ OracleAnnotationNames.ValueGenerationStrategy] as OracleValueGenerationStrategy?; var oldIdentity = oldValueGenerationStrategy == OracleValueGenerationStrategy.IdentityColumn; if (identity != oldIdentity) { throw new InvalidOperationException(OracleStrings.AlterIdentityColumn); } var type = operation.ColumnType ?? GetColumnType( operation.Schema, operation.Table, operation.Name, operation.ClrType, operation.IsUnicode, operation.MaxLength, operation.IsRowVersion, model); var oldType = operation.OldColumn.ColumnType ?? GetColumnType( operation.Schema, operation.Table, operation.Name, operation.OldColumn.ClrType, operation.OldColumn.IsUnicode, operation.OldColumn.MaxLength, operation.OldColumn.IsRowVersion, model); narrowed = type != oldType || !operation.IsNullable && operation.OldColumn.IsNullable; } if (narrowed) { indexesToRebuild = GetIndexesToRebuild(property, operation).ToList(); DropIndexes(indexesToRebuild, builder); } DropDefaultConstraint(operation.Schema, operation.Table, operation.Name, builder); builder .Append("ALTER TABLE ") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Table, operation.Schema)) .Append(" ALTER COLUMN "); ColumnDefinition( operation.Schema, operation.Table, operation.Name, operation.ClrType, operation.ColumnType, operation.IsUnicode, operation.MaxLength, operation.IsRowVersion, operation.IsNullable, /*defaultValue:*/ null, /*defaultValueSql:*/ null, operation.ComputedColumnSql, /*identity:*/ false, operation, model, builder); builder.AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator); if (operation.DefaultValue != null || operation.DefaultValueSql != null) { builder .Append("ALTER TABLE ") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Table, operation.Schema)) .Append(" ADD"); DefaultValue(operation.DefaultValue, operation.DefaultValueSql, builder); builder .Append(" FOR ") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Name)) .AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator); } if (narrowed) { CreateIndexes(indexesToRebuild, builder); } builder.EndCommand(); }
protected virtual void Generate([NotNull] AlterColumnOperation operation, [NotNull] IndentedStringBuilder builder) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); builder .Append(".AlterColumn<") .Append(_code.Reference(operation.ClrType)) .AppendLine(">("); using (builder.Indent()) { builder .Append("name: ") .Append(_code.Literal(operation.Name)); if (operation.Schema != null) { builder .AppendLine(",") .Append("schema: ") .Append(_code.Literal(operation.Schema)); } builder .AppendLine(",") .Append("table: ") .Append(_code.Literal(operation.Table)); if (operation.ColumnType != null) { builder.AppendLine(",") .Append("type: ") .Append(_code.Literal(operation.ColumnType)); } builder.AppendLine(",") .Append("nullable: ") .Append(_code.Literal(operation.IsNullable)); if (operation.DefaultValueSql != null) { builder .AppendLine(",") .Append("defaultValueSql: ") .Append(_code.Literal(operation.DefaultValueSql)); } else if (operation.ComputedColumnSql != null) { builder .AppendLine(",") .Append("computedColumnSql: ") .Append(_code.UnknownLiteral(operation.ComputedColumnSql)); } else if (operation.DefaultValue != null) { builder .AppendLine(",") .Append("defaultValue: ") .Append(_code.UnknownLiteral(operation.DefaultValue)); } builder.Append(")"); Annotations(operation.GetAnnotations(), builder); } }
protected virtual void Generate([NotNull] AlterColumnOperation operation, [NotNull] IndentedStringBuilder builder) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); builder .Append(".AlterColumn<") .Append(_code.Reference(operation.ClrType)) .AppendLine(">("); using (builder.Indent()) { builder .Append("name: ") .Append(_code.Literal(operation.Name)); if (operation.Schema != null) { builder .AppendLine(",") .Append("schema: ") .Append(_code.Literal(operation.Schema)); } builder .AppendLine(",") .Append("table: ") .Append(_code.Literal(operation.Table)); if (operation.ColumnType != null) { builder.AppendLine(",") .Append("type: ") .Append(_code.Literal(operation.ColumnType)); } if (operation.IsUnicode == false) { builder .AppendLine(",") .Append("unicode: false"); } if (operation.MaxLength.HasValue) { builder.AppendLine(",") .Append("maxLength: ") .Append(_code.Literal(operation.MaxLength.Value)); } if (operation.IsRowVersion) { builder .AppendLine(",") .Append("rowVersion: true"); } builder.AppendLine(",") .Append("nullable: ") .Append(_code.Literal(operation.IsNullable)); if (operation.DefaultValueSql != null) { builder .AppendLine(",") .Append("defaultValueSql: ") .Append(_code.Literal(operation.DefaultValueSql)); } else if (operation.ComputedColumnSql != null) { builder .AppendLine(",") .Append("computedColumnSql: ") .Append(_code.UnknownLiteral(operation.ComputedColumnSql)); } else if (operation.DefaultValue != null) { builder .AppendLine(",") .Append("defaultValue: ") .Append(_code.UnknownLiteral(operation.DefaultValue)); } if (operation.OldColumn.ClrType != null) { builder.AppendLine(",") .Append("oldClrType: typeof(") .Append(_code.Reference(operation.OldColumn.ClrType)) .Append(")"); } if (operation.OldColumn.ColumnType != null) { builder.AppendLine(",") .Append("oldType: ") .Append(_code.Literal(operation.OldColumn.ColumnType)); } if (operation.OldColumn.IsUnicode == false) { builder .AppendLine(",") .Append("oldUnicode: false"); } if (operation.OldColumn.MaxLength.HasValue) { builder.AppendLine(",") .Append("oldMaxLength: ") .Append(_code.Literal(operation.OldColumn.MaxLength.Value)); } if (operation.OldColumn.IsRowVersion) { builder .AppendLine(",") .Append("oldRowVersion: true"); } if (operation.OldColumn.IsNullable) { builder.AppendLine(",") .Append("oldNullable: true"); } if (operation.OldColumn.DefaultValueSql != null) { builder .AppendLine(",") .Append("oldDefaultValueSql: ") .Append(_code.Literal(operation.OldColumn.DefaultValueSql)); } else if (operation.OldColumn.ComputedColumnSql != null) { builder .AppendLine(",") .Append("oldComputedColumnSql: ") .Append(_code.UnknownLiteral(operation.OldColumn.ComputedColumnSql)); } else if (operation.OldColumn.DefaultValue != null) { builder .AppendLine(",") .Append("oldDefaultValue: ") .Append(_code.UnknownLiteral(operation.OldColumn.DefaultValue)); } builder.Append(")"); Annotations(operation.GetAnnotations(), builder); OldAnnotations(operation.OldColumn.GetAnnotations(), builder); } }
protected override void Generate(AlterColumnOperation operation, IModel model, MigrationCommandListBuilder builder) { try { if (m_oracleLogger != null && m_oracleLogger.Logger != null && m_oracleLogger.Logger.IsEnabled(LogLevel.Trace)) { Trace <DbLoggerCategory.Migrations> .Write(m_oracleLogger, LogLevel.Trace, OracleTraceTag.Entry, OracleTraceClassName.OracleMigrationsSqlGenerator, OracleTraceFuncName.Generate, "AlterColumnOperation"); } Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); FindProperty(model, operation.Schema, operation.Table, operation.Name); if (operation.ComputedColumnSql != null) { DropColumnOperation operation2 = new DropColumnOperation { Schema = operation.Schema, Table = operation.Table, Name = operation.Name, }; AddColumnOperation addColumnOperation = new AddColumnOperation { Schema = operation.Schema, Table = operation.Table, Name = operation.Name, ClrType = operation.ClrType, ColumnType = operation.ColumnType, IsUnicode = operation.IsUnicode, MaxLength = operation.MaxLength, IsRowVersion = operation.IsRowVersion, IsNullable = operation.IsNullable, DefaultValue = operation.DefaultValue, DefaultValueSql = operation.DefaultValueSql, ComputedColumnSql = operation.ComputedColumnSql, IsFixedLength = operation.IsFixedLength, }; addColumnOperation.AddAnnotations(operation.GetAnnotations()); Generate(operation2, model, builder); Generate(addColumnOperation, model, builder); return; } bool flag = operation["Oracle:ValueGenerationStrategy"] as OracleValueGenerationStrategy? == OracleValueGenerationStrategy.IdentityColumn; if (IsOldColumnSupported(model)) { if (operation.OldColumn["Oracle:ValueGenerationStrategy"] as OracleValueGenerationStrategy? == OracleValueGenerationStrategy.IdentityColumn && !flag) { if (_oracleSQLCompatibility == "11") { DropIdentityForDB11(operation, builder); } else { DropIdentity(operation, builder); } } if (operation.OldColumn.DefaultValue != null || (operation.OldColumn.DefaultValueSql != null && (operation.DefaultValue == null || operation.DefaultValueSql == null))) { DropDefaultConstraint(operation.Schema, operation.Table, operation.Name, builder); } } else { if (!flag) { if (_oracleSQLCompatibility == "11") { DropIdentityForDB11(operation, builder); } else { DropIdentity(operation, builder); } } if (operation.DefaultValue == null && operation.DefaultValueSql == null) { DropDefaultConstraint(operation.Schema, operation.Table, operation.Name, builder); } } builder.AppendLine("declare"); if (operation.Schema != null) { builder.AppendLine(" l_nullable all_tab_columns.nullable % type" + Dependencies.SqlGenerationHelper.StatementTerminator); } else { builder.AppendLine(" l_nullable user_tab_columns.nullable % type" + Dependencies.SqlGenerationHelper.StatementTerminator); } builder.AppendLine("begin ").AppendLine(" select nullable into l_nullable "); if (operation.Schema != null) { builder.AppendLine(" from all_tab_columns "); } else { builder.AppendLine(" from user_tab_columns "); } builder.AppendLine(" where table_name = '" + operation.Table + "' ").AppendLine(" and column_name = '" + operation.Name + "' "); if (operation.Schema != null) { builder.AppendLine(" and owner = '" + operation.Schema + "'" + Dependencies.SqlGenerationHelper.StatementTerminator + " "); } else { builder.AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator ?? ""); } builder.AppendLine(" if l_nullable = 'N' then "); builder.Append(" EXECUTE IMMEDIATE 'ALTER TABLE ").Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Table, operation.Schema)).Append(" MODIFY "); if (operation.IsNullable) { ColumnDefinition(operation.Schema, operation.Table, operation.Name, operation.ClrType, operation.ColumnType, operation.IsUnicode, operation.MaxLength, operation.IsFixedLength, operation.IsRowVersion, operation.IsNullable, operation.DefaultValue, operation.DefaultValueSql, operation.ComputedColumnSql, flag, operation, model, builder); builder.AppendLine(" NULL'" + Dependencies.SqlGenerationHelper.StatementTerminator); } else { ColumnDefinition(operation.Schema, operation.Table, operation.Name, operation.ClrType, operation.ColumnType, operation.IsUnicode, operation.MaxLength, operation.IsFixedLength, operation.IsRowVersion, operation.IsNullable, operation.DefaultValue, operation.DefaultValueSql, operation.ComputedColumnSql, flag, operation, model, builder, addNotNullKeyword: false); builder.AppendLine(" '" + Dependencies.SqlGenerationHelper.StatementTerminator); } builder.AppendLine(" else "); builder.Append(" EXECUTE IMMEDIATE 'ALTER TABLE ").Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Table, operation.Schema)).Append(" MODIFY "); ColumnDefinition(operation.Schema, operation.Table, operation.Name, operation.ClrType, operation.ColumnType, operation.IsUnicode, operation.MaxLength, operation.IsFixedLength, operation.IsRowVersion, operation.IsNullable, operation.DefaultValue, operation.DefaultValueSql, operation.ComputedColumnSql, flag, operation, model, builder); builder.AppendLine("'" + Dependencies.SqlGenerationHelper.StatementTerminator).AppendLine(" end if" + Dependencies.SqlGenerationHelper.StatementTerminator).AppendLine("end" + Dependencies.SqlGenerationHelper.StatementTerminator); builder.EndCommand(); } catch (Exception ex) { if (m_oracleLogger != null && m_oracleLogger.Logger != null && m_oracleLogger.Logger.IsEnabled(LogLevel.Error)) { Trace <DbLoggerCategory.Migrations> .Write(m_oracleLogger, LogLevel.Error, OracleTraceTag.Error, OracleTraceClassName.OracleMigrationsSqlGenerator, OracleTraceFuncName.Generate, ex.ToString()); } throw; } finally { if (m_oracleLogger != null && m_oracleLogger.Logger != null && m_oracleLogger.Logger.IsEnabled(LogLevel.Trace)) { Trace <DbLoggerCategory.Migrations> .Write(m_oracleLogger, LogLevel.Trace, OracleTraceTag.Exit, OracleTraceClassName.OracleMigrationsSqlGenerator, OracleTraceFuncName.Generate, "AlterColumnOperation"); } } }
/// <summary> /// Builds commands for the given <see cref="AlterColumnOperation" /> /// by making calls on the given <see cref="MigrationCommandListBuilder" />. /// </summary> /// <param name="operation"> The operation. </param> /// <param name="model"> The target model which may be <c>null</c> if the operations exist without a model. </param> /// <param name="builder"> The command builder to use to build the commands. </param> protected override void Generate( AlterColumnOperation operation, IModel model, MigrationCommandListBuilder builder) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); IEnumerable <IIndex> indexesToRebuild = null; var property = FindProperty(model, operation.Schema, operation.Table, operation.Name); if (operation.ComputedColumnSql != null) { var dropColumnOperation = new DropColumnOperation { Schema = operation.Schema, Table = operation.Table, Name = operation.Name }; if (property != null) { dropColumnOperation.AddAnnotations(_migrationsAnnotations.ForRemove(property)); } var addColumnOperation = new AddColumnOperation { Schema = operation.Schema, Table = operation.Table, Name = operation.Name, ClrType = operation.ClrType, ColumnType = operation.ColumnType, IsUnicode = operation.IsUnicode, MaxLength = operation.MaxLength, IsRowVersion = operation.IsRowVersion, IsNullable = operation.IsNullable, DefaultValue = operation.DefaultValue, DefaultValueSql = operation.DefaultValueSql, ComputedColumnSql = operation.ComputedColumnSql, IsFixedLength = operation.IsFixedLength }; addColumnOperation.AddAnnotations(operation.GetAnnotations()); // TODO: Use a column rebuild instead indexesToRebuild = GetIndexesToRebuild(property, operation) .ToList(); DropIndexes(indexesToRebuild, builder); Generate(dropColumnOperation, model, builder, terminate: false); builder.AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator); Generate(addColumnOperation, model, builder, terminate: false); builder.AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator); CreateIndexes(indexesToRebuild, builder); builder.EndCommand(); return; } var narrowed = false; if (IsOldColumnSupported(model)) { if (IsIdentity(operation) != IsIdentity(operation.OldColumn)) { throw new InvalidOperationException(JetStrings.AlterIdentityColumn); } var type = operation.ColumnType ?? GetColumnType( operation.Schema, operation.Table, operation.Name, operation, model); var oldType = operation.OldColumn.ColumnType ?? GetColumnType( operation.Schema, operation.Table, operation.Name, operation.OldColumn, model); narrowed = type != oldType || !operation.IsNullable && operation.OldColumn.IsNullable; } if (narrowed) { indexesToRebuild = GetIndexesToRebuild(property, operation) .ToList(); DropIndexes(indexesToRebuild, builder); } DropDefaultConstraint(operation.Table, operation.Name, builder); builder .Append("ALTER TABLE ") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Table)) .Append(" ALTER COLUMN "); // NB: DefaultValue, DefaultValueSql, and identity are handled elsewhere. Don't copy them here. var definitionOperation = new AlterColumnOperation { Schema = operation.Schema, Table = operation.Table, Name = operation.Name, ClrType = operation.ClrType, ColumnType = operation.ColumnType, IsUnicode = operation.IsUnicode, IsFixedLength = operation.IsFixedLength, MaxLength = operation.MaxLength, IsRowVersion = operation.IsRowVersion, IsNullable = operation.IsNullable, ComputedColumnSql = operation.ComputedColumnSql, OldColumn = operation.OldColumn }; definitionOperation.AddAnnotations( operation.GetAnnotations() .Where( a => a.Name != JetAnnotationNames.ValueGenerationStrategy && a.Name != JetAnnotationNames.Identity)); ColumnDefinition( operation.Schema, operation.Table, operation.Name, definitionOperation, model, builder); builder.AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator); if (operation.DefaultValue != null || operation.DefaultValueSql != null) { builder .Append("ALTER TABLE ") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Table)) .Append(" ADD"); DefaultValue(operation.DefaultValue, operation.DefaultValueSql, operation.ColumnType, builder); builder .Append(" FOR ") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Name)) .AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator); } // TODO: Implement comment/description support. (ADOX/DAO) /* * if (operation.OldColumn.Comment != operation.Comment) * { * var dropDescription = operation.OldColumn.Comment != null; * if (dropDescription) * { * DropDescription( * builder, * operation.Schema, * operation.Table, * operation.Name); * } * * if (operation.Comment != null) * { * AddDescription( * builder, operation.Comment, * operation.Schema, * operation.Table, * operation.Name, * omitSchemaVariable: dropDescription); * } * } */ if (narrowed) { CreateIndexes(indexesToRebuild, builder); } builder.EndCommand(); }