Skip to content

Commit

Permalink
Fix for 21072. Rename GetName/GetDefaultName methods to GetDatabaseNa…
Browse files Browse the repository at this point in the history
…me/GetDefaultDatabaseName for index extension methods. (#21081)
  • Loading branch information
lajones authored May 29, 2020
1 parent 95e779b commit 0afeb92
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ protected virtual IndexBuilder VisitUniqueConstraint(
var indexBuilder = builder.HasIndex(propertyNames).IsUnique();

if (!string.IsNullOrEmpty(uniqueConstraint.Name)
&& uniqueConstraint.Name != indexBuilder.Metadata.GetDefaultName())
&& uniqueConstraint.Name != indexBuilder.Metadata.GetDefaultDatabaseName())
{
indexBuilder.HasName(uniqueConstraint.Name);
}
Expand Down Expand Up @@ -683,7 +683,7 @@ protected virtual IndexBuilder VisitIndex([NotNull] EntityTypeBuilder builder, [
}

if (!string.IsNullOrEmpty(index.Name)
&& index.Name != indexBuilder.Metadata.GetDefaultName())
&& index.Name != indexBuilder.Metadata.GetDefaultDatabaseName())
{
indexBuilder.HasName(index.Name);
}
Expand Down
27 changes: 18 additions & 9 deletions src/EFCore.Relational/Extensions/RelationalIndexExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static class RelationalIndexExtensions
/// <param name="index"> The index. </param>
/// <returns> The name for this index. </returns>
public static string GetDatabaseName([NotNull] this IIndex index)
=> index.Name ?? index.GetDefaultName();
=> index.Name ?? index.GetDefaultDatabaseName();

/// <summary>
/// Returns the name for this index.
Expand All @@ -33,7 +33,7 @@ public static string GetDatabaseName([NotNull] this IIndex index)
/// <returns> The name for this index. </returns>
[Obsolete("Use GetDatabaseName() instead")]
public static string GetName([NotNull] this IIndex index)
=> index.Name ?? index.GetDefaultName();
=> GetDatabaseName(index);

/// <summary>
/// Returns the name for this index.
Expand All @@ -42,18 +42,18 @@ public static string GetName([NotNull] this IIndex index)
/// <param name="tableName"> The table name. </param>
/// <param name="schema"> The schema. </param>
/// <returns> The name for this index. </returns>
public static string GetName(
public static string GetDatabaseName(
[NotNull] this IIndex index,
[NotNull] string tableName,
[CanBeNull] string schema)
=> index.Name ?? index.GetDefaultName(tableName, schema);
=> index.Name ?? index.GetDefaultDatabaseName(tableName, schema);

/// <summary>
/// Returns the default name that would be used for this index.
/// </summary>
/// <param name="index"> The index. </param>
/// <returns> The default name that would be used for this index. </returns>
public static string GetDefaultName([NotNull] this IIndex index)
public static string GetDefaultDatabaseName([NotNull] this IIndex index)
{
var tableName = index.DeclaringEntityType.GetTableName();
var schema = index.DeclaringEntityType.GetSchema();
Expand All @@ -67,14 +67,23 @@ public static string GetDefaultName([NotNull] this IIndex index)
return Uniquifier.Truncate(baseName, index.DeclaringEntityType.Model.GetMaxIdentifierLength());
}

/// <summary>
/// Returns the default name that would be used for this index.
/// </summary>
/// <param name="index"> The index. </param>
/// <returns> The default name that would be used for this index. </returns>
[Obsolete("Use GetDefaultDatabaseName() instead")]
public static string GetDefaultName([NotNull] this IIndex index)
=> GetDefaultDatabaseName(index);

/// <summary>
/// Returns the default name that would be used for this index.
/// </summary>
/// <param name="index"> The index. </param>
/// <param name="tableName"> The table name. </param>
/// <param name="schema"> The schema. </param>
/// <returns> The default name that would be used for this index. </returns>
public static string GetDefaultName(
public static string GetDefaultDatabaseName(
[NotNull] this IIndex index,
[NotNull] string tableName,
[CanBeNull] string schema)
Expand All @@ -100,7 +109,7 @@ public static string GetDefaultName(

if (rootIndex != index)
{
return rootIndex.GetName(tableName, schema);
return rootIndex.GetDatabaseName(tableName, schema);
}

var baseName = new StringBuilder()
Expand Down Expand Up @@ -237,7 +246,7 @@ public static IIndex FindSharedTableRootIndex(
Check.NotNull(index, nameof(index));
Check.NotNull(tableName, nameof(tableName));

var indexName = index.GetName(tableName, schema);
var indexName = index.GetDatabaseName(tableName, schema);
var rootIndex = index;

// Limit traversal to avoid getting stuck in a cycle (validation will throw for these later)
Expand All @@ -247,7 +256,7 @@ public static IIndex FindSharedTableRootIndex(
var linkedIndex = rootIndex.DeclaringEntityType
.FindTableRowInternalForeignKeys(tableName, schema)
.SelectMany(fk => fk.PrincipalEntityType.GetIndexes())
.FirstOrDefault(i => i.GetName(tableName, schema) == indexName);
.FirstOrDefault(i => i.GetDatabaseName(tableName, schema) == indexName);
if (linkedIndex == null)
{
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ protected virtual void ValidateSharedIndexesCompatibility(

foreach (var index in mappedTypes.SelectMany(et => et.GetDeclaredIndexes()))
{
var indexName = index.GetName(tableName, schema);
var indexName = index.GetDatabaseName(tableName, schema);
if (!indexMappings.TryGetValue(indexName, out var duplicateIndex))
{
indexMappings[indexName] = index;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ private void TryUniquifyIndexNames(
{
foreach (var index in entityType.GetDeclaredIndexes())
{
var indexName = index.GetName(tableName, schema);
var indexName = index.GetDatabaseName(tableName, schema);
if (!indexes.TryGetValue(indexName, out var otherIndex))
{
indexes[indexName] = index;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static bool AreCompatible(
duplicateIndex.Properties.Format(),
duplicateIndex.DeclaringEntityType.DisplayName(),
index.DeclaringEntityType.GetSchemaQualifiedTableName(),
index.GetName(tableName, schema),
index.GetDatabaseName(tableName, schema),
index.Properties.FormatColumns(tableName, schema),
duplicateIndex.Properties.FormatColumns(tableName, schema)));
}
Expand All @@ -61,7 +61,7 @@ public static bool AreCompatible(
duplicateIndex.Properties.Format(),
duplicateIndex.DeclaringEntityType.DisplayName(),
index.DeclaringEntityType.GetSchemaQualifiedTableName(),
index.GetName(tableName, schema)));
index.GetDatabaseName(tableName, schema)));
}

return false;
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Relational/Metadata/Internal/RelationalModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ private static void PopulateConstraints(Table table)

foreach (var index in entityType.GetIndexes())
{
var name = index.GetName(table.Name, table.Schema);
var name = index.GetDatabaseName(table.Name, table.Schema);
if (!table.Indexes.TryGetValue(name, out var tableIndex))
{
var columns = new Column[index.Properties.Count];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static bool AreCompatibleForSqlServer(
duplicateIndex.Properties.Format(),
duplicateIndex.DeclaringEntityType.DisplayName(),
index.DeclaringEntityType.GetSchemaQualifiedTableName(),
index.GetName(tableName, schema),
index.GetDatabaseName(tableName, schema),
FormatInclude(index, tableName, schema),
FormatInclude(duplicateIndex, tableName, schema)));
}
Expand All @@ -66,7 +66,7 @@ public static bool AreCompatibleForSqlServer(
duplicateIndex.Properties.Format(),
duplicateIndex.DeclaringEntityType.DisplayName(),
index.DeclaringEntityType.GetSchemaQualifiedTableName(),
index.GetName(tableName, schema)));
index.GetDatabaseName(tableName, schema)));
}

return false;
Expand All @@ -83,7 +83,7 @@ public static bool AreCompatibleForSqlServer(
duplicateIndex.Properties.Format(),
duplicateIndex.DeclaringEntityType.DisplayName(),
index.DeclaringEntityType.GetSchemaQualifiedTableName(),
index.GetName(tableName, schema)));
index.GetDatabaseName(tableName, schema)));
}

return false;
Expand All @@ -100,7 +100,7 @@ public static bool AreCompatibleForSqlServer(
duplicateIndex.Properties.Format(),
duplicateIndex.DeclaringEntityType.DisplayName(),
index.DeclaringEntityType.GetSchemaQualifiedTableName(),
index.GetName(tableName, schema)));
index.GetDatabaseName(tableName, schema)));
}

return false;
Expand Down

0 comments on commit 0afeb92

Please sign in to comment.