Skip to content

Commit

Permalink
Fix splitting migrations SQL by GO (#32548)
Browse files Browse the repository at this point in the history
Fixes #32457
  • Loading branch information
ajcvickers committed Dec 8, 2023
1 parent 3ebc2ca commit f595929
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 3 deletions.
1 change: 1 addition & 0 deletions All.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 @@ -1418,7 +1418,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
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

0 comments on commit f595929

Please sign in to comment.