Skip to content

Commit 5a4ffe6

Browse files
authored
Fix splitting migrations SQL by GO (#32548)
Fixes #32457
1 parent 679b311 commit 5a4ffe6

File tree

5 files changed

+197
-4
lines changed

5 files changed

+197
-4
lines changed

EFCore.sln.DotSettings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ The .NET Foundation licenses this file to you under the MIT license.
268268
<s:Boolean x:Key="/Default/Environment/InjectedLayers/InjectedLayerCustomization/=FileEF4F00E20178B341995BD2EFE53739B5/@KeyIndexDefined">True</s:Boolean>
269269
<s:Double x:Key="/Default/Environment/InjectedLayers/InjectedLayerCustomization/=FileEF4F00E20178B341995BD2EFE53739B5/RelativePriority/@EntryValue">2</s:Double>
270270
<s:String x:Key="/Default/Environment/PerformanceGuide/SwitchBehaviour/=VsBulb/@EntryIndexedValue">DO_NOTHING</s:String>
271+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EFeature_002EServices_002ECodeCleanup_002EFileHeader_002EFileHeaderSettingsMigrate/@EntryIndexedValue">True</s:Boolean>
271272
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EFeature_002EServices_002EDaemon_002ESettings_002EMigration_002ESwaWarningsModeSettingsMigrate/@EntryIndexedValue">True</s:Boolean>
272273
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpAttributeForSingleLineMethodUpgrade/@EntryIndexedValue">True</s:Boolean>
273274
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>

src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,7 +1381,9 @@ protected override void Generate(SqlOperation operation, IModel? model, Migratio
13811381
}
13821382

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

test/EFCore.Relational.Specification.Tests/Migrations/MigrationsInfrastructureTestBase.cs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ public virtual void Can_apply_all_migrations()
7171
history.GetAppliedMigrations(),
7272
x => Assert.Equal("00000000000001_Migration1", x.MigrationId),
7373
x => Assert.Equal("00000000000002_Migration2", x.MigrationId),
74-
x => Assert.Equal("00000000000003_Migration3", x.MigrationId));
74+
x => Assert.Equal("00000000000003_Migration3", x.MigrationId),
75+
x => Assert.Equal("00000000000004_Migration4", x.MigrationId));
7576
}
7677

7778
[ConditionalFact]
@@ -142,7 +143,8 @@ public virtual async Task Can_apply_all_migrations_async()
142143
await history.GetAppliedMigrationsAsync(),
143144
x => Assert.Equal("00000000000001_Migration1", x.MigrationId),
144145
x => Assert.Equal("00000000000002_Migration2", x.MigrationId),
145-
x => Assert.Equal("00000000000003_Migration3", x.MigrationId));
146+
x => Assert.Equal("00000000000003_Migration3", x.MigrationId),
147+
x => Assert.Equal("00000000000004_Migration4", x.MigrationId));
146148
}
147149

148150
[ConditionalFact]
@@ -412,4 +414,42 @@ protected override void Down(MigrationBuilder migrationBuilder)
412414
{
413415
}
414416
}
417+
418+
[DbContext(typeof(MigrationsContext))]
419+
[Migration("00000000000004_Migration4")]
420+
private class Migration4 : Migration
421+
{
422+
protected override void Up(MigrationBuilder migrationBuilder)
423+
{
424+
if (ActiveProvider == "Microsoft.EntityFrameworkCore.SqlServer")
425+
{
426+
migrationBuilder.Sql("""
427+
CREATE PROCEDURE [dbo].[GotoReproduction]
428+
AS
429+
BEGIN
430+
DECLARE @Counter int;
431+
SET @Counter = 1;
432+
WHILE @Counter < 10
433+
BEGIN
434+
SELECT @Counter
435+
SET @Counter = @Counter + 1
436+
IF @Counter = 4 GOTO Branch_One --Jumps to the first branch.
437+
IF @Counter = 5 GOTO Branch_Two --This will never execute.
438+
END
439+
Branch_One:
440+
SELECT 'Jumping To Branch One.'
441+
GOTO Branch_Three; --This will prevent Branch_Two from executing.
442+
Branch_Two:
443+
SELECT 'Jumping To Branch Two.'
444+
Branch_Three:
445+
SELECT 'Jumping To Branch Three.'
446+
END;
447+
""");
448+
}
449+
}
450+
451+
protected override void Down(MigrationBuilder migrationBuilder)
452+
{
453+
}
454+
}
415455
}

test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsInfrastructureSqlServerTest.cs

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,38 @@ INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
123123
COMMIT;
124124
GO
125125
126+
BEGIN TRANSACTION;
127+
GO
128+
129+
CREATE PROCEDURE [dbo].[GotoReproduction]
130+
AS
131+
BEGIN
132+
DECLARE @Counter int;
133+
SET @Counter = 1;
134+
WHILE @Counter < 10
135+
BEGIN
136+
SELECT @Counter
137+
SET @Counter = @Counter + 1
138+
IF @Counter = 4 GOTO Branch_One --Jumps to the first branch.
139+
IF @Counter = 5 GOTO Branch_Two --This will never execute.
140+
END
141+
Branch_One:
142+
SELECT 'Jumping To Branch One.'
143+
GOTO Branch_Three; --This will prevent Branch_Two from executing.
144+
Branch_Two:
145+
SELECT 'Jumping To Branch Two.'
146+
Branch_Three:
147+
SELECT 'Jumping To Branch Three.'
148+
END;
149+
GO
150+
151+
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
152+
VALUES (N'00000000000004_Migration4', N'7.0.0-test');
153+
GO
154+
155+
COMMIT;
156+
GO
157+
126158
127159
""",
128160
Sql,
@@ -173,6 +205,32 @@ INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
173205
VALUES (N'00000000000003_Migration3', N'7.0.0-test');
174206
GO
175207
208+
CREATE PROCEDURE [dbo].[GotoReproduction]
209+
AS
210+
BEGIN
211+
DECLARE @Counter int;
212+
SET @Counter = 1;
213+
WHILE @Counter < 10
214+
BEGIN
215+
SELECT @Counter
216+
SET @Counter = @Counter + 1
217+
IF @Counter = 4 GOTO Branch_One --Jumps to the first branch.
218+
IF @Counter = 5 GOTO Branch_Two --This will never execute.
219+
END
220+
Branch_One:
221+
SELECT 'Jumping To Branch One.'
222+
GOTO Branch_Three; --This will prevent Branch_Two from executing.
223+
Branch_Two:
224+
SELECT 'Jumping To Branch Two.'
225+
Branch_Three:
226+
SELECT 'Jumping To Branch Three.'
227+
END;
228+
GO
229+
230+
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
231+
VALUES (N'00000000000004_Migration4', N'7.0.0-test');
232+
GO
233+
176234
177235
""",
178236
Sql,
@@ -333,6 +391,50 @@ INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
333391
COMMIT;
334392
GO
335393
394+
BEGIN TRANSACTION;
395+
GO
396+
397+
IF NOT EXISTS (
398+
SELECT * FROM [__EFMigrationsHistory]
399+
WHERE [MigrationId] = N'00000000000004_Migration4'
400+
)
401+
BEGIN
402+
CREATE PROCEDURE [dbo].[GotoReproduction]
403+
AS
404+
BEGIN
405+
DECLARE @Counter int;
406+
SET @Counter = 1;
407+
WHILE @Counter < 10
408+
BEGIN
409+
SELECT @Counter
410+
SET @Counter = @Counter + 1
411+
IF @Counter = 4 GOTO Branch_One --Jumps to the first branch.
412+
IF @Counter = 5 GOTO Branch_Two --This will never execute.
413+
END
414+
Branch_One:
415+
SELECT 'Jumping To Branch One.'
416+
GOTO Branch_Three; --This will prevent Branch_Two from executing.
417+
Branch_Two:
418+
SELECT 'Jumping To Branch Two.'
419+
Branch_Three:
420+
SELECT 'Jumping To Branch Three.'
421+
END;
422+
END;
423+
GO
424+
425+
IF NOT EXISTS (
426+
SELECT * FROM [__EFMigrationsHistory]
427+
WHERE [MigrationId] = N'00000000000004_Migration4'
428+
)
429+
BEGIN
430+
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
431+
VALUES (N'00000000000004_Migration4', N'7.0.0-test');
432+
END;
433+
GO
434+
435+
COMMIT;
436+
GO
437+
336438
337439
""",
338440
Sql,
@@ -425,6 +527,44 @@ INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
425527
END;
426528
GO
427529
530+
IF NOT EXISTS (
531+
SELECT * FROM [__EFMigrationsHistory]
532+
WHERE [MigrationId] = N'00000000000004_Migration4'
533+
)
534+
BEGIN
535+
CREATE PROCEDURE [dbo].[GotoReproduction]
536+
AS
537+
BEGIN
538+
DECLARE @Counter int;
539+
SET @Counter = 1;
540+
WHILE @Counter < 10
541+
BEGIN
542+
SELECT @Counter
543+
SET @Counter = @Counter + 1
544+
IF @Counter = 4 GOTO Branch_One --Jumps to the first branch.
545+
IF @Counter = 5 GOTO Branch_Two --This will never execute.
546+
END
547+
Branch_One:
548+
SELECT 'Jumping To Branch One.'
549+
GOTO Branch_Three; --This will prevent Branch_Two from executing.
550+
Branch_Two:
551+
SELECT 'Jumping To Branch Two.'
552+
Branch_Three:
553+
SELECT 'Jumping To Branch Three.'
554+
END;
555+
END;
556+
GO
557+
558+
IF NOT EXISTS (
559+
SELECT * FROM [__EFMigrationsHistory]
560+
WHERE [MigrationId] = N'00000000000004_Migration4'
561+
)
562+
BEGIN
563+
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
564+
VALUES (N'00000000000004_Migration4', N'7.0.0-test');
565+
END;
566+
GO
567+
428568
429569
""",
430570
Sql,

test/EFCore.Sqlite.FunctionalTests/Migrations/MigrationsInfrastructureSqliteTest.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ public override void Can_generate_up_scripts()
8888
8989
COMMIT;
9090
91+
BEGIN TRANSACTION;
92+
93+
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
94+
VALUES ('00000000000004_Migration4', '7.0.0-test');
95+
96+
COMMIT;
97+
9198
9299
""",
93100
Sql,
@@ -121,6 +128,9 @@ public override void Can_generate_up_scripts_noTransactions()
121128
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
122129
VALUES ('00000000000003_Migration3', '7.0.0-test');
123130
131+
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
132+
VALUES ('00000000000004_Migration4', '7.0.0-test');
133+
124134
125135
""",
126136
Sql,

0 commit comments

Comments
 (0)