Skip to content

Commit

Permalink
Generate Sqlite create table comments
Browse files Browse the repository at this point in the history
- This only works with create table. Alter table commands do not
preserve comments.
- If comments are included, each column will be spaced.
- Table comments
- Column comments

Fixes #16820
  • Loading branch information
skalpin committed Aug 17, 2019
1 parent fb94063 commit da541b6
Show file tree
Hide file tree
Showing 2 changed files with 257 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,88 @@ protected override void Generate(
}
}

base.Generate(operation, model, builder, terminate);
if (string.IsNullOrEmpty(operation.Comment))
{
base.Generate(operation, model, builder, terminate);
}
else
{
builder
.Append("CREATE TABLE ")
.Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Name, operation.Schema))
.AppendLine(" (");

using (builder.Indent())
{
CreateComment(builder, operation.Comment);
builder.AppendLine();
CreateTableColumns(operation, model, builder);
CreateTableConstraints(operation, model, builder);
builder.AppendLine();
}

builder.Append(")");

if (terminate)
{
builder.AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator);
EndStatement(builder);
}
}
}

protected override void CreateTableColumns(
CreateTableOperation operation,
IModel model,
MigrationCommandListBuilder builder)
{
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(
CreateTableOperation operation,
IModel model,
MigrationCommandListBuilder builder)
{
for (var i = 0; i < operation.Columns.Count; i++)
{
var column = operation.Columns[i];

if (i > 0)
{
builder.AppendLine();
}

if (!string.IsNullOrEmpty(column.Comment))
{
CreateComment(builder, column.Comment);
}

ColumnDefinition(column, model, builder);

if (i != operation.Columns.Count - 1)
{
builder.AppendLine(",");
}
}
}

private void CreateComment(MigrationCommandListBuilder builder, string comment)
{
foreach (var line in comment.Split(Environment.NewLine).Select(l => $"-- {l}"))
{
builder.AppendLine(line);
}
}

/// <summary>
Expand Down
175 changes: 175 additions & 0 deletions test/EFCore.Sqlite.FunctionalTests/SqliteMigrationSqlGeneratorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,181 @@ public virtual void CreateTableOperation_old_autoincrement_annotation()
");
}

[ConditionalFact]
public virtual void CreateTableOperation_has_comment()
{
Generate(
new CreateTableOperation
{
Name = "People",
Columns =
{
new AddColumnOperation
{
Name = "Id",
Table = "People",
ClrType = typeof(int),
IsNullable = false,
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"
}
}
});

AssertSql(
@"CREATE TABLE ""People"" (
-- 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
);
");
}

[ConditionalFact]
public virtual void CreateTableOperation_has_multi_line_comment()
{
Generate(
new CreateTableOperation
{
Name = "People",
Columns =
{
new AddColumnOperation
{
Name = "Id",
Table = "People",
ClrType = typeof(int),
IsNullable = false,
Comment = @"This is a multi-line
comment.
More information can
be found in the docs."

},
}
});

AssertSql(
@"CREATE TABLE ""People"" (
-- This is a multi-line
-- comment.
-- More information can
-- be found in the docs.
""Id"" INTEGER NOT NULL
);
");
}

[ConditionalFact]
public virtual void CreateTableOperation_has_multi_line_table_comment()
{
Generate(
new CreateTableOperation
{
Name = "People",
Comment = @"Table level comment
that continues onto another line",
Columns =
{
new AddColumnOperation
{
Name = "Id",
Table = "People",
ClrType = typeof(int),
IsNullable = false,
Comment = "My Comment"
},
}
});

AssertSql(
@"CREATE TABLE ""People"" (
-- Table level comment
-- that continues onto another line
-- My Comment
""Id"" INTEGER NOT NULL
);
");
}

public SqliteMigrationSqlGeneratorTest()
: base(SqliteTestHelpers.Instance)
{
Expand Down

0 comments on commit da541b6

Please sign in to comment.