Skip to content

Commit

Permalink
Control indenting in SQL generated
Browse files Browse the repository at this point in the history
Fixes aspnet#15256
  • Loading branch information
krzycho1024 committed Apr 10, 2019
1 parent 948e683 commit ab56c43
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/EFCore.Relational/Migrations/Internal/Migrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public virtual string GenerateScript(
builder.AppendLine(_historyRepository.GetBeginIfExistsScript(migration.GetId()));
using (builder.Indent())
{
builder.AppendLines(command.CommandText);
builder.AppendLines(command.CommandText, isSql: true);
}

builder.AppendLine(_historyRepository.GetEndIfScript());
Expand All @@ -338,7 +338,7 @@ public virtual string GenerateScript(
builder.AppendLine(_historyRepository.GetBeginIfNotExistsScript(migration.GetId()));
using (builder.Indent())
{
builder.AppendLines(command.CommandText);
builder.AppendLines(command.CommandText, isSql: true);
}

builder.AppendLine(_historyRepository.GetEndIfScript());
Expand Down
9 changes: 8 additions & 1 deletion src/EFCore/Internal/IndentedStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,12 @@ public virtual IndentedStringBuilder AppendLine([NotNull] object o)
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public virtual IndentedStringBuilder AppendLines([NotNull] object o, bool skipFinalNewline = false)
public virtual IndentedStringBuilder AppendLines([NotNull] object o, bool skipFinalNewline = false, bool isSql = false)
{
using (var reader = new StringReader(o.ToString()))
{
var first = true;
var quoteCount = 0;
string line;
while ((line = reader.ReadLine()) != null)
{
Expand All @@ -116,8 +117,14 @@ public virtual IndentedStringBuilder AppendLines([NotNull] object o, bool skipFi

if (line.Length != 0)
{
if (isSql)
{
_indentPending = quoteCount % 2 != 1;
}
Append(line);
}

quoteCount += line.Split('\'').Length - 1;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.TestUtilities;
Expand Down Expand Up @@ -101,5 +102,23 @@ protected override void Down(MigrationBuilder migrationBuilder)
{
}
}

[DbContext(typeof(MigrationsContext))]
[Migration("00000000000004_Migration4")]
private class Migration4 : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
=> migrationBuilder.CreateTable(
"Table2",
x => new
{
Id = x.Column<int>(),
Baz = x.Column<string>(defaultValue: "first" + Environment.NewLine + "second" + Environment.NewLine + "third"),
Quz = x.Column<string>(defaultValue: "1" + Environment.NewLine + "2")
});

protected override void Down(MigrationBuilder migrationBuilder)
=> migrationBuilder.DropTable("Table2");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public virtual void Can_apply_all_migrations()
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("00000000000003_Migration3", x.MigrationId),
x => Assert.Equal("00000000000004_Migration4", x.MigrationId));
}
}

Expand Down Expand Up @@ -163,7 +164,8 @@ public virtual async Task Can_apply_all_migrations_async()
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("00000000000003_Migration3", x.MigrationId),
x => Assert.Equal("00000000000004_Migration4", x.MigrationId));
}
}

Expand Down
38 changes: 38 additions & 0 deletions test/EFCore.SqlServer.FunctionalTests/MigrationsSqlServerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
GO
CREATE TABLE [Table2] (
[Id] int NOT NULL,
[Baz] nvarchar(max) NOT NULL DEFAULT N'first
second
third',
[Quz] nvarchar(max) NOT NULL DEFAULT N'1
2'
);
GO
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'00000000000004_Migration4', N'7.0.0-test');
GO
",
Sql,
ignoreLineEndingDifferences: true);
Expand Down Expand Up @@ -232,6 +248,28 @@ INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'00000000000004_Migration4')
BEGIN
CREATE TABLE [Table2] (
[Id] int NOT NULL,
[Baz] nvarchar(max) NOT NULL DEFAULT N'first
second
third',
[Quz] nvarchar(max) NOT NULL DEFAULT N'1
2'
);
END;
GO
IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N'00000000000004_Migration4')
BEGIN
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'00000000000004_Migration4', N'7.0.0-test');
END;
GO
",
Sql,
ignoreLineEndingDifferences: true);
Expand Down
12 changes: 12 additions & 0 deletions test/EFCore.Sqlite.FunctionalTests/MigrationsSqliteTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ public override void Can_generate_up_scripts()
INSERT INTO ""__EFMigrationsHistory"" (""MigrationId"", ""ProductVersion"")
VALUES ('00000000000003_Migration3', '7.0.0-test');
CREATE TABLE ""Table2"" (
""Id"" INTEGER NOT NULL,
""Baz"" TEXT NOT NULL DEFAULT 'first
second
third',
""Quz"" TEXT NOT NULL DEFAULT '1
2'
);
INSERT INTO ""__EFMigrationsHistory"" (""MigrationId"", ""ProductVersion"")
VALUES ('00000000000004_Migration4', '7.0.0-test');
",
Sql,
ignoreLineEndingDifferences: true);
Expand Down

0 comments on commit ab56c43

Please sign in to comment.