From ab56c43b818169afa51440253badccca0d882ce8 Mon Sep 17 00:00:00 2001 From: Krzysztof Myhan Date: Wed, 10 Apr 2019 23:40:12 +0200 Subject: [PATCH] Control indenting in SQL generated Fixes aspnet#15256 --- .../Migrations/Internal/Migrator.cs | 4 +- src/EFCore/Internal/IndentedStringBuilder.cs | 9 ++++- .../MigrationsFixtureBase.cs | 19 ++++++++++ .../MigrationsTestBase.cs | 6 ++- .../MigrationsSqlServerTest.cs | 38 +++++++++++++++++++ .../MigrationsSqliteTest.cs | 12 ++++++ 6 files changed, 83 insertions(+), 5 deletions(-) diff --git a/src/EFCore.Relational/Migrations/Internal/Migrator.cs b/src/EFCore.Relational/Migrations/Internal/Migrator.cs index 1c9134b70bf..775b726f84f 100644 --- a/src/EFCore.Relational/Migrations/Internal/Migrator.cs +++ b/src/EFCore.Relational/Migrations/Internal/Migrator.cs @@ -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()); @@ -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()); diff --git a/src/EFCore/Internal/IndentedStringBuilder.cs b/src/EFCore/Internal/IndentedStringBuilder.cs index abc9fc47cd6..1da9f151b8b 100644 --- a/src/EFCore/Internal/IndentedStringBuilder.cs +++ b/src/EFCore/Internal/IndentedStringBuilder.cs @@ -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. /// - 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) { @@ -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; } } diff --git a/test/EFCore.Relational.Specification.Tests/MigrationsFixtureBase.cs b/test/EFCore.Relational.Specification.Tests/MigrationsFixtureBase.cs index 15b252d340f..0688a45e2a6 100644 --- a/test/EFCore.Relational.Specification.Tests/MigrationsFixtureBase.cs +++ b/test/EFCore.Relational.Specification.Tests/MigrationsFixtureBase.cs @@ -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; @@ -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(), + Baz = x.Column(defaultValue: "first" + Environment.NewLine + "second" + Environment.NewLine + "third"), + Quz = x.Column(defaultValue: "1" + Environment.NewLine + "2") + }); + + protected override void Down(MigrationBuilder migrationBuilder) + => migrationBuilder.DropTable("Table2"); + } } } diff --git a/test/EFCore.Relational.Specification.Tests/MigrationsTestBase.cs b/test/EFCore.Relational.Specification.Tests/MigrationsTestBase.cs index 5940932bb9f..27506cc3b15 100644 --- a/test/EFCore.Relational.Specification.Tests/MigrationsTestBase.cs +++ b/test/EFCore.Relational.Specification.Tests/MigrationsTestBase.cs @@ -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)); } } @@ -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)); } } diff --git a/test/EFCore.SqlServer.FunctionalTests/MigrationsSqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/MigrationsSqlServerTest.cs index 0dbd9fb39f3..9495754d06e 100644 --- a/test/EFCore.SqlServer.FunctionalTests/MigrationsSqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/MigrationsSqlServerTest.cs @@ -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); @@ -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); diff --git a/test/EFCore.Sqlite.FunctionalTests/MigrationsSqliteTest.cs b/test/EFCore.Sqlite.FunctionalTests/MigrationsSqliteTest.cs index 3e85ec83c5b..5198fbd97e6 100644 --- a/test/EFCore.Sqlite.FunctionalTests/MigrationsSqliteTest.cs +++ b/test/EFCore.Sqlite.FunctionalTests/MigrationsSqliteTest.cs @@ -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);