diff --git a/src/EFCore.Relational/Migrations/Internal/Migrator.cs b/src/EFCore.Relational/Migrations/Internal/Migrator.cs index 8e0d1ef5cd7..a767c77be06 100644 --- a/src/EFCore.Relational/Migrations/Internal/Migrator.cs +++ b/src/EFCore.Relational/Migrations/Internal/Migrator.cs @@ -182,7 +182,7 @@ private bool MigrateImplementation( } _migrationCommandExecutor.ExecuteNonQuery( - getCommands(), _connection, state, commitTransaction: false, MigrationTransactionIsolationLevel); + getCommands(), _connection, state, commitTransaction: useTransaction, MigrationTransactionIsolationLevel); } var coreOptionsExtension = @@ -317,7 +317,7 @@ private async Task MigrateImplementationAsync( } await _migrationCommandExecutor.ExecuteNonQueryAsync( - getCommands(), _connection, state, commitTransaction: false, MigrationTransactionIsolationLevel, cancellationToken) + getCommands(), _connection, state, commitTransaction: useTransaction, MigrationTransactionIsolationLevel, cancellationToken) .ConfigureAwait(false); } @@ -595,7 +595,6 @@ public virtual string GenerateScript( var migrationsToApply = migratorData.AppliedMigrations; var migrationsToRevert = migratorData.RevertedMigrations; var actualTargetMigration = migratorData.TargetMigration; - var transactionStarted = false; for (var i = 0; i < migrationsToRevert.Count; i++) { var migration = migrationsToRevert[i]; @@ -611,7 +610,7 @@ public virtual string GenerateScript( GenerateSqlScript( GenerateDownSql(migration, previousMigration, options), - builder, _sqlGenerationHelper, ref transactionStarted, noTransactions, idempotencyCondition, idempotencyEnd); + builder, _sqlGenerationHelper, noTransactions, idempotencyCondition, idempotencyEnd); } foreach (var migration in migrationsToApply) @@ -624,14 +623,7 @@ public virtual string GenerateScript( GenerateSqlScript( GenerateUpSql(migration, options), - builder, _sqlGenerationHelper, ref transactionStarted, noTransactions, idempotencyCondition, idempotencyEnd); - } - - if (transactionStarted) - { - builder - .AppendLine(_sqlGenerationHelper.CommitTransactionStatement) - .Append(_sqlGenerationHelper.BatchTerminator); + builder, _sqlGenerationHelper, noTransactions, idempotencyCondition, idempotencyEnd); } return builder.ToString(); @@ -641,11 +633,11 @@ private static void GenerateSqlScript( IEnumerable commands, IndentedStringBuilder builder, ISqlGenerationHelper sqlGenerationHelper, - ref bool transactionStarted, bool noTransactions = false, string? idempotencyCondition = null, string? idempotencyEnd = null) { + var transactionStarted = false; foreach (var command in commands) { if (!noTransactions) @@ -691,6 +683,13 @@ private static void GenerateSqlScript( builder.Append(Environment.NewLine); } } + + if (transactionStarted) + { + builder + .AppendLine(sqlGenerationHelper.CommitTransactionStatement) + .Append(sqlGenerationHelper.BatchTerminator); + } } /// diff --git a/test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsInfrastructureSqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsInfrastructureSqlServerTest.cs index a5cf93d959c..bdef788dd58 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsInfrastructureSqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsInfrastructureSqlServerTest.cs @@ -100,6 +100,10 @@ CONSTRAINT [PK_Table1] PRIMARY KEY ([Id]) INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion]) VALUES (N'00000000000001_Migration1', N'7.0.0-test'); +COMMIT; +GO + +BEGIN TRANSACTION; EXEC sp_rename N'[Table1].[Foo]', N'Bar', 'COLUMN'; INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion]) @@ -158,6 +162,10 @@ INSERT INTO Table1 (Id, Bar, Description) VALUES (-1, 3, 'Value With INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion]) VALUES (N'00000000000005_Migration5', N'7.0.0-test'); +COMMIT; +GO + +BEGIN TRANSACTION; INSERT INTO Table1 (Id, Bar, Description) VALUES (-2, 4, 'GO Value With @@ -166,6 +174,10 @@ Value With INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion]) VALUES (N'00000000000006_Migration6', N'7.0.0-test'); +COMMIT; +GO + +BEGIN TRANSACTION; INSERT INTO Table1 (Id, Bar, Description) VALUES (-3, 5, '--Start GO Value With @@ -186,23 +198,47 @@ INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion]) DELETE FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'00000000000007_Migration7'; +COMMIT; +GO + +BEGIN TRANSACTION; DELETE FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'00000000000006_Migration6'; +COMMIT; +GO + +BEGIN TRANSACTION; DELETE FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'00000000000005_Migration5'; +COMMIT; +GO + +BEGIN TRANSACTION; DELETE FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'00000000000004_Migration4'; +COMMIT; +GO + +BEGIN TRANSACTION; DELETE FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'00000000000003_Migration3'; +COMMIT; +GO + +BEGIN TRANSACTION; EXEC sp_rename N'[Table1].[Bar]', N'Foo', 'COLUMN'; DELETE FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'00000000000002_Migration2'; +COMMIT; +GO + +BEGIN TRANSACTION; DROP TABLE [Table1]; DELETE FROM [__EFMigrationsHistory] @@ -467,6 +503,10 @@ INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion]) VALUES (N'00000000000001_Migration1', N'7.0.0-test'); END; +COMMIT; +GO + +BEGIN TRANSACTION; IF NOT EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'00000000000002_Migration2' @@ -505,6 +545,10 @@ DELETE FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'00000000000002_Migration2'; END; +COMMIT; +GO + +BEGIN TRANSACTION; IF EXISTS ( SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'00000000000001_Migration1' diff --git a/test/EFCore.Sqlite.FunctionalTests/Migrations/MigrationsInfrastructureSqliteTest.cs b/test/EFCore.Sqlite.FunctionalTests/Migrations/MigrationsInfrastructureSqliteTest.cs index 7b56e7a4bf2..c57a2a2f098 100644 --- a/test/EFCore.Sqlite.FunctionalTests/Migrations/MigrationsInfrastructureSqliteTest.cs +++ b/test/EFCore.Sqlite.FunctionalTests/Migrations/MigrationsInfrastructureSqliteTest.cs @@ -68,23 +68,41 @@ public override async Task Can_generate_up_and_down_scripts() INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion") VALUES ('00000000000001_Migration1', '7.0.0-test'); +COMMIT; + +BEGIN TRANSACTION; ALTER TABLE "Table1" RENAME COLUMN "Foo" TO "Bar"; INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion") VALUES ('00000000000002_Migration2', '7.0.0-test'); +COMMIT; + +BEGIN TRANSACTION; INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion") VALUES ('00000000000003_Migration3', '7.0.0-test'); +COMMIT; + +BEGIN TRANSACTION; INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion") VALUES ('00000000000004_Migration4', '7.0.0-test'); +COMMIT; + +BEGIN TRANSACTION; INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion") VALUES ('00000000000005_Migration5', '7.0.0-test'); +COMMIT; + +BEGIN TRANSACTION; INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion") VALUES ('00000000000006_Migration6', '7.0.0-test'); +COMMIT; + +BEGIN TRANSACTION; INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion") VALUES ('00000000000007_Migration7', '7.0.0-test'); @@ -94,23 +112,41 @@ public override async Task Can_generate_up_and_down_scripts() DELETE FROM "__EFMigrationsHistory" WHERE "MigrationId" = '00000000000007_Migration7'; +COMMIT; + +BEGIN TRANSACTION; DELETE FROM "__EFMigrationsHistory" WHERE "MigrationId" = '00000000000006_Migration6'; +COMMIT; + +BEGIN TRANSACTION; DELETE FROM "__EFMigrationsHistory" WHERE "MigrationId" = '00000000000005_Migration5'; +COMMIT; + +BEGIN TRANSACTION; DELETE FROM "__EFMigrationsHistory" WHERE "MigrationId" = '00000000000004_Migration4'; +COMMIT; + +BEGIN TRANSACTION; DELETE FROM "__EFMigrationsHistory" WHERE "MigrationId" = '00000000000003_Migration3'; +COMMIT; + +BEGIN TRANSACTION; ALTER TABLE "Table1" RENAME COLUMN "Bar" TO "Foo"; DELETE FROM "__EFMigrationsHistory" WHERE "MigrationId" = '00000000000002_Migration2'; +COMMIT; + +BEGIN TRANSACTION; DROP TABLE "Table1"; DELETE FROM "__EFMigrationsHistory"