Fix to #29902 - Renaming PeriodStart and PeriodEnd columns of a temporal table causes them to be swapped (#32328)

Problem was that migration model differ was too lax with pairing up columns. Fix is to add more predicates - one that matches annotations+values and one that just matches annotations, ignoring the values, before we fallback to simple column definition.
Now that we reworked temporal annotations, this actually gives clean match for the period start and period end column.
Moreover, this change could improve matching in other, non-temporal scenarios.

Fixes #29902
This commit is contained in:
Maurycy Markowski 2023-11-17 14:10:01 -08:00 коммит произвёл GitHub
Родитель f55b4a0c83
Коммит 1e3207347c
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 45 добавлений и 1 удалений

Просмотреть файл

@ -929,8 +929,52 @@ public class MigrationsModelDiffer : IMigrationsModelDiffer
tm =>
string.Equals(sm.Property.Name, tm.Property.Name, StringComparison.OrdinalIgnoreCase)
&& EntityTypePathEquals(sm.Property.DeclaringType, tm.Property.DeclaringType, c))),
(s, t, _) => ColumnStructureEquals(s, t) && ColumnAnnotationsEqual(s, t, matchValues: true),
(s, t, _) => ColumnStructureEquals(s, t) && ColumnAnnotationsEqual(s, t, matchValues: false),
(s, t, _) => ColumnStructureEquals(s, t));
private static bool ColumnAnnotationsEqual(IColumn source, IColumn target, bool matchValues)
{
var sourceAnnotations = source.GetAnnotations().ToList();
var targetAnnotations = target.GetAnnotations().ToList();
if (sourceAnnotations.Count != targetAnnotations.Count)
{
return false;
}
foreach (var sourceAnnotation in sourceAnnotations)
{
var matchFound = false;
for (var i = 0; i < targetAnnotations.Count; i++)
{
var targetAnnotation = targetAnnotations[i];
if (sourceAnnotation.Name != targetAnnotation.Name)
{
continue;
}
if (matchValues && sourceAnnotation.Value != targetAnnotation.Value)
{
continue;
}
targetAnnotations.RemoveAt(i);
matchFound = true;
break;
}
if (!matchFound)
{
return false;
}
}
return true;
}
private static bool ColumnStructureEquals(IColumn source, IColumn target)
{
if (!source.TryGetDefaultValue(out var sourceDefault))

Просмотреть файл

@ -8429,7 +8429,7 @@ EXEC sp_rename N'[CustomersHistory]', N'HistoryTable';
""");
}
[ConditionalFact(Skip = "issue #29902")]
[ConditionalFact]
public virtual async Task Change_names_of_period_columns_in_temporal_table()
{
await Test(