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

MigrationsSqlGenerator: DefaultValue formats default value properly #16909

Merged
merged 2 commits into from
Aug 13, 2019
Merged
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
13 changes: 10 additions & 3 deletions src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1141,14 +1141,15 @@ protected virtual void ColumnDefinition(
return;
}

var columnType = operation.ColumnType ?? GetColumnType(schema, table, name, operation, model);
builder
.Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(name))
.Append(" ")
.Append(operation.ColumnType ?? GetColumnType(schema, table, name, operation, model));
.Append(columnType);

builder.Append(operation.IsNullable ? " NULL" : " NOT NULL");

DefaultValue(operation.DefaultValue, operation.DefaultValueSql, builder);
DefaultValue(operation.DefaultValue, operation.DefaultValueSql, columnType, builder);
}

/// <summary>
Expand Down Expand Up @@ -1223,10 +1224,12 @@ protected virtual string GetColumnType(
/// </summary>
/// <param name="defaultValue"> The default value for the column. </param>
/// <param name="defaultValueSql"> The SQL expression to use for the column's default constraint. </param>
/// <param name="columnType"> Store/database type of the column. </param>
/// <param name="builder"> The command builder to use to add the SQL fragment. </param>
protected virtual void DefaultValue(
[CanBeNull] object defaultValue,
[CanBeNull] string defaultValueSql,
[CanBeNull] string columnType,
[NotNull] MigrationCommandListBuilder builder)
{
Check.NotNull(builder, nameof(builder));
Expand All @@ -1240,7 +1243,11 @@ protected virtual void DefaultValue(
}
else if (defaultValue != null)
{
var typeMapping = Dependencies.TypeMappingSource.GetMappingForValue(defaultValue);
var typeMapping = columnType != null ? Dependencies.TypeMappingSource.FindMapping(defaultValue.GetType(), columnType) : null;
if (typeMapping == null)
{
typeMapping = Dependencies.TypeMappingSource.GetMappingForValue(defaultValue);
}

builder
.Append(" DEFAULT ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ protected override void Generate(
.Append("ALTER TABLE ")
.Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Table, operation.Schema))
.Append(" ADD");
DefaultValue(operation.DefaultValue, operation.DefaultValueSql, builder);
DefaultValue(operation.DefaultValue, operation.DefaultValueSql, operation.ColumnType, builder);
builder
.Append(" FOR ")
.Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Name))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,44 @@ public override void AddColumnOperation_with_maxLength()
");
}

[ConditionalFact]
public virtual void AddColumnOperation_datetime_with_defaultValue()
{
Generate(
new AddColumnOperation
{
Table = "People",
Schema = "dbo",
Name = "Birthday",
ClrType = typeof(DateTime),
ColumnType = "datetime",
IsNullable = false,
DefaultValue = new DateTime(2019, 1, 1)
});

AssertSql(@"ALTER TABLE [dbo].[People] ADD [Birthday] datetime NOT NULL DEFAULT '2019-01-01T00:00:00.000';
");
}

[ConditionalFact]
public virtual void AddColumnOperation_smalldatetime_with_defaultValue()
{
Generate(
new AddColumnOperation
{
Table = "People",
Schema = "dbo",
Name = "Birthday",
ClrType = typeof(DateTime),
ColumnType = "smalldatetime",
IsNullable = false,
DefaultValue = new DateTime(2019, 1, 1)
});

AssertSql(@"ALTER TABLE [dbo].[People] ADD [Birthday] smalldatetime NOT NULL DEFAULT '2019-01-01T00:00:00';
");
}

public override void AddColumnOperation_with_maxLength_overridden()
{
base.AddColumnOperation_with_maxLength_overridden();
Expand Down