Skip to content

Commit

Permalink
[6.0.2] Restore the ability to break multiple FK cycles (#26959)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriySvyryd authored Dec 15, 2021
1 parent 0d35946 commit e13e56c
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/Shared/Multigraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public IReadOnlyList<TVertex> TopologicalSort(
&& tryBreakEdge != null)
{
var candidateVertex = candidateVertices[candidateIndex];
if (predecessorCounts[candidateVertex] != 1)
if (predecessorCounts[candidateVertex] == 0)
{
candidateIndex++;
continue;
Expand All @@ -211,9 +211,12 @@ public IReadOnlyList<TVertex> TopologicalSort(
_successorMap[incomingNeighbor].Remove(candidateVertex);
_predecessorMap[candidateVertex].Remove(incomingNeighbor);
predecessorCounts[candidateVertex]--;
queue.Add(candidateVertex);
broken = true;
break;
if (predecessorCounts[candidateVertex] == 0)
{
queue.Add(candidateVertex);
broken = true;
}
continue;
}

candidateIndex++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,91 @@ public void Model_differ_breaks_double_foreign_key_cycles_in_create_table_operat
});
}

[ConditionalFact]
public void Model_differ_breaks_multiple_foreign_key_cycles_in_create_table_operations()
{
Execute(
_ => { },
modelBuilder =>
{
modelBuilder.Entity<Book>(entity =>
{
entity.HasOne(d => d.Album)
.WithMany(p => p.Books);
entity.HasOne(d => d.User)
.WithMany(p => p.Books);
});
modelBuilder.Entity<Album>(entity =>
{
entity.HasOne(d => d.OwnerUser)
.WithMany(p => p.AlbumOwnerUsers);
entity.HasOne(d => d.Book)
.WithMany(p => p.Albums);
});
modelBuilder.Entity<User>(entity =>
{
entity.HasOne(d => d.Book)
.WithMany(p => p.Users);
entity.HasOne(d => d.ReaderGroup)
.WithMany(p => p.UserReaderGroups);
});
modelBuilder.Entity<Group>(entity =>
{
entity.HasOne(d => d.OwnerAlbum)
.WithMany(p => p.Groups);
entity.HasOne(d => d.OwnerUser)
.WithMany(p => p.Groups);
});
},
result =>
{
Assert.Equal(4, result.OfType<CreateTableOperation>().Count());
Assert.Equal(8, result.OfType<CreateIndexOperation>().Count());
Assert.Equal(8, result.OfType<CreateTableOperation>().SelectMany(t => t.ForeignKeys).Count()
+ result.OfType<AddForeignKeyOperation>().Count());
});
}

private class Book
{
public int Id { get; set; }

public Album Album { get; set; }
public User User { get; set; }
public ICollection<Album> Albums { get; set; }
public ICollection<User> Users { get; set; }
}

private class Album
{
public int Id { get; set; }

public User OwnerUser { get; set; }
public Book Book { get; set; }
public ICollection<Book> Books { get; set; }
public ICollection<Group> Groups { get; set; }
}

private class User
{
public int Id { get; set; }

public Book Book { get; set; }
public Group ReaderGroup { get; set; }
public ICollection<Album> AlbumOwnerUsers { get; set; }
public ICollection<Book> Books { get; set; }
public ICollection<Group> Groups { get; set; }
}

private class Group
{
public int Id { get; set; }

public Album OwnerAlbum { get; set; }
public User OwnerUser { get; set; }
public ICollection<User> UserReaderGroups { get; set; }
}

[ConditionalFact]
public void Create_table()
{
Expand Down

0 comments on commit e13e56c

Please sign in to comment.