-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use IRelationalMetadataExtensionProvider in ModelDiffer
- Loading branch information
Showing
7 changed files
with
316 additions
and
168 deletions.
There are no files selected for viewing
252 changes: 163 additions & 89 deletions
252
src/EntityFramework.Relational/Migrations/Infrastructure/ModelDiffer.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 43 additions & 33 deletions
76
test/EntityFramework.Relational.Tests/Migrations/Infrastructure/ModelDifferTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,57 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.Data.Entity.Relational.Migrations.Infrastructure; | ||
using Microsoft.Data.Entity.Relational.Migrations.Operations; | ||
using Xunit; | ||
|
||
namespace Microsoft.Data.Entity.Relational.Tests.Migrations.Infrastructure | ||
namespace Microsoft.Data.Entity.Relational.Migrations.Infrastructure | ||
{ | ||
public class ModelDifferTest | ||
public class ModelDifferTest : ModelDifferTestBase | ||
{ | ||
[Fact] | ||
public void Model_differ_breaks_foreign_key_cycles() | ||
{ | ||
var model = new Entity.Metadata.Model(); | ||
|
||
var firstType = model.AddEntityType("First"); | ||
var firstKey = firstType.SetPrimaryKey(firstType.AddProperty("ID", typeof(int), true)); | ||
var firstFk = firstType.AddProperty("FK", typeof(int), true); | ||
|
||
var secondType = model.AddEntityType("Second"); | ||
var secondKey = secondType.SetPrimaryKey(secondType.AddProperty("ID", typeof(int), true)); | ||
var secondFk = secondType.AddProperty("FK", typeof(int), true); | ||
|
||
firstType.AddForeignKey(firstFk, secondKey); | ||
secondType.AddForeignKey(secondFk, firstKey); | ||
|
||
var modelDiffer = new ModelDiffer(new RelationalTypeMapper()); | ||
|
||
var result = modelDiffer.GetDifferences(null, model); | ||
|
||
Assert.Equal(3, result.Count); | ||
|
||
var firstOperation = result[0] as CreateTableOperation; | ||
var secondOperation = result[1] as CreateTableOperation; | ||
var thirdOperation = result[2] as AddForeignKeyOperation; | ||
|
||
Assert.NotNull(firstOperation); | ||
Assert.NotNull(secondOperation); | ||
Assert.NotNull(thirdOperation); | ||
|
||
Assert.Equal(0, firstOperation.ForeignKeys.Count); | ||
Assert.Equal(1, secondOperation.ForeignKeys.Count); | ||
Assert.Equal(firstOperation.Name, thirdOperation.Table); | ||
Execute( | ||
_ => { }, | ||
modelBuilder => | ||
{ | ||
modelBuilder.Entity( | ||
"First", | ||
x => | ||
{ | ||
x.Property<int>("ID"); | ||
x.Key("ID"); | ||
x.Property<int>("FK"); | ||
}); | ||
modelBuilder.Entity( | ||
"Second", | ||
x => | ||
{ | ||
x.Property<int>("ID"); | ||
x.Key("ID"); | ||
x.Property<int>("FK"); | ||
}); | ||
modelBuilder.Entity("First").Reference("Second").InverseCollection().ForeignKey("FK").PrincipalKey("ID"); | ||
modelBuilder.Entity("Second").Reference("First").InverseCollection().ForeignKey("FK").PrincipalKey("ID"); | ||
}, | ||
result => | ||
{ | ||
Assert.Equal(3, result.Count); | ||
var firstOperation = result[0] as CreateTableOperation; | ||
var secondOperation = result[1] as CreateTableOperation; | ||
var thirdOperation = result[2] as AddForeignKeyOperation; | ||
Assert.NotNull(firstOperation); | ||
Assert.NotNull(secondOperation); | ||
Assert.NotNull(thirdOperation); | ||
Assert.Equal(0, firstOperation.ForeignKeys.Count); | ||
Assert.Equal(1, secondOperation.ForeignKeys.Count); | ||
Assert.Equal(firstOperation.Name, thirdOperation.Table); | ||
}); | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
test/EntityFramework.Relational.Tests/Migrations/Infrastructure/ModelDifferTestBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.Data.Entity.Metadata; | ||
using Microsoft.Data.Entity.Metadata.Builders; | ||
using Microsoft.Data.Entity.Relational.Metadata; | ||
using Microsoft.Data.Entity.Relational.Migrations.Operations; | ||
|
||
namespace Microsoft.Data.Entity.Relational.Migrations.Infrastructure | ||
{ | ||
public class ModelDifferTestBase | ||
{ | ||
protected void Execute( | ||
Action<ModelBuilder> buildSourceAction, | ||
Action<ModelBuilder> buildTargetAction, | ||
Action<IReadOnlyList<MigrationOperation>> assertAction) | ||
{ | ||
var sourceModelBuilder = CreateModelBuilder(); | ||
buildSourceAction(sourceModelBuilder); | ||
|
||
var targetModelBuilder = CreateModelBuilder(); | ||
buildTargetAction(targetModelBuilder); | ||
|
||
var modelDiffer = CreateModelDiffer(); | ||
|
||
var operations = modelDiffer.GetDifferences(sourceModelBuilder.Model, targetModelBuilder.Model); | ||
|
||
assertAction(operations); | ||
} | ||
|
||
protected virtual ModelBuilder CreateModelBuilder() => new ModelBuilderFactory().CreateConventionBuilder(); | ||
protected virtual ModelDiffer CreateModelDiffer() => new ModelDiffer(new RelationalTypeMapper(), new TestMetadataExtensionProvider()); | ||
|
||
private class TestMetadataExtensionProvider : IRelationalMetadataExtensionProvider | ||
{ | ||
public IRelationalEntityTypeExtensions Extensions(IEntityType entityType) => entityType.Relational(); | ||
public IRelationalForeignKeyExtensions Extensions(IForeignKey foreignKey) => foreignKey.Relational(); | ||
public IRelationalIndexExtensions Extensions(IIndex index) => index.Relational(); | ||
public IRelationalKeyExtensions Extensions(IKey key) => key.Relational(); | ||
public IRelationalModelExtensions Extensions(IModel model) => model.Relational(); | ||
public IRelationalPropertyExtensions Extensions(IProperty property) => property.Relational(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
test/EntityFramework.SqlServer.Tests/Migrations/SqlServerModelDifferTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.Data.Entity.Relational.Migrations.Infrastructure; | ||
using Microsoft.Data.Entity.Relational.Migrations.Operations; | ||
using Microsoft.Data.Entity.SqlServer.Metadata; | ||
using Xunit; | ||
|
||
namespace Microsoft.Data.Entity.SqlServer.Migrations | ||
{ | ||
public class SqlServerModelDifferTest : ModelDifferTestBase | ||
{ | ||
[Fact] | ||
public void Can_use_provider_overrides() | ||
{ | ||
Execute( | ||
_ => { }, | ||
modelBuilder => | ||
{ | ||
modelBuilder.Entity( | ||
"Person", | ||
x => | ||
{ | ||
x.Property<int>("Id"); | ||
x.Key("Id"); | ||
x.ForSqlServer().Table("People"); | ||
}); | ||
}, | ||
operations => | ||
{ | ||
Assert.Equal(1, operations.Count); | ||
var addTableOperation = Assert.IsType<CreateTableOperation>(operations[0]); | ||
Assert.Equal("People", addTableOperation.Name); | ||
}); | ||
} | ||
|
||
protected override ModelBuilder CreateModelBuilder() => new SqlServerModelBuilderFactory().CreateConventionBuilder(); | ||
protected override ModelDiffer CreateModelDiffer() | ||
=> new SqlServerModelDiffer(new SqlServerTypeMapper(), new SqlServerMetadataExtensionProvider()); | ||
} | ||
} |