|
| 1 | +using NPoco; |
| 2 | +using Umbraco.Cms.Core; |
| 3 | +using Umbraco.Cms.Core.Serialization; |
| 4 | +using Umbraco.Cms.Infrastructure.Persistence; |
| 5 | +using Umbraco.Cms.Infrastructure.Persistence.Dtos; |
| 6 | +using Umbraco.Extensions; |
| 7 | + |
| 8 | +namespace Umbraco.Cms.Infrastructure.Migrations.Upgrade.V_14_2_0; |
| 9 | + |
| 10 | +public class AddMissingDateTimeConfiguration : MigrationBase |
| 11 | +{ |
| 12 | + private readonly IConfigurationEditorJsonSerializer _configurationEditorJsonSerializer; |
| 13 | + |
| 14 | + public AddMissingDateTimeConfiguration(IMigrationContext context, IConfigurationEditorJsonSerializer configurationEditorJsonSerializer) |
| 15 | + : base(context) |
| 16 | + => _configurationEditorJsonSerializer = configurationEditorJsonSerializer; |
| 17 | + |
| 18 | + protected override void Migrate() |
| 19 | + { |
| 20 | + Sql<ISqlContext> sql = Sql() |
| 21 | + .Select<DataTypeDto>() |
| 22 | + .From<DataTypeDto>() |
| 23 | + .Where<DataTypeDto>(dto => |
| 24 | + dto.NodeId == Constants.DataTypes.DateTime |
| 25 | + && dto.EditorAlias.Equals(Constants.PropertyEditors.Aliases.DateTime)); |
| 26 | + |
| 27 | + DataTypeDto? dataTypeDto = Database.FirstOrDefault<DataTypeDto>(sql); |
| 28 | + if (dataTypeDto is null) |
| 29 | + { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + Dictionary<string, object> configurationData = dataTypeDto.Configuration.IsNullOrWhiteSpace() |
| 34 | + ? new Dictionary<string, object>() |
| 35 | + : _configurationEditorJsonSerializer |
| 36 | + .Deserialize<Dictionary<string, object?>>(dataTypeDto.Configuration)? |
| 37 | + .Where(item => item.Value is not null) |
| 38 | + .ToDictionary(item => item.Key, item => item.Value!) |
| 39 | + ?? new Dictionary<string, object>(); |
| 40 | + |
| 41 | + // only proceed with the migration if the data-type has no format assigned |
| 42 | + if (configurationData.TryAdd("format", "YYYY-MM-DD HH:mm:ss") is false) |
| 43 | + { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + dataTypeDto.Configuration = _configurationEditorJsonSerializer.Serialize(configurationData); |
| 48 | + Database.Update(dataTypeDto); |
| 49 | + } |
| 50 | +} |
0 commit comments