Skip to content

Commit

Permalink
Correct ordering of check constraint creation (#21040)
Browse files Browse the repository at this point in the history
Fixes #21037
  • Loading branch information
roji authored May 27, 2020
1 parent 3448ae1 commit f244396
Show file tree
Hide file tree
Showing 4 changed files with 37 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
4 changes: 4 additions & 0 deletions test/EFCore.Sqlite.FunctionalTests/MigrationsSqliteTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@ await Test(
@"ALTER TABLE ""People"" ADD ""Name"" AS ('hello') COLLATE NOCASE;");
}

[ConditionalFact]
public override Task Add_column_with_check_constraint()
=> AssertNotSupportedAsync(base.Add_column_with_check_constraint, SqliteStrings.InvalidMigrationOperation("CreateCheckConstraintOperation"));

public override Task Alter_column_make_required()
=> AssertNotSupportedAsync(base.Alter_column_make_required, SqliteStrings.InvalidMigrationOperation("AlterColumnOperation"));

Expand Down

0 comments on commit f244396

Please sign in to comment.