Skip to content

Commit

Permalink
[5.0.x] Fix default constraint logic in SQL Server migrations (#24274) (
Browse files Browse the repository at this point in the history
#24305)

Fixes #24272

(cherry picked from commit 2eabf1f)
  • Loading branch information
roji committed Mar 10, 2021
1 parent fbe40a1 commit 09ec021
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
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

0 comments on commit 09ec021

Please sign in to comment.