From 2f5387640d8b6d2147455460c00ea5ef76dce5f7 Mon Sep 17 00:00:00 2001 From: Stephen Kalpin Date: Fri, 16 Aug 2019 12:25:53 -0500 Subject: [PATCH] Space Sqlite columns with comments If comments are included, each column will be spaced. Table comments are not preserved. Fixes #16820 --- .../SqliteMigrationsSqlGenerator.cs | 22 ++++- .../SqliteMigrationSqlGeneratorTest.cs | 86 ++++++++++++++++++- 2 files changed, 103 insertions(+), 5 deletions(-) diff --git a/src/EFCore.Sqlite.Core/Migrations/SqliteMigrationsSqlGenerator.cs b/src/EFCore.Sqlite.Core/Migrations/SqliteMigrationsSqlGenerator.cs index 1d5616aa019..da567dbbbea 100644 --- a/src/EFCore.Sqlite.Core/Migrations/SqliteMigrationsSqlGenerator.cs +++ b/src/EFCore.Sqlite.Core/Migrations/SqliteMigrationsSqlGenerator.cs @@ -356,11 +356,31 @@ protected override void CreateTableColumns( Check.NotNull(operation, nameof(operation)); Check.NotNull(builder, nameof(builder)); + if (!operation.Columns.Any(c => !string.IsNullOrEmpty(c.Comment))) + { + base.CreateTableColumns(operation, model, builder); + } + else + { + CreateTableColumnsWithComments(operation, model, builder); + } + } + + private void CreateTableColumnsWithComments( + [NotNull] CreateTableOperation operation, + [CanBeNull] IModel model, + [NotNull] MigrationCommandListBuilder builder) + { for (var i = 0; i < operation.Columns.Count; i++) { var column = operation.Columns[i]; - if(!string.IsNullOrEmpty(column.Comment)) + if (i > 0) + { + builder.AppendLine(); + } + + if (!string.IsNullOrEmpty(column.Comment)) { builder.AppendLine($"-- {column.Comment}"); } diff --git a/test/EFCore.Sqlite.FunctionalTests/SqliteMigrationSqlGeneratorTest.cs b/test/EFCore.Sqlite.FunctionalTests/SqliteMigrationSqlGeneratorTest.cs index 59ba60e91b3..029b6f84a40 100644 --- a/test/EFCore.Sqlite.FunctionalTests/SqliteMigrationSqlGeneratorTest.cs +++ b/test/EFCore.Sqlite.FunctionalTests/SqliteMigrationSqlGeneratorTest.cs @@ -521,19 +521,97 @@ public virtual void CreateTableOperation_has_comment() Table = "People", ClrType = typeof(int), IsNullable = false, - Comment = "My Comment" + Comment = "The ID" + }, + new AddColumnOperation + { + Name = "UncommentedColumn1", + Table = "People", + ClrType = typeof(string), + IsNullable = false + }, + new AddColumnOperation + { + Name = "UncommentedColumn2", + Table = "People", + ClrType = typeof(string), + IsNullable = false + }, + new AddColumnOperation + { + Name = "Name", + Table = "People", + ClrType = typeof(string), + IsNullable = false, + Comment = "The Name" } } }); - var sql = Sql; + AssertSql( @"CREATE TABLE ""People"" ( - -- My Comment - ""Id"" INTEGER NOT NULL + -- The ID + ""Id"" INTEGER NOT NULL, + + ""UncommentedColumn1"" TEXT NOT NULL, + + ""UncommentedColumn2"" TEXT NOT NULL, + + -- The Name + ""Name"" TEXT NOT NULL ); "); } + [ConditionalFact] + public virtual void CreateTableOperation_no_comments() + { + Generate( + new CreateTableOperation + { + Name = "People", + Columns = + { + new AddColumnOperation + { + Name = "Id", + Table = "People", + ClrType = typeof(int), + IsNullable = false, + }, + new AddColumnOperation + { + Name = "UncommentedColumn1", + Table = "People", + ClrType = typeof(string), + IsNullable = false + }, + new AddColumnOperation + { + Name = "UncommentedColumn2", + Table = "People", + ClrType = typeof(string), + IsNullable = false + }, + new AddColumnOperation + { + Name = "UncommentedName", + Table = "People", + ClrType = typeof(string), + IsNullable = false, + } + } + }); + + AssertSql( + @"CREATE TABLE ""People"" ( + ""Id"" INTEGER NOT NULL, + ""UncommentedColumn1"" TEXT NOT NULL, + ""UncommentedColumn2"" TEXT NOT NULL, + ""UncommentedName"" TEXT NOT NULL +); +"); + } public SqliteMigrationSqlGeneratorTest() : base(SqliteTestHelpers.Instance) {