Skip to content

Commit

Permalink
Space Sqlite columns with comments
Browse files Browse the repository at this point in the history
If comments are included, each column will be spaced.
Table comments are not preserved.

Fixes dotnet#16820
  • Loading branch information
skalpin committed Aug 16, 2019
1 parent 1c090a3 commit 2f53876
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit 2f53876

Please sign in to comment.