Skip to content

Commit

Permalink
Correct ordering of check constraint creation
Browse files Browse the repository at this point in the history
Fixes #21037
  • Loading branch information
roji committed May 25, 2020
1 parent 7641cc3 commit 3bc4efb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ public class MigrationsModelDiffer : IMigrationsModelDiffer

private static readonly Type[] _columnOperationTypes = { typeof(AddColumnOperation), typeof(AlterColumnOperation) };

private static readonly Type[] _constraintOperationTypes = { typeof(AddForeignKeyOperation), typeof(CreateIndexOperation) };
private static readonly Type[] _constraintOperationTypes =
{
typeof(AddForeignKeyOperation),
typeof(CreateIndexOperation),
typeof(CreateCheckConstraintOperation)
};

private IUpdateAdapter _sourceUpdateAdapter;
private IUpdateAdapter _targetUpdateAdapter;
Expand Down Expand Up @@ -212,10 +217,6 @@ protected virtual IReadOnlyList<MigrationOperation> Sort(
{
createSequenceOperations.Add(operation);
}
else if (type == typeof(CreateCheckConstraintOperation))
{
createCheckConstraintOperations.Add(operation);
}
else if (type == typeof(CreateTableOperation))
{
createTableOperations.Add((CreateTableOperation)operation);
Expand Down
16 changes: 16 additions & 0 deletions test/EFCore.Relational.Specification.Tests/MigrationsTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,22 @@ public virtual Task Add_column_shared()
// Assert.Equal("nvarchar(30)", column.StoreType);
});

[ConditionalFact]
public virtual Task Add_column_with_check_constraint()
=> Test(
builder => builder.Entity("People").Property<int>("Id"),
builder => { },
builder => builder.Entity(
"People", e =>
{
e.Property<int>("DriverLicense");
e.HasCheckConstraint("CK_Foo", $"{DelimitIdentifier("DriverLicense")} > 0");
}),
model =>
{
// TODO: no scaffolding support for check constraints, https://github.com/aspnet/EntityFrameworkCore/issues/15408
});

[ConditionalFact]
public virtual Task Alter_column_change_type()
=> Test(
Expand Down
12 changes: 11 additions & 1 deletion test/EFCore.SqlServer.FunctionalTests/MigrationsSqlServerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public MigrationsSqlServerTest(MigrationsSqlServerFixture fixture, ITestOutputHe
: base(fixture)
{
Fixture.TestSqlLoggerFactory.Clear();
Fixture.TestSqlLoggerFactory.SetTestOutputHelper(testOutputHelper);
// Fixture.TestSqlLoggerFactory.SetTestOutputHelper(testOutputHelper);
}

public override async Task Create_table()
Expand Down Expand Up @@ -414,6 +414,16 @@ public override async Task Add_column_shared()
@"ALTER TABLE [Base] ADD [Foo] nvarchar(max) NULL;");
}

public override async Task Add_column_with_check_constraint()
{
await base.Add_column_with_check_constraint();

AssertSql(
@"ALTER TABLE [People] ADD [DriverLicense] int NOT NULL DEFAULT 0;",
//
@"ALTER TABLE [People] ADD CONSTRAINT [CK_Foo] CHECK ([DriverLicense] > 0);");
}

[ConditionalFact]
public virtual async Task Add_column_identity()
{
Expand Down

0 comments on commit 3bc4efb

Please sign in to comment.