From 6b2465dcd36820fd4986117a582ba6133a243111 Mon Sep 17 00:00:00 2001 From: Andriy Svyryd Date: Wed, 9 Oct 2024 10:28:32 -0700 Subject: [PATCH] Don't remove quoted 'GO' lines Fixes #32826 --- .../SqlServerMigrationsSqlGenerator.cs | 32 +++++++++++++++++-- .../MigrationsInfrastructureSqlServerTest.cs | 16 ++-------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs b/src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs index a46d20ce58c..14fd173e8d7 100644 --- a/src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs +++ b/src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs @@ -1395,11 +1395,13 @@ 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]))) { @@ -1407,7 +1409,7 @@ protected override void Generate(SqlOperation operation, IModel? model, Migratio 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; @@ -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); } } diff --git a/test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsInfrastructureSqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsInfrastructureSqlServerTest.cs index f0fc3e5cccb..33e82d01424 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsInfrastructureSqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsInfrastructureSqlServerTest.cs @@ -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; @@ -18,12 +17,6 @@ public class MigrationsInfrastructureSqlServerTest( MigrationsInfrastructureSqlServerTest.MigrationsInfrastructureSqlServerFixture fixture) : MigrationsInfrastructureTestBase(fixture) { - public override void Can_apply_all_migrations() // Issue #32826 - => Assert.Throws(base.Can_apply_all_migrations); - - public override Task Can_apply_all_migrations_async() // Issue #32826 - => Assert.ThrowsAsync(base.Can_apply_all_migrations_async); - public override void Can_apply_range_of_migrations() { base.Can_apply_range_of_migrations(); @@ -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]) @@ -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