Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Control indenting in SQL generated #15314

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels error-prone to me. Let me think about it some more...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Identifiers can contain quotes. We're going to need to rely on stronger semantics than just counting quotes.

CREATE TABLE [Patient] (
    [Id] int NOT NULL PRIMARY KEY,
    [Can't eat] bit NOT NULL,
    [Can't sleep] bit NOT NULL
);

}
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