Fix migration to handle identical aliases for different group names and write error to log

This commit is contained in:
Ronald Barendse
2021-07-15 16:53:42 +02:00
parent 40bce56efd
commit cae6e02ceb

View File

@@ -1,4 +1,5 @@
using System.Linq;
using Umbraco.Core.Logging;
using Umbraco.Core.Persistence.Dtos;
namespace Umbraco.Core.Migrations.Upgrade.V_8_16_0
@@ -12,16 +13,40 @@ namespace Umbraco.Core.Migrations.Upgrade.V_8_16_0
public override void Migrate()
{
AddColumn<PropertyTypeGroupDto>("type");
AddColumn<PropertyTypeGroupDto>("alias", out var sqls);
var dtos = Database.Fetch<PropertyTypeGroupDto>();
foreach (var dto in dtos)
{
// Generate alias from current name
dto.Alias = dto.Text.ToSafeAlias(true);
Database.Update(dto, x => new { x.Alias });
AddColumn<PropertyTypeGroupDto>("alias", out var sqls);
var dtos = Database.Fetch<PropertyTypeGroupDto>();
foreach (var dtosPerAlias in dtos.GroupBy(x => x.Text.ToSafeAlias(true)))
{
var dtosPerAliasAndText = dtosPerAlias.GroupBy(x => x.Text);
var numberSuffix = 1;
foreach (var dtosPerText in dtosPerAliasAndText)
{
foreach (var dto in dtosPerText)
{
dto.Alias = dtosPerAlias.Key;
if (numberSuffix > 1)
{
// More than 1 name found for the alias, so add a suffix
dto.Alias += numberSuffix;
}
Database.Update(dto, x => new { x.Alias });
}
numberSuffix++;
}
if (numberSuffix > 2)
{
Logger.Error<AddPropertyTypeGroupColumns>("Detected the same alias {Alias} for different property group names {Names}, the migration added suffixes, but this might break backwards compatibility.", dtosPerAlias.Key, dtosPerAliasAndText.Select(x => x.Key));
}
}
foreach (var sql in sqls) Database.Execute(sql);
foreach (var sql in sqls)
Database.Execute(sql);
}
}
}