Skip to content

Fix table splitting for derived types in the update pipeline. - #8976

Merged
AndriySvyryd merged 1 commit into
devfrom
Issue8907
Jun 29, 2017
Merged

AndriySvyryd merged 1 commit into
devfrom
Issue8907

Conversation

@AndriySvyryd

Copy link
Copy Markdown
Member

Make the columns nullable for dependent types involved in table splitting.
When uniquifying column names use the declaring entity type name as prefix.

Fixes #8907

[Fact]
public void Can_query_shared()
{
using (var store = CreateTestStore(OnModelCreating))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this yet another test initialization pattern? 😄

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😢
Do we need to create new store everytime? Have a fixture based testing

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fixture is unnecessary as we don't share anything. The store needs to be created every time to get a clean database

Property.Format(key.Properties),
otherKey.Relational().Name,
Property.Format(otherKey.Properties)));
var entityType = unvalidatedTypes.Dequeue();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a comment describing the algorithm here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish there was another way to right this code to make it self-explanatory. (or you can suggest something)
In brief the algorithm is
Input List of entity types

  1. Take first entity type and make it first node in graph.
  2. Find all ETs which are either derived type or connected via identifying relationship and add them to graph.
  3. Repeat step 2 till graph no longer expands.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something recursive like in EagerLoadingExpressionVisitor.WalkNavigations?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar.
One difference I see is, in WalkNavigations, we know all outbound edges, we do DFS and add the leaf nodes. So the graph is known we are just walking.
Here we have list of nodes in which need to find the edges, hence create the unknown graph.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored this to look like a BFS

@anpete

anpete commented Jun 29, 2017

Copy link
Copy Markdown
Contributor

@bricelam, @smitpatel Are we happy that table-splitting works well with Migrations?

dependentList = new List<IEntityType>();
dependents[principalEntityType] = dependentList;
}
((List<IEntityType>)dependentList).Add(entityType);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems unusual - why is dependents declared using IReadOnlyList?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ModificationCommandIdentityMap constructor takes IReadOnlyDictionary<IEntityType, IReadOnlyList<IEntityType>> and one cannot cast Dictionary<IEntityType, List<IEntityType>> to it.

@smitpatel smitpatel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Migration should be fine.
This mainly changes how they are handled in model building & update pipeline.
Once perfectly built model migrations would just work that way.

Metadata changes look good with some comments.

[Fact]
public void Can_query_shared()
{
using (var store = CreateTestStore(OnModelCreating))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😢
Do we need to create new store everytime? Have a fixture based testing

validatedTypes.Add(entityType);
foreach (var entityType in invalidTypes)
{
throw new InvalidOperationException(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how does foreach & throw Exception work together? 😕

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we get here and invalidTypes.Count > 1, then why not just exit the loop above early. I.e. stop on the first invalid type.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is due to graph traversal.

For the graph of A->B->C and starting point as A. Till B is discovered C is invalid type. So we need to iterate over the graph till it expands (and stabilizes). Then only we can be certain of invalid type.

Property.Format(key.Properties),
otherKey.Relational().Name,
Property.Format(otherKey.Properties)));
var entityType = unvalidatedTypes.Dequeue();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish there was another way to right this code to make it self-explanatory. (or you can suggest something)
In brief the algorithm is
Input List of entity types

  1. Take first entity type and make it first node in graph.
  2. Find all ETs which are either derived type or connected via identifying relationship and add them to graph.
  3. Repeat step 2 till graph no longer expands.

&& entityTypeAnnotations.Schema == principalTypeAnnotations.Schema)
{
var index = -1;
for (var i = 0; i < pk.Properties.Count; i++)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use LINQ for this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be uglier IMO

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FindIndex method?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have that or are you proposing to extract it?

@smitpatel smitpatel Jun 29, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FindIndex is on List. You can use First. Would do the same as above loop just smaller code.
Leave it as is.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this better?

dependentList = new List<IEntityType>();
dependents[principalEntityType] = dependentList;
}
((List<IEntityType>)dependentList).Add(entityType);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seems to some mismatch here. Dictionary.Value is readonly list but the value being assigned is List instead. Consider making it one type.
If Readonly list is needed for perf then consider making them explicitly.

}

[Fact(Skip = "#8907")]
[Fact(Skip = "#8973")]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We moved from one bug to another 😅

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Progress! :trollface:

@anpete

anpete commented Jun 29, 2017

Copy link
Copy Markdown
Contributor

@smitpatel

This mainly changes how they are handled in model building & update pipeline.
Once perfectly built model migrations would just work that way.

I was mostly thinking about the fact that there can be multiple ETs pointing to same table and wanted to make sure this is handled somehow. Do we have a test for migrations with table splitting?

@smitpatel

Copy link
Copy Markdown
Contributor

@bricelam - For migration changes

@AndriySvyryd

Copy link
Copy Markdown
Member Author

I added tests to MigrationsModelDifferTest

var relationshipFound = validatedTypes.Any(validatedType =>
var entityType = typesToValidate.Dequeue();
var typesToValidateLeft = typesToValidate.Count;
var nextTypeSet = unvalidatedTypes.Where(validatedType =>

@smitpatel smitpatel Jun 29, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change the name of parameter in lambda?
picking validatedType from unvalidatedTypes enumerator :trollface:


foreach (var typeToValidate in typesToValidate.Skip(typesToValidateLeft))
{
unvalidatedTypes.Remove(typeToValidate);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed when Enqueueing if nextTypeSet is allocated using ToList().
Whichever is more performant 👍

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was added specifically to avoid ToList() 😄

Make the columns nullable for dependent types involved in table splitting.
When uniquifying column names use the declaring entity type name as prefix.

Fixes #8907
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants