-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the default date-with-time configuration if missing (#16902)
- Loading branch information
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/Umbraco.Infrastructure/Migrations/Upgrade/V_14_2_0/AddMissingDateTimeConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using NPoco; | ||
using Umbraco.Cms.Core; | ||
using Umbraco.Cms.Core.Serialization; | ||
using Umbraco.Cms.Infrastructure.Persistence; | ||
using Umbraco.Cms.Infrastructure.Persistence.Dtos; | ||
using Umbraco.Extensions; | ||
|
||
namespace Umbraco.Cms.Infrastructure.Migrations.Upgrade.V_14_2_0; | ||
|
||
public class AddMissingDateTimeConfiguration : MigrationBase | ||
{ | ||
private readonly IConfigurationEditorJsonSerializer _configurationEditorJsonSerializer; | ||
|
||
public AddMissingDateTimeConfiguration(IMigrationContext context, IConfigurationEditorJsonSerializer configurationEditorJsonSerializer) | ||
: base(context) | ||
=> _configurationEditorJsonSerializer = configurationEditorJsonSerializer; | ||
|
||
protected override void Migrate() | ||
{ | ||
Sql<ISqlContext> sql = Sql() | ||
.Select<DataTypeDto>() | ||
.From<DataTypeDto>() | ||
.Where<DataTypeDto>(dto => | ||
dto.NodeId == Constants.DataTypes.DateTime | ||
&& dto.EditorAlias.Equals(Constants.PropertyEditors.Aliases.DateTime)); | ||
|
||
DataTypeDto? dataTypeDto = Database.FirstOrDefault<DataTypeDto>(sql); | ||
if (dataTypeDto is null) | ||
{ | ||
return; | ||
} | ||
|
||
Dictionary<string, object> configurationData = dataTypeDto.Configuration.IsNullOrWhiteSpace() | ||
? new Dictionary<string, object>() | ||
: _configurationEditorJsonSerializer | ||
.Deserialize<Dictionary<string, object?>>(dataTypeDto.Configuration)? | ||
.Where(item => item.Value is not null) | ||
.ToDictionary(item => item.Key, item => item.Value!) | ||
?? new Dictionary<string, object>(); | ||
|
||
// only proceed with the migration if the data-type has no format assigned | ||
if (configurationData.TryAdd("format", "YYYY-MM-DD HH:mm:ss") is false) | ||
{ | ||
return; | ||
} | ||
|
||
dataTypeDto.Configuration = _configurationEditorJsonSerializer.Serialize(configurationData); | ||
Database.Update(dataTypeDto); | ||
} | ||
} |