Skip to content
12 changes: 3 additions & 9 deletions src/EFCore.Relational/Migrations/Internal/Migrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,8 @@ private static void GenerateSqlScript(
if (!transactionStarted && !command.TransactionSuppressed)
{
builder
.AppendLine(sqlGenerationHelper.StartTransactionStatement);
.AppendLine(sqlGenerationHelper.StartTransactionStatement)
.Append(sqlGenerationHelper.BatchTerminator);
transactionStarted = true;
}

Expand Down Expand Up @@ -674,14 +675,7 @@ private static void GenerateSqlScript(
builder.Append(command.CommandText);
}

if (!transactionStarted)
{
builder.Append(sqlGenerationHelper.BatchTerminator);
}
else
Comment thread
AndriySvyryd marked this conversation as resolved.
Resolved
{
builder.Append(Environment.NewLine);
}
builder.Append(sqlGenerationHelper.BatchTerminator);
}

if (transactionStarted)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,24 +90,32 @@ CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId])
GO

BEGIN TRANSACTION;
GO

CREATE TABLE [Table1] (
[Id] int NOT NULL,
[Foo] int NOT NULL,
[Description] nvarchar(max) NOT NULL,
CONSTRAINT [PK_Table1] PRIMARY KEY ([Id])
);
GO

INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'00000000000001_Migration1', N'7.0.0-test');
GO

COMMIT;
GO

BEGIN TRANSACTION;
GO

EXEC sp_rename N'[Table1].[Foo]', N'Bar', 'COLUMN';
GO

INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'00000000000002_Migration2', N'7.0.0-test');
GO

COMMIT;
GO
Expand Down Expand Up @@ -155,29 +163,39 @@ INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
GO

BEGIN TRANSACTION;
GO

INSERT INTO Table1 (Id, Bar, Description) VALUES (-1, 3, 'Value With

Empty Lines')
GO

INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'00000000000005_Migration5', N'7.0.0-test');
GO

COMMIT;
GO

BEGIN TRANSACTION;
GO

INSERT INTO Table1 (Id, Bar, Description) VALUES (-2, 4, 'GO
Value With

Empty Lines')
GO

INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'00000000000006_Migration6', N'7.0.0-test');
GO

COMMIT;
GO

BEGIN TRANSACTION;
GO

INSERT INTO Table1 (Id, Bar, Description) VALUES (-3, 5, '--Start
GO
Value With
Expand All @@ -187,62 +205,87 @@ Value With
Empty Lines;
GO
')
GO

INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'00000000000007_Migration7', N'7.0.0-test');
GO

COMMIT;
GO

BEGIN TRANSACTION;
GO

DELETE FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'00000000000007_Migration7';
GO

COMMIT;
GO

BEGIN TRANSACTION;
GO

DELETE FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'00000000000006_Migration6';
GO

COMMIT;
GO

BEGIN TRANSACTION;
GO

DELETE FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'00000000000005_Migration5';
GO

COMMIT;
GO

BEGIN TRANSACTION;
GO

DELETE FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'00000000000004_Migration4';
GO

COMMIT;
GO

BEGIN TRANSACTION;
GO

DELETE FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'00000000000003_Migration3';
GO

COMMIT;
GO

BEGIN TRANSACTION;
GO

EXEC sp_rename N'[Table1].[Bar]', N'Foo', 'COLUMN';
GO

DELETE FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'00000000000002_Migration2';
GO

COMMIT;
GO

BEGIN TRANSACTION;
GO

DROP TABLE [Table1];
GO

DELETE FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'00000000000001_Migration1';
GO

COMMIT;
GO
Expand Down Expand Up @@ -411,19 +454,27 @@ public override async Task Can_generate_one_up_and_down_script()
Assert.Equal(
"""
BEGIN TRANSACTION;
GO
Comment thread
AndriySvyryd marked this conversation as resolved.
Resolved

EXEC sp_rename N'[Table1].[Foo]', N'Bar', 'COLUMN';
GO

INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'00000000000002_Migration2', N'7.0.0-test');
GO

COMMIT;
GO

BEGIN TRANSACTION;
GO

EXEC sp_rename N'[Table1].[Bar]', N'Foo', 'COLUMN';
GO

DELETE FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'00000000000002_Migration2';
GO

COMMIT;
GO
Expand All @@ -441,19 +492,27 @@ public override async Task Can_generate_up_and_down_script_using_names()
Assert.Equal(
"""
BEGIN TRANSACTION;
GO

EXEC sp_rename N'[Table1].[Foo]', N'Bar', 'COLUMN';
GO

INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'00000000000002_Migration2', N'7.0.0-test');
GO

COMMIT;
GO

BEGIN TRANSACTION;
GO

EXEC sp_rename N'[Table1].[Bar]', N'Foo', 'COLUMN';
GO

DELETE FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'00000000000002_Migration2';
GO

COMMIT;
GO
Expand Down Expand Up @@ -481,6 +540,8 @@ CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId])
GO

BEGIN TRANSACTION;
GO

IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'00000000000001_Migration1'
Expand All @@ -493,6 +554,7 @@ [Description] nvarchar(max) NOT NULL,
CONSTRAINT [PK_Table1] PRIMARY KEY ([Id])
);
END;
GO

IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
Expand All @@ -502,18 +564,22 @@ IF NOT EXISTS (
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'00000000000001_Migration1', N'7.0.0-test');
END;
GO

COMMIT;
GO

BEGIN TRANSACTION;
GO

IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'00000000000002_Migration2'
)
BEGIN
EXEC sp_rename N'[Table1].[Foo]', N'Bar', 'COLUMN';
END;
GO

IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
Expand All @@ -523,18 +589,22 @@ IF NOT EXISTS (
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'00000000000002_Migration2', N'7.0.0-test');
END;
GO

COMMIT;
GO

BEGIN TRANSACTION;
GO

IF EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'00000000000002_Migration2'
)
BEGIN
EXEC sp_rename N'[Table1].[Bar]', N'Foo', 'COLUMN';
END;
GO

IF EXISTS (
SELECT * FROM [__EFMigrationsHistory]
Expand All @@ -544,18 +614,22 @@ IF EXISTS (
DELETE FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'00000000000002_Migration2';
END;
GO

COMMIT;
GO

BEGIN TRANSACTION;
GO

IF EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'00000000000001_Migration1'
)
BEGIN
DROP TABLE [Table1];
END;
GO

IF EXISTS (
SELECT * FROM [__EFMigrationsHistory]
Expand All @@ -565,6 +639,7 @@ IF EXISTS (
DELETE FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'00000000000001_Migration1';
END;
GO

COMMIT;
GO
Expand Down
Loading
Loading