Skip to content

Commit

Permalink
RevEng: Call correct overload of HasIndex when scaffolding constraint…
Browse files Browse the repository at this point in the history
…s without name (#23277)

Resolves #23268
  • Loading branch information
smitpatel authored Nov 17, 2020
1 parent 46cfc53 commit a07bee7
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,10 @@ protected virtual IndexBuilder VisitUniqueConstraint(
}

var propertyNames = uniqueConstraint.Columns.Select(GetPropertyName).ToArray();
var indexBuilder = builder.HasIndex(propertyNames, uniqueConstraint.Name).IsUnique();
var indexBuilder = string.IsNullOrEmpty(uniqueConstraint.Name)
? builder.HasIndex(propertyNames)
: builder.HasIndex(propertyNames, uniqueConstraint.Name);
indexBuilder = indexBuilder.IsUnique();
indexBuilder.Metadata.AddAnnotations(uniqueConstraint.GetAnnotations());

return indexBuilder;
Expand Down Expand Up @@ -671,11 +674,11 @@ protected virtual IndexBuilder VisitIndex([NotNull] EntityTypeBuilder builder, [
}

var propertyNames = index.Columns.Select(GetPropertyName).ToArray();
var indexBuilder =
index.Name == null
? builder.HasIndex(propertyNames)
: builder.HasIndex(propertyNames, index.Name)
.IsUnique(index.IsUnique);
var indexBuilder = string.IsNullOrEmpty(index.Name)
? builder.HasIndex(propertyNames)
: builder.HasIndex(propertyNames, index.Name);

indexBuilder = indexBuilder.IsUnique(index.IsUnique);

if (index.Filter != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,87 @@ public void Unique_constraint()
Assert.Same(entityType.FindProperty("MyColumn"), index.Properties.Single());
}

[ConditionalFact]
public void Unique_constraint_without_name()
{
var myColumn = new DatabaseColumn
{
Table = Table,
Name = "MyColumn",
StoreType = "int"
};

var databaseModel = new DatabaseModel
{
Tables =
{
new DatabaseTable
{
Database = Database,
Name = "MyTable",
Columns = { IdColumn, myColumn },
PrimaryKey = IdPrimaryKey,
UniqueConstraints =
{
new DatabaseUniqueConstraint
{
Table = Table,
Columns = { myColumn }
}
}
}
}
};

var entityType = (EntityType)_factory.Create(databaseModel, new ModelReverseEngineerOptions()).GetEntityTypes().Single();
var index = entityType.GetIndexes().Single();

Assert.True(index.IsUnique);
Assert.Equal("IX_MyTable_MyColumn", index.GetDatabaseName());
Assert.Same(entityType.FindProperty("MyColumn"), index.Properties.Single());
}

[ConditionalFact]
public void Unique_constraint_with_empty_string_name()
{
var myColumn = new DatabaseColumn
{
Table = Table,
Name = "MyColumn",
StoreType = "int"
};

var databaseModel = new DatabaseModel
{
Tables =
{
new DatabaseTable
{
Database = Database,
Name = "MyTable",
Columns = { IdColumn, myColumn },
PrimaryKey = IdPrimaryKey,
UniqueConstraints =
{
new DatabaseUniqueConstraint
{
Table = Table,
Name = "",
Columns = { myColumn }
}
}
}
}
};

var entityType = (EntityType)_factory.Create(databaseModel, new ModelReverseEngineerOptions()).GetEntityTypes().Single();
var index = entityType.GetIndexes().Single();

Assert.True(index.IsUnique);
Assert.Equal("IX_MyTable_MyColumn", index.GetDatabaseName());
Assert.Same(entityType.FindProperty("MyColumn"), index.Properties.Single());
}

[ConditionalFact]
public void Indexes_and_alternate_keys()
{
Expand Down Expand Up @@ -591,6 +672,14 @@ public void Indexes_and_alternate_keys()
Columns = { table.Columns.ElementAt(1) },
IsUnique = true
});
table.Indexes.Add(
new DatabaseIndex
{
Table = Table,
Name = "",
Columns = { table.Columns.ElementAt(2) },
IsUnique = true
});
table.Indexes.Add(
new DatabaseIndex
{
Expand All @@ -599,6 +688,13 @@ public void Indexes_and_alternate_keys()
Columns = { table.Columns.ElementAt(1), table.Columns.ElementAt(0) },
IsUnique = false
});
table.Indexes.Add(
new DatabaseIndex
{
Table = Table,
Columns = { table.Columns.ElementAt(1), table.Columns.ElementAt(2) },
IsUnique = false
});
table.Indexes.Add(
new DatabaseIndex
{
Expand All @@ -614,26 +710,47 @@ public void Indexes_and_alternate_keys()

Assert.Collection(
entityType.GetIndexes(),
indexColumn1 =>
t =>
{
Assert.True(t.IsUnique);
Assert.Null(t.Name);
Assert.Equal("IX_T_C3", t.GetDatabaseName());
Assert.Same(entityType.FindProperty("C3"), t.Properties.Single());
},
t =>
{
Assert.False(t.IsUnique);
Assert.Null(t.Name);
Assert.Equal("IX_T_C2_C3", t.GetDatabaseName());
Assert.Equal(new[] { "C2", "C3" }, t.Properties.Select(c => c.Name).ToArray());
},
t =>
{
Assert.False(indexColumn1.IsUnique);
Assert.Equal("IDX_C1", indexColumn1.GetDatabaseName());
Assert.Same(entityType.FindProperty("C1"), indexColumn1.Properties.Single());
Assert.False(t.IsUnique);
Assert.Equal("IDX_C1", t.Name);
Assert.Equal("IDX_C1", t.GetDatabaseName());
Assert.Same(entityType.FindProperty("C1"), t.Properties.Single());
},
uniqueColumn2 =>
t =>
{
Assert.True(uniqueColumn2.IsUnique);
Assert.Same(entityType.FindProperty("C2"), uniqueColumn2.Properties.Single());
Assert.True(t.IsUnique);
Assert.Equal("IDX_C2", t.Name);
Assert.Equal("IDX_C2", t.GetDatabaseName());
Assert.Same(entityType.FindProperty("C2"), t.Properties.Single());
},
indexColumn2Column1 =>
t =>
{
Assert.False(indexColumn2Column1.IsUnique);
Assert.Equal(new[] { "C2", "C1" }, indexColumn2Column1.Properties.Select(c => c.Name).ToArray());
Assert.False(t.IsUnique);
Assert.Equal("IDX_C2_C1", t.Name);
Assert.Equal("IDX_C2_C1", t.GetDatabaseName());
Assert.Equal(new[] { "C2", "C1" }, t.Properties.Select(c => c.Name).ToArray());
},
uniqueColumn3Column1 =>
t =>
{
Assert.True(uniqueColumn3Column1.IsUnique);
Assert.Equal(new[] { "C3", "C1" }, uniqueColumn3Column1.Properties.Select(c => c.Name).ToArray());
Assert.True(t.IsUnique);
Assert.Equal("UNQ_C3_C1", t.Name);
Assert.Equal("UNQ_C3_C1", t.GetDatabaseName());
Assert.Equal(new[] { "C3", "C1" }, t.Properties.Select(c => c.Name).ToArray());
}
);

Expand Down

0 comments on commit a07bee7

Please sign in to comment.