protected override void Generate([NotNull] MigrationOperation operation, [CanBeNull] IModel model, [NotNull] MigrationCommandListBuilder builder) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); var createDatabaseOperation = operation as MySqlCreateDatabaseOperation; if (createDatabaseOperation != null) { Generate(createDatabaseOperation, model, builder); builder.EndCommand(); return; } var dropDatabaseOperation = operation as MySqlDropDatabaseOperation; if (dropDatabaseOperation is MySqlDropDatabaseOperation) { Generate(dropDatabaseOperation, model, builder); builder.EndCommand(); return; } base.Generate(operation, model, builder); }
protected override void Generate(RenameIndexOperation operation, IModel model, MigrationCommandListBuilder builder) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); if (model == null) { throw new NotSupportedException(string.Format(NotSupported, operation.GetType().Name)); } var index = FindEntityType(model, null, operation.Table).GetIndexes().Single(i => _annotations.For(i).Name == operation.NewName); var dropIndexOperation = new DropIndexOperation { Name = operation.Name, IsDestructiveChange = true, Table = operation.Table }; builder.EndCommand(); Generate(dropIndexOperation, model, builder); var createIndexOperation = new CreateIndexOperation { Columns = index.Properties.Select(p => p.Name).ToArray(), IsUnique = index.IsUnique, Name = operation.NewName, Table = operation.Table }; builder.EndCommand(); Generate(createIndexOperation, model, builder); }
protected override void Generate(AlterColumnOperation operation, IModel model, MigrationCommandListBuilder builder) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); builder .EndCommand() .Append("ALTER TABLE ") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Table)) .Append(" ALTER COLUMN ") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Name)) .Append(" DROP DEFAULT") .AppendLine(); builder .EndCommand() .Append("ALTER TABLE ") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Table)) .Append(" ALTER COLUMN "); ColumnDefinition( null, 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(); if ((operation.DefaultValue != null) || (operation.DefaultValueSql != null)) { builder .EndCommand() .Append("ALTER TABLE ") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Table)) .Append(" ALTER COLUMN ") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Name)) .Append(" SET "); DefaultValue(operation.DefaultValue, operation.DefaultValueSql, builder); } }
protected override void Generate( RenameTableOperation operation, IModel model, MigrationCommandListBuilder builder) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); var name = operation.Name; if (operation.NewName != null) { var qualifiedName = new StringBuilder(); if (operation.Schema != null) { qualifiedName .Append(operation.Schema) .Append("."); } qualifiedName.Append(operation.Name); Rename(qualifiedName.ToString(), operation.NewName, builder); name = operation.NewName; } if (operation.NewSchema != null) { Transfer(operation.NewSchema, operation.Schema, name, builder); } builder.EndCommand(); }
protected override void Generate( RenameIndexOperation operation, IModel model, MigrationCommandListBuilder builder) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); if (string.IsNullOrEmpty(operation.Table)) { throw new InvalidOperationException(OracleStrings.IndexTableRequired); } var qualifiedName = new StringBuilder(); if (operation.Schema != null) { qualifiedName .Append(operation.Schema) .Append("."); } qualifiedName .Append(operation.Table) .Append(".") .Append(operation.Name); Rename(qualifiedName.ToString(), operation.NewName, "INDEX", builder); builder.EndCommand(); }
public override IReadOnlyList <MigrationCommand> Generate(IReadOnlyList <MigrationOperation> operations, IModel model = null) { Check.NotNull(operations, nameof(operations)); var builder = new MigrationCommandListBuilder(_commandBuilderFactory); foreach (var operation in operations) { Generate(operation, model, builder); builder .EndCommand(); } var list = builder.GetCommandList(); //HACK to force logging of migration SQL foreach (var migrationCommand in list) { if (!string.IsNullOrEmpty(migrationCommand.CommandText)) { //TODO ErikEJ Investigate how to fix this? //_logger.Logger.LogCommandExecuted(new SqlCeCommand(migrationCommand.CommandText), Stopwatch.GetTimestamp(), Stopwatch.GetTimestamp()); } } return(list); }
protected override void Generate( RenameColumnOperation operation, IModel model, MigrationCommandListBuilder builder) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); var qualifiedName = new StringBuilder(); if (operation.Schema != null) { qualifiedName .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Schema)) .Append("."); } qualifiedName.Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Table)); builder .Append("ALTER TABLE ") .Append(qualifiedName) .Append(" RENAME COLUMN ") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Name)) .Append(" TO ") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.NewName)); builder.EndCommand(); }
protected override void Generate( InsertDataOperation operation, IModel model, MigrationCommandListBuilder builder) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); var sqlBuilder = new StringBuilder(); foreach (var modificationCommand in operation.GenerateModificationCommands(model)) { SqlGenerator.AppendInsertOperation( sqlBuilder, modificationCommand, 0); } builder .AppendLine("BEGIN") .Append(sqlBuilder) .Append("END") .AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator); builder.EndCommand(); }
protected virtual void EndStatement( [NotNull] MigrationCommandListBuilder builder, bool suppressTransaction = false) { Check.NotNull(builder, nameof(builder)); builder.EndCommand(suppressTransaction); }
protected override void Generate( InsertDataOperation operation, IModel model, MigrationCommandListBuilder builder) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); builder.Append("IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [object_id] = OBJECT_ID(N'"); if (operation.Schema != null) { builder .Append(Dependencies.SqlGenerationHelper.EscapeLiteral(operation.Schema)) .Append("."); } builder .Append(Dependencies.SqlGenerationHelper.EscapeLiteral(operation.Table)) .AppendLine("'))"); using (builder.Indent()) { builder .Append("SET IDENTITY_INSERT ") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Table, operation.Schema)) .Append(" ON") .AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator); } base.Generate(operation, model, builder, terminate: false); builder .AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator) .Append("IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [object_id] = OBJECT_ID(N'"); if (operation.Schema != null) { builder .Append(Dependencies.SqlGenerationHelper.EscapeLiteral(operation.Schema)) .Append("."); } builder .Append(Dependencies.SqlGenerationHelper.EscapeLiteral(operation.Table)) .AppendLine("'))"); using (builder.Indent()) { builder .Append("SET IDENTITY_INSERT ") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Table, operation.Schema)) .Append(" OFF") .AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator); } builder.EndCommand(); }
protected override void Generate(DropIndexOperation operation, IModel model, MigrationCommandListBuilder builder) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); builder .EndCommand() .Append("DROP INDEX ") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Table)) .Append(".") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Name)); }
protected override void Generate(RenameTableOperation operation, IModel model, MigrationCommandListBuilder builder) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); if (operation.NewName != null) { builder .EndCommand() .Append("sp_rename N'") .Append(operation.Name) .Append("', N'") .Append(operation.NewName) .Append("'"); } }
public override IReadOnlyList <MigrationCommand> Generate(IReadOnlyList <MigrationOperation> operations, IModel model = null) { Check.NotNull(operations, nameof(operations)); var builder = new MigrationCommandListBuilder(_commandBuilderFactory); foreach (var operation in operations) { Generate(operation, model, builder); builder .EndCommand(); } var list = builder.GetCommandList(); return(list); }
protected override void Generate( RenameIndexOperation operation, IModel model, MigrationCommandListBuilder builder) { if (operation.NewName != null) { builder .Append("ALTER INDEX ") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Name)) .Append(" RENAME TO ") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.NewName)); } builder.EndCommand(); }
/// <summary> /// Builds commands for the given <see cref="InsertDataOperation" /> by making calls on the given /// <see cref="MigrationCommandListBuilder" />, and then terminates the final command. /// </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> /// <param name="terminate"> Indicates whether or not to terminate the command after generating SQL for the operation. </param> protected override void Generate( InsertDataOperation operation, IModel model, MigrationCommandListBuilder builder, bool terminate = true) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); var sqlBuilder = new StringBuilder(); builder.Append(sqlBuilder.ToString()); if (terminate) { builder.EndCommand(); } }
protected virtual void Generate( [NotNull] SqlServerDropDatabaseOperation operation, [CanBeNull] IModel model, [NotNull] MigrationCommandListBuilder builder) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); builder .EndCommand() .Append("IF SERVERPROPERTY('EngineEdition') <> 5 EXEC(N'ALTER DATABASE ") .Append(SqlGenerationHelper.DelimitIdentifier(operation.Name)) .Append(" SET SINGLE_USER WITH ROLLBACK IMMEDIATE')") .AppendLine(SqlGenerationHelper.StatementTerminator) .EndCommand(suppressTransaction: true) .Append("DROP DATABASE ") .Append(SqlGenerationHelper.DelimitIdentifier(operation.Name)); EndStatement(builder, suppressTransaction: true); }
protected virtual void Generate( [NotNull] SqlServerCreateDatabaseOperation operation, [CanBeNull] IModel model, [NotNull] MigrationCommandListBuilder builder) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); builder .EndCommand() .Append("CREATE DATABASE ") .Append(SqlGenerationHelper.DelimitIdentifier(operation.Name)) .AppendLine(SqlGenerationHelper.StatementTerminator) .EndCommand(suppressTransaction: true) .Append("IF SERVERPROPERTY('EngineEdition') <> 5 EXEC(N'ALTER DATABASE ") .Append(SqlGenerationHelper.DelimitIdentifier(operation.Name)) .Append(" SET READ_COMMITTED_SNAPSHOT ON')"); EndStatement(builder, suppressTransaction: true); }
/// <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)); builder .Append("ALTER TABLE ") .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Table, operation.Schema)) .Append(" MODIFY COLUMN "); ColumnDefinition( operation.Schema, operation.Table, operation.Name, operation, model, builder); builder.AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator); builder.EndCommand(); }
protected override void Generate( RenameColumnOperation operation, IModel model, MigrationCommandListBuilder builder) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); var qualifiedName = new StringBuilder(); if (operation.Schema != null) { qualifiedName .Append(operation.Schema) .Append("."); } qualifiedName .Append(operation.Table) .Append(".") .Append(operation.Name); Rename(qualifiedName.ToString(), operation.NewName, "COLUMN", builder); builder.EndCommand(); }
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(); }
/// <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(); }
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 override void Generate( AlterDatabaseOperation operation, IModel model, MigrationCommandListBuilder builder) { Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); builder.AppendLine("IF SERVERPROPERTY('IsXTPSupported') = 1 AND SERVERPROPERTY('EngineEdition') <> 5"); using (builder.Indent()) { builder .AppendLine("BEGIN") .AppendLine("IF NOT EXISTS ("); using (builder.Indent()) { builder .Append("SELECT 1 FROM [sys].[filegroups] [FG] ") .Append("JOIN [sys].[database_files] [F] ON [FG].[data_space_id] = [F].[data_space_id] ") .AppendLine("WHERE [FG].[type] = N'FX' AND [F].[type] = 2)"); } using (builder.Indent()) { builder .AppendLine("BEGIN") .AppendLine("DECLARE @db_name NVARCHAR(MAX) = DB_NAME();") .AppendLine("DECLARE @fg_name NVARCHAR(MAX);") .AppendLine("SELECT TOP(1) @fg_name = [name] FROM [sys].[filegroups] WHERE [type] = N'FX';") .AppendLine() .AppendLine("IF @fg_name IS NULL"); using (builder.Indent()) { builder .AppendLine("BEGIN") .AppendLine("SET @fg_name = @db_name + N'_MODFG';") .AppendLine("EXEC(N'ALTER DATABASE CURRENT ADD FILEGROUP [' + @fg_name + '] CONTAINS MEMORY_OPTIMIZED_DATA;');") .AppendLine("END"); } builder .AppendLine() .AppendLine("DECLARE @path NVARCHAR(MAX);") .Append("SELECT TOP(1) @path = [physical_name] FROM [sys].[database_files] ") .AppendLine("WHERE charindex('\\', [physical_name]) > 0 ORDER BY [file_id];") .AppendLine("IF (@path IS NULL)") .IncrementIndent().AppendLine("SET @path = '\\' + @db_name;").DecrementIndent() .AppendLine() .AppendLine("DECLARE @filename NVARCHAR(MAX) = right(@path, charindex('\\', reverse(@path)) - 1);") .AppendLine("SET @filename = REPLACE(left(@filename, len(@filename) - charindex('.', reverse(@filename))), '''', '''''') + N'_MOD';") .AppendLine("DECLARE @new_path NVARCHAR(MAX) = REPLACE(CAST(SERVERPROPERTY('InstanceDefaultDataPath') AS NVARCHAR(MAX)), '''', '''''') + @filename;") .AppendLine() .AppendLine("EXEC(N'"); using (builder.Indent()) { builder .AppendLine("ALTER DATABASE CURRENT") .AppendLine("ADD FILE (NAME=''' + @filename + ''', filename=''' + @new_path + ''')") .AppendLine("TO FILEGROUP [' + @fg_name + '];')"); } builder.AppendLine("END"); } builder.AppendLine("END"); } builder.AppendLine() .AppendLine("IF SERVERPROPERTY('IsXTPSupported') = 1") .AppendLine("EXEC(N'"); using (builder.Indent()) { builder .AppendLine("ALTER DATABASE CURRENT") .AppendLine("SET MEMORY_OPTIMIZED_ELEVATE_TO_SNAPSHOT ON;')"); } builder.EndCommand(suppressTransaction: true); }