Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions src/EFCore.Relational/Migrations/Internal/Migrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ private bool MigrateImplementation(
}

_migrationCommandExecutor.ExecuteNonQuery(
getCommands(), _connection, state, commitTransaction: false, MigrationTransactionIsolationLevel);
getCommands(), _connection, state, commitTransaction: useTransaction, MigrationTransactionIsolationLevel);
}

var coreOptionsExtension =
Expand Down Expand Up @@ -317,7 +317,7 @@ private async Task<bool> MigrateImplementationAsync(
}

await _migrationCommandExecutor.ExecuteNonQueryAsync(
getCommands(), _connection, state, commitTransaction: false, MigrationTransactionIsolationLevel, cancellationToken)
getCommands(), _connection, state, commitTransaction: useTransaction, MigrationTransactionIsolationLevel, cancellationToken)
.ConfigureAwait(false);
}

Expand Down Expand Up @@ -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];
Expand All @@ -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)
Expand All @@ -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();
Expand All @@ -641,11 +633,11 @@ private static void GenerateSqlScript(
IEnumerable<MigrationCommand> 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)
Expand Down Expand Up @@ -691,6 +683,13 @@ private static void GenerateSqlScript(
builder.Append(Environment.NewLine);
}
}

if (transactionStarted)
{
builder
.AppendLine(sqlGenerationHelper.CommitTransactionStatement)
.Append(sqlGenerationHelper.BatchTerminator);
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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]
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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"
Expand Down
Loading