Skip to content

Commit

Permalink
[release/8.0] Stop stripping new lines from migration script literals (
Browse files Browse the repository at this point in the history
…#32788) (#32851)

* Stop stripping new lines from migration script literals (#32788)

* Stop stripping new lines from migration script literals

Fixes #32730

Will port to 8.0 for patch after merge.

* More tests for cases with GO

* Quirk
  • Loading branch information
ajcvickers authored Feb 7, 2024
1 parent 9d9fa7a commit 9b8b38f
Show file tree
Hide file tree
Showing 4 changed files with 447 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public class SqlServerMigrationsSqlGenerator : MigrationsSqlGenerator
private static readonly bool UseOldBehavior32457 =
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue32457", out var enabled32457) && enabled32457;

private static readonly bool UseOldBehavior32730 =
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue32730", out var enabled32730) && enabled32730;

private IReadOnlyList<MigrationOperation> _operations = null!;
private int _variableCounter;

Expand Down Expand Up @@ -1415,17 +1418,18 @@ protected override void Generate(SqlOperation operation, IModel? model, Migratio
var batchBuilder = new StringBuilder();
foreach (var line in preBatched)
{
if (string.IsNullOrWhiteSpace(line))
{
continue;
}

var trimmed = line.TrimStart();
if (trimmed.StartsWith("GO", StringComparison.OrdinalIgnoreCase)
&& (UseOldBehavior32457
|| trimmed.Length == 2
|| char.IsWhiteSpace(trimmed[2])))
{
if (UseOldBehavior32730
&& string.IsNullOrWhiteSpace(line))
{
continue;
}

var batch = batchBuilder.ToString();
batchBuilder.Clear();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,32 @@ public virtual void Can_apply_all_migrations()
x => Assert.Equal("00000000000001_Migration1", x.MigrationId),
x => Assert.Equal("00000000000002_Migration2", x.MigrationId),
x => Assert.Equal("00000000000003_Migration3", x.MigrationId),
x => Assert.Equal("00000000000004_Migration4", x.MigrationId));
x => Assert.Equal("00000000000004_Migration4", x.MigrationId),
x => Assert.Equal("00000000000005_Migration5", x.MigrationId),
x => Assert.Equal("00000000000006_Migration6", x.MigrationId),
x => Assert.Equal("00000000000007_Migration7", x.MigrationId));
}

[ConditionalFact]
public virtual void Can_apply_range_of_migrations()
{
using var db = Fixture.CreateContext();
db.Database.EnsureDeleted();

GiveMeSomeTime(db);

var migrator = db.GetService<IMigrator>();
migrator.Migrate("Migration6");

var history = db.GetService<IHistoryRepository>();
Assert.Collection(
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("00000000000004_Migration4", x.MigrationId),
x => Assert.Equal("00000000000005_Migration5", x.MigrationId),
x => Assert.Equal("00000000000006_Migration6", x.MigrationId));
}

[ConditionalFact]
Expand Down Expand Up @@ -100,9 +125,8 @@ public virtual void Can_revert_all_migrations()

GiveMeSomeTime(db);

db.Database.Migrate();

var migrator = db.GetService<IMigrator>();
migrator.Migrate("Migration5");
migrator.Migrate(Migration.InitialDatabase);

var history = db.GetService<IHistoryRepository>();
Expand All @@ -117,15 +141,17 @@ public virtual void Can_revert_one_migrations()

GiveMeSomeTime(db);

db.Database.Migrate();

var migrator = db.GetService<IMigrator>();
migrator.Migrate("Migration1");
migrator.Migrate("Migration5");
migrator.Migrate("Migration4");

var history = db.GetService<IHistoryRepository>();
Assert.Collection(
history.GetAppliedMigrations(),
x => Assert.Equal("00000000000001_Migration1", x.MigrationId));
x => Assert.Equal("00000000000001_Migration1", x.MigrationId),
x => Assert.Equal("00000000000002_Migration2", x.MigrationId),
x => Assert.Equal("00000000000003_Migration3", x.MigrationId),
x => Assert.Equal("00000000000004_Migration4", x.MigrationId));
}

[ConditionalFact]
Expand All @@ -144,7 +170,10 @@ 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("00000000000004_Migration4", x.MigrationId));
x => Assert.Equal("00000000000004_Migration4", x.MigrationId),
x => Assert.Equal("00000000000005_Migration5", x.MigrationId),
x => Assert.Equal("00000000000006_Migration6", x.MigrationId),
x => Assert.Equal("00000000000007_Migration7", x.MigrationId));
}

[ConditionalFact]
Expand Down Expand Up @@ -357,6 +386,7 @@ public MigrationsContext(DbContextOptions options)
public class Foo
{
public int Id { get; set; }
public string Description { get; set; }
}

[DbContext(typeof(MigrationsContext))]
Expand All @@ -370,7 +400,7 @@ protected override void Up(MigrationBuilder migrationBuilder)
migrationBuilder
.CreateTable(
name: "Table1",
columns: x => new { Id = x.Column<int>(), Foo = x.Column<int>() })
columns: x => new { Id = x.Column<int>(), Foo = x.Column<int>(), Description = x.Column<string>() })
.PrimaryKey(
name: "PK_Table1",
columns: x => x.Id);
Expand Down Expand Up @@ -452,4 +482,72 @@ protected override void Down(MigrationBuilder migrationBuilder)
{
}
}

[DbContext(typeof(MigrationsContext))]
[Migration("00000000000005_Migration5")]
private class Migration5 : Migration
{
public const string TestValue = """
Value With
Empty Lines
""";

protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql($"INSERT INTO Table1 (Id, Bar, Description) VALUES (-1, ' ', '{TestValue}')");
}

protected override void Down(MigrationBuilder migrationBuilder)
{

}
}

[DbContext(typeof(MigrationsContext))]
[Migration("00000000000006_Migration6")]
private class Migration6 : Migration
{
public const string TestValue = """
GO
Value With
Empty Lines
""";

protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql($"INSERT INTO Table1 (Id, Bar, Description) VALUES (-2, ' ', '{TestValue}')");
}

protected override void Down(MigrationBuilder migrationBuilder)
{

}
}

[DbContext(typeof(MigrationsContext))]
[Migration("00000000000007_Migration7")]
private class Migration7 : Migration
{
public const string TestValue = """
GO
Value With
GO
Empty Lines
GO
""";

protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql($"INSERT INTO Table1 (Id, Bar, Description) VALUES (-3, ' ', '{TestValue}')");
}

protected override void Down(MigrationBuilder migrationBuilder)
{

}
}
}
Loading

0 comments on commit 9b8b38f

Please sign in to comment.