Skip to content

Commit bd74ac4

Browse files
committed
Stop stripping new lines from migration script literals
Fixes #32730 Will port to 8.0 for patch after merge.
1 parent 08b34ff commit bd74ac4

File tree

4 files changed

+125
-10
lines changed

4 files changed

+125
-10
lines changed

src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,11 +1377,6 @@ protected override void Generate(SqlOperation operation, IModel? model, Migratio
13771377
var batchBuilder = new StringBuilder();
13781378
foreach (var line in preBatched)
13791379
{
1380-
if (string.IsNullOrWhiteSpace(line))
1381-
{
1382-
continue;
1383-
}
1384-
13851380
var trimmed = line.TrimStart();
13861381
if (trimmed.StartsWith("GO", StringComparison.OrdinalIgnoreCase)
13871382
&& (trimmed.Length == 2

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ public virtual void Can_apply_all_migrations()
7272
x => Assert.Equal("00000000000001_Migration1", x.MigrationId),
7373
x => Assert.Equal("00000000000002_Migration2", x.MigrationId),
7474
x => Assert.Equal("00000000000003_Migration3", x.MigrationId),
75-
x => Assert.Equal("00000000000004_Migration4", x.MigrationId));
75+
x => Assert.Equal("00000000000004_Migration4", x.MigrationId),
76+
x => Assert.Equal("00000000000005_Migration5", x.MigrationId));
7677
}
7778

7879
[ConditionalFact]
@@ -144,7 +145,8 @@ await history.GetAppliedMigrationsAsync(),
144145
x => Assert.Equal("00000000000001_Migration1", x.MigrationId),
145146
x => Assert.Equal("00000000000002_Migration2", x.MigrationId),
146147
x => Assert.Equal("00000000000003_Migration3", x.MigrationId),
147-
x => Assert.Equal("00000000000004_Migration4", x.MigrationId));
148+
x => Assert.Equal("00000000000004_Migration4", x.MigrationId),
149+
x => Assert.Equal("00000000000005_Migration5", x.MigrationId));
148150
}
149151

150152
[ConditionalFact]
@@ -346,6 +348,7 @@ public class MigrationsContext(DbContextOptions options) : PoolableDbContext(opt
346348
public class Foo
347349
{
348350
public int Id { get; set; }
351+
public string Description { get; set; }
349352
}
350353

351354
[DbContext(typeof(MigrationsContext))]
@@ -359,7 +362,7 @@ protected override void Up(MigrationBuilder migrationBuilder)
359362
migrationBuilder
360363
.CreateTable(
361364
name: "Table1",
362-
columns: x => new { Id = x.Column<int>(), Foo = x.Column<int>() })
365+
columns: x => new { Id = x.Column<int>(), Foo = x.Column<int>(), Description = x.Column<string>() })
363366
.PrimaryKey(
364367
name: "PK_Table1",
365368
columns: x => x.Id);
@@ -441,4 +444,25 @@ protected override void Down(MigrationBuilder migrationBuilder)
441444
{
442445
}
443446
}
447+
448+
[DbContext(typeof(MigrationsContext))]
449+
[Migration("00000000000005_Migration5")]
450+
private class Migration5 : Migration
451+
{
452+
public const string TestValue = """
453+
Value With
454+
455+
Empty Lines
456+
""";
457+
458+
protected override void Up(MigrationBuilder migrationBuilder)
459+
{
460+
migrationBuilder.Sql($"INSERT INTO Table1 (Id, Bar, Description) VALUES (-1, ' ', '{TestValue}')");
461+
}
462+
463+
protected override void Down(MigrationBuilder migrationBuilder)
464+
{
465+
466+
}
467+
}
444468
}

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId])
7878
CREATE TABLE [Table1] (
7979
[Id] int NOT NULL,
8080
[Foo] int NOT NULL,
81+
[Description] nvarchar(max) NOT NULL,
8182
CONSTRAINT [PK_Table1] PRIMARY KEY ([Id])
8283
);
8384
GO
@@ -150,6 +151,21 @@ INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
150151
COMMIT;
151152
GO
152153
154+
BEGIN TRANSACTION;
155+
GO
156+
157+
INSERT INTO Table1 (Id, Bar, Description) VALUES (-1, ' ', 'Value With
158+
159+
Empty Lines')
160+
GO
161+
162+
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
163+
VALUES (N'00000000000005_Migration5', N'7.0.0-test');
164+
GO
165+
166+
COMMIT;
167+
GO
168+
153169
154170
""",
155171
Sql,
@@ -175,6 +191,7 @@ CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId])
175191
CREATE TABLE [Table1] (
176192
[Id] int NOT NULL,
177193
[Foo] int NOT NULL,
194+
[Description] nvarchar(max) NOT NULL,
178195
CONSTRAINT [PK_Table1] PRIMARY KEY ([Id])
179196
);
180197
GO
@@ -226,6 +243,15 @@ INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
226243
VALUES (N'00000000000004_Migration4', N'7.0.0-test');
227244
GO
228245
246+
INSERT INTO Table1 (Id, Bar, Description) VALUES (-1, ' ', 'Value With
247+
248+
Empty Lines')
249+
GO
250+
251+
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
252+
VALUES (N'00000000000005_Migration5', N'7.0.0-test');
253+
GO
254+
229255
230256
""",
231257
Sql,
@@ -309,6 +335,7 @@ IF NOT EXISTS (
309335
CREATE TABLE [Table1] (
310336
[Id] int NOT NULL,
311337
[Foo] int NOT NULL,
338+
[Description] nvarchar(max) NOT NULL,
312339
CONSTRAINT [PK_Table1] PRIMARY KEY ([Id])
313340
);
314341
END;
@@ -430,6 +457,33 @@ INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
430457
COMMIT;
431458
GO
432459
460+
BEGIN TRANSACTION;
461+
GO
462+
463+
IF NOT EXISTS (
464+
SELECT * FROM [__EFMigrationsHistory]
465+
WHERE [MigrationId] = N'00000000000005_Migration5'
466+
)
467+
BEGIN
468+
INSERT INTO Table1 (Id, Bar, Description) VALUES (-1, ' ', 'Value With
469+
470+
Empty Lines')
471+
END;
472+
GO
473+
474+
IF NOT EXISTS (
475+
SELECT * FROM [__EFMigrationsHistory]
476+
WHERE [MigrationId] = N'00000000000005_Migration5'
477+
)
478+
BEGIN
479+
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
480+
VALUES (N'00000000000005_Migration5', N'7.0.0-test');
481+
END;
482+
GO
483+
484+
COMMIT;
485+
GO
486+
433487
434488
""",
435489
Sql,
@@ -460,6 +514,7 @@ IF NOT EXISTS (
460514
CREATE TABLE [Table1] (
461515
[Id] int NOT NULL,
462516
[Foo] int NOT NULL,
517+
[Description] nvarchar(max) NOT NULL,
463518
CONSTRAINT [PK_Table1] PRIMARY KEY ([Id])
464519
);
465520
END;
@@ -560,6 +615,27 @@ INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
560615
END;
561616
GO
562617
618+
IF NOT EXISTS (
619+
SELECT * FROM [__EFMigrationsHistory]
620+
WHERE [MigrationId] = N'00000000000005_Migration5'
621+
)
622+
BEGIN
623+
INSERT INTO Table1 (Id, Bar, Description) VALUES (-1, ' ', 'Value With
624+
625+
Empty Lines')
626+
END;
627+
GO
628+
629+
IF NOT EXISTS (
630+
SELECT * FROM [__EFMigrationsHistory]
631+
WHERE [MigrationId] = N'00000000000005_Migration5'
632+
)
633+
BEGIN
634+
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
635+
VALUES (N'00000000000005_Migration5', N'7.0.0-test');
636+
END;
637+
GO
638+
563639
564640
""",
565641
Sql,

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public override void Can_generate_up_scripts()
5959
6060
CREATE TABLE "Table1" (
6161
"Id" INTEGER NOT NULL CONSTRAINT "PK_Table1" PRIMARY KEY,
62-
"Foo" INTEGER NOT NULL
62+
"Foo" INTEGER NOT NULL,
63+
"Description" TEXT NOT NULL
6364
);
6465
6566
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
@@ -90,6 +91,17 @@ public override void Can_generate_up_scripts()
9091
9192
COMMIT;
9293
94+
BEGIN TRANSACTION;
95+
96+
INSERT INTO Table1 (Id, Bar, Description) VALUES (-1, ' ', 'Value With
97+
98+
Empty Lines')
99+
100+
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
101+
VALUES ('00000000000005_Migration5', '7.0.0-test');
102+
103+
COMMIT;
104+
93105
94106
""",
95107
Sql,
@@ -109,7 +121,8 @@ public override void Can_generate_up_scripts_noTransactions()
109121
110122
CREATE TABLE "Table1" (
111123
"Id" INTEGER NOT NULL CONSTRAINT "PK_Table1" PRIMARY KEY,
112-
"Foo" INTEGER NOT NULL
124+
"Foo" INTEGER NOT NULL,
125+
"Description" TEXT NOT NULL
113126
);
114127
115128
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
@@ -126,6 +139,13 @@ public override void Can_generate_up_scripts_noTransactions()
126139
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
127140
VALUES ('00000000000004_Migration4', '7.0.0-test');
128141
142+
INSERT INTO Table1 (Id, Bar, Description) VALUES (-1, ' ', 'Value With
143+
144+
Empty Lines')
145+
146+
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
147+
VALUES ('00000000000005_Migration5', '7.0.0-test');
148+
129149
130150
""",
131151
Sql,

0 commit comments

Comments
 (0)