Skip to content

Commit

Permalink
Don't remove quoted 'GO' lines (#34917)
Browse files Browse the repository at this point in the history
Fixes #32826
  • Loading branch information
AndriySvyryd authored Oct 16, 2024
1 parent cd28eae commit 1567ab3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
32 changes: 30 additions & 2 deletions src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1395,19 +1395,21 @@ protected override void Generate(SqlOperation operation, IModel? model, Migratio
.Replace("\\\r\n", "")
.Split(["\r\n", "\n"], StringSplitOptions.None);

var quoted = false;
var batchBuilder = new StringBuilder();
foreach (var line in preBatched)
{
var trimmed = line.TrimStart();
if (trimmed.StartsWith("GO", StringComparison.OrdinalIgnoreCase)
if (!quoted
&& trimmed.StartsWith("GO", StringComparison.OrdinalIgnoreCase)
&& (trimmed.Length == 2
|| char.IsWhiteSpace(trimmed[2])))
{
var batch = batchBuilder.ToString();
batchBuilder.Clear();

var count = trimmed.Length >= 4
&& int.TryParse(trimmed.Substring(3), out var specifiedCount)
&& int.TryParse(trimmed.AsSpan(3), out var specifiedCount)
? specifiedCount
: 1;

Expand All @@ -1418,6 +1420,32 @@ protected override void Generate(SqlOperation operation, IModel? model, Migratio
}
else
{
var commentStart = false;
foreach (var c in trimmed)
{
switch (c)
{
case '\'':
quoted = !quoted;
commentStart = false;
break;
case '-':
if (!quoted)
{
if (commentStart)
{
goto LineEnd;
}
commentStart = true;
}
break;
default:
commentStart = false;
break;
}
}

LineEnd:
batchBuilder.AppendLine(line);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#nullable disable

using Identity30.Data;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore.Diagnostics.Internal;
using Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal;
using Microsoft.EntityFrameworkCore.TestModels.AspNetIdentity;
Expand All @@ -18,12 +17,6 @@ public class MigrationsInfrastructureSqlServerTest(
MigrationsInfrastructureSqlServerTest.MigrationsInfrastructureSqlServerFixture fixture)
: MigrationsInfrastructureTestBase<MigrationsInfrastructureSqlServerTest.MigrationsInfrastructureSqlServerFixture>(fixture)
{
public override void Can_apply_all_migrations() // Issue #32826
=> Assert.Throws<SqlException>(base.Can_apply_all_migrations);

public override Task Can_apply_all_migrations_async() // Issue #32826
=> Assert.ThrowsAsync<SqlException>(base.Can_apply_all_migrations_async);

public override void Can_apply_range_of_migrations()
{
base.Can_apply_range_of_migrations();
Expand Down Expand Up @@ -174,13 +167,13 @@ INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'00000000000006_Migration6', N'7.0.0-test');

INSERT INTO Table1 (Id, Bar, Description) VALUES (-3, 5, '--Start

GO
Value With


GO

Empty Lines;

GO
')

INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
Expand Down Expand Up @@ -322,15 +315,12 @@ INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])

INSERT INTO Table1 (Id, Bar, Description) VALUES (-3, 5, '--Start
GO

Value With

GO


Empty Lines;
GO

')
GO

Expand Down

0 comments on commit 1567ab3

Please sign in to comment.