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

[5.0.x] Fix default constraint logic in SQL Server migrations (#24274) #24305

Merged
merged 2 commits into from Mar 10, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs
Expand Up @@ -38,6 +38,9 @@ public class SqlServerMigrationsSqlGenerator : MigrationsSqlGenerator
private IReadOnlyList<MigrationOperation> _operations;
private int _variableCounter;

private static readonly bool _useOldAlterColumnDefaultValueLogic =
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue24272", out var enabled) && enabled;

/// <summary>
/// Creates a new <see cref="SqlServerMigrationsSqlGenerator" /> instance.
/// </summary>
Expand Down Expand Up @@ -339,11 +342,14 @@ protected override void Generate(AddCheckConstraintOperation operation, IModel m
|| operation.Collation != operation.OldColumn.Collation
|| HasDifferences(operation.GetAnnotations(), operation.OldColumn.GetAnnotations());

var (oldDefaultValue, oldDefaultValueSql) = (operation.OldColumn.DefaultValue, operation.OldColumn.DefaultValueSql);

if (alterStatementNeeded
|| !Equals(operation.DefaultValue, operation.OldColumn.DefaultValue)
|| operation.DefaultValueSql != operation.OldColumn.DefaultValueSql)
|| !Equals(operation.DefaultValue, oldDefaultValue)
|| operation.DefaultValueSql != oldDefaultValueSql)
{
DropDefaultConstraint(operation.Schema, operation.Table, operation.Name, builder);
(oldDefaultValue, oldDefaultValueSql) = (null, null);
}

if (alterStatementNeeded)
Expand Down Expand Up @@ -388,8 +394,11 @@ protected override void Generate(AddCheckConstraintOperation operation, IModel m
builder.AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator);
}

if (operation.DefaultValue != null
|| operation.DefaultValueSql != null)
var addDefaultValue = _useOldAlterColumnDefaultValueLogic
? operation.DefaultValue != null || operation.DefaultValueSql != null
: !Equals(operation.DefaultValue, oldDefaultValue) || operation.DefaultValueSql != oldDefaultValueSql;

if (addDefaultValue)
{
builder
.Append("ALTER TABLE ")
Expand Down
23 changes: 23 additions & 0 deletions test/EFCore.SqlServer.FunctionalTests/MigrationsSqlServerTest.cs
Expand Up @@ -945,6 +945,29 @@ public virtual async Task Alter_column_change_default()
ALTER TABLE [People] ADD DEFAULT N'Doe' FOR [Name];");
}

[ConditionalFact]
public virtual async Task Alter_column_change_comment_with_default()
{
await Test(
builder => builder.Entity("People").Property<string>("Name").HasDefaultValue("Doe"),
builder => { },
builder => builder.Entity("People").Property<string>("Name")
.HasComment("Some comment"),
model =>
{
var nameColumn = Assert.Single(Assert.Single(model.Tables).Columns);
Assert.Equal("(N'Doe')", nameColumn.DefaultValueSql);
Assert.Equal("Some comment", nameColumn.Comment);
});

AssertSql(
@"DECLARE @defaultSchema AS sysname;
SET @defaultSchema = SCHEMA_NAME();
DECLARE @description AS sql_variant;
SET @description = N'Some comment';
EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE', N'People', 'COLUMN', N'Name';");
}

public override async Task Drop_column()
{
await base.Drop_column();
Expand Down