Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix splitting migrations SQL by GO #32548

Merged
merged 1 commit into from
Dec 8, 2023
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
1 change: 1 addition & 0 deletions EFCore.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ The .NET Foundation licenses this file to you under the MIT license.
<s:Boolean x:Key="/Default/Environment/InjectedLayers/InjectedLayerCustomization/=FileEF4F00E20178B341995BD2EFE53739B5/@KeyIndexDefined">True</s:Boolean>
<s:Double x:Key="/Default/Environment/InjectedLayers/InjectedLayerCustomization/=FileEF4F00E20178B341995BD2EFE53739B5/RelativePriority/@EntryValue">2</s:Double>
<s:String x:Key="/Default/Environment/PerformanceGuide/SwitchBehaviour/=VsBulb/@EntryIndexedValue">DO_NOTHING</s:String>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EFeature_002EServices_002ECodeCleanup_002EFileHeader_002EFileHeaderSettingsMigrate/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EFeature_002EServices_002EDaemon_002ESettings_002EMigration_002ESwaWarningsModeSettingsMigrate/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpAttributeForSingleLineMethodUpgrade/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1381,7 +1381,9 @@ protected override void Generate(SqlOperation operation, IModel? model, Migratio
}

var trimmed = line.TrimStart();
if (trimmed.StartsWith("GO", StringComparison.OrdinalIgnoreCase))
if (trimmed.StartsWith("GO", StringComparison.OrdinalIgnoreCase)
&& (trimmed.Length == 2
|| char.IsWhiteSpace(trimmed[2])))
{
var batch = batchBuilder.ToString();
batchBuilder.Clear();
Expand Down Expand Up @@ -2489,7 +2491,7 @@ private IReadOnlyList<MigrationOperation> RewriteOperations(
// for create table we always generate new temporal information from the operation itself
// just in case there was a table with that name before that got deleted/renamed
// this shouldn't happen as we re-use existin tables rather than drop/recreate
// but we are being extra defensive here
// but we are being extra defensive here
// and also, temporal state (disabled versioning etc.) should always reset when creating a table
temporalInformation = BuildTemporalInformationFromMigrationOperation(schema, createTableOperation);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public virtual void Can_apply_all_migrations()
history.GetAppliedMigrations(),
x => Assert.Equal("00000000000001_Migration1", x.MigrationId),
x => Assert.Equal("00000000000002_Migration2", x.MigrationId),
x => Assert.Equal("00000000000003_Migration3", x.MigrationId));
x => Assert.Equal("00000000000003_Migration3", x.MigrationId),
x => Assert.Equal("00000000000004_Migration4", x.MigrationId));
}

[ConditionalFact]
Expand Down Expand Up @@ -142,7 +143,8 @@ public virtual async Task Can_apply_all_migrations_async()
await history.GetAppliedMigrationsAsync(),
x => Assert.Equal("00000000000001_Migration1", x.MigrationId),
x => Assert.Equal("00000000000002_Migration2", x.MigrationId),
x => Assert.Equal("00000000000003_Migration3", x.MigrationId));
x => Assert.Equal("00000000000003_Migration3", x.MigrationId),
x => Assert.Equal("00000000000004_Migration4", x.MigrationId));
}

[ConditionalFact]
Expand Down Expand Up @@ -412,4 +414,42 @@ protected override void Down(MigrationBuilder migrationBuilder)
{
}
}

[DbContext(typeof(MigrationsContext))]
[Migration("00000000000004_Migration4")]
private class Migration4 : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
if (ActiveProvider == "Microsoft.EntityFrameworkCore.SqlServer")
{
migrationBuilder.Sql("""
CREATE PROCEDURE [dbo].[GotoReproduction]
AS
BEGIN
DECLARE @Counter int;
SET @Counter = 1;
WHILE @Counter < 10
BEGIN
SELECT @Counter
SET @Counter = @Counter + 1
IF @Counter = 4 GOTO Branch_One --Jumps to the first branch.
IF @Counter = 5 GOTO Branch_Two --This will never execute.
END
Branch_One:
SELECT 'Jumping To Branch One.'
GOTO Branch_Three; --This will prevent Branch_Two from executing.
Branch_Two:
SELECT 'Jumping To Branch Two.'
Branch_Three:
SELECT 'Jumping To Branch Three.'
END;
""");
}
}

protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,38 @@ INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
COMMIT;
GO

BEGIN TRANSACTION;
GO

CREATE PROCEDURE [dbo].[GotoReproduction]
AS
BEGIN
DECLARE @Counter int;
SET @Counter = 1;
WHILE @Counter < 10
BEGIN
SELECT @Counter
SET @Counter = @Counter + 1
IF @Counter = 4 GOTO Branch_One --Jumps to the first branch.
IF @Counter = 5 GOTO Branch_Two --This will never execute.
END
Branch_One:
SELECT 'Jumping To Branch One.'
GOTO Branch_Three; --This will prevent Branch_Two from executing.
Branch_Two:
SELECT 'Jumping To Branch Two.'
Branch_Three:
SELECT 'Jumping To Branch Three.'
END;
GO

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

COMMIT;
GO


""",
Sql,
Expand Down Expand Up @@ -173,6 +205,32 @@ INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'00000000000003_Migration3', N'7.0.0-test');
GO

CREATE PROCEDURE [dbo].[GotoReproduction]
AS
BEGIN
DECLARE @Counter int;
SET @Counter = 1;
WHILE @Counter < 10
BEGIN
SELECT @Counter
SET @Counter = @Counter + 1
IF @Counter = 4 GOTO Branch_One --Jumps to the first branch.
IF @Counter = 5 GOTO Branch_Two --This will never execute.
END
Branch_One:
SELECT 'Jumping To Branch One.'
GOTO Branch_Three; --This will prevent Branch_Two from executing.
Branch_Two:
SELECT 'Jumping To Branch Two.'
Branch_Three:
SELECT 'Jumping To Branch Three.'
END;
GO

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


""",
Sql,
Expand Down Expand Up @@ -333,6 +391,50 @@ INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
COMMIT;
GO

BEGIN TRANSACTION;
GO

IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'00000000000004_Migration4'
)
BEGIN
CREATE PROCEDURE [dbo].[GotoReproduction]
AS
BEGIN
DECLARE @Counter int;
SET @Counter = 1;
WHILE @Counter < 10
BEGIN
SELECT @Counter
SET @Counter = @Counter + 1
IF @Counter = 4 GOTO Branch_One --Jumps to the first branch.
IF @Counter = 5 GOTO Branch_Two --This will never execute.
END
Branch_One:
SELECT 'Jumping To Branch One.'
GOTO Branch_Three; --This will prevent Branch_Two from executing.
Branch_Two:
SELECT 'Jumping To Branch Two.'
Branch_Three:
SELECT 'Jumping To Branch Three.'
END;
END;
GO

IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'00000000000004_Migration4'
)
BEGIN
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'00000000000004_Migration4', N'7.0.0-test');
END;
GO

COMMIT;
GO


""",
Sql,
Expand Down Expand Up @@ -425,6 +527,44 @@ INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
END;
GO

IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'00000000000004_Migration4'
)
BEGIN
CREATE PROCEDURE [dbo].[GotoReproduction]
AS
BEGIN
DECLARE @Counter int;
SET @Counter = 1;
WHILE @Counter < 10
BEGIN
SELECT @Counter
SET @Counter = @Counter + 1
IF @Counter = 4 GOTO Branch_One --Jumps to the first branch.
IF @Counter = 5 GOTO Branch_Two --This will never execute.
END
Branch_One:
SELECT 'Jumping To Branch One.'
GOTO Branch_Three; --This will prevent Branch_Two from executing.
Branch_Two:
SELECT 'Jumping To Branch Two.'
Branch_Three:
SELECT 'Jumping To Branch Three.'
END;
END;
GO

IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'00000000000004_Migration4'
)
BEGIN
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'00000000000004_Migration4', N'7.0.0-test');
END;
GO


""",
Sql,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ public override void Can_generate_up_scripts()

COMMIT;

BEGIN TRANSACTION;

INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
VALUES ('00000000000004_Migration4', '7.0.0-test');

COMMIT;


""",
Sql,
Expand Down Expand Up @@ -121,6 +128,9 @@ public override void Can_generate_up_scripts_noTransactions()
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
VALUES ('00000000000003_Migration3', '7.0.0-test');

INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
VALUES ('00000000000004_Migration4', '7.0.0-test');


""",
Sql,
Expand Down
Loading