Skip to content

Commit

Permalink
Merge pull request #135 from Jumoo/wip/propertymigrators
Browse files Browse the repository at this point in the history
Migrators by property name.
  • Loading branch information
KevinJump authored Jun 5, 2023
2 parents 5b9e544 + 601c285 commit 62d0e4c
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 25 deletions.
10 changes: 10 additions & 0 deletions MyMigrations/MyMigrationProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ public MyMigrationProfile(SyncMigrationHandlerCollection migrationHandlers)

// eveything beneath is optional...

//PropertyMigrators = new Dictionary<string, string>()
//{
// // use the NestedToBlockListMigrator For myProperty in the 'MyContentType' contentType
// { "myContentType_myProperty", nameof(NestedToBlockListMigrator) },

// // Convert all properties called myGridProperty to blocklist
// { "myGridProperty", nameof(GridToBlockListMigrator) }
//},


// add a list of things we don't want to import
BlockedItems = new Dictionary<string, List<string>>
{
Expand Down
10 changes: 10 additions & 0 deletions uSync.Migrations/Configuration/Models/MigrationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ public class MigrationOptions

public IDictionary<string, string>? PreferredMigrators { get; set; }

/// <summary>
/// migrators by property name.
/// </summary>
/// <remarks>
/// property migrators will be searched by property alias
/// and contentType_propertyAlias, so you can define both
/// for all properties with a name, or restrict to the content/media type
/// </remarks>
public IDictionary<string, string>? PropertyMigrators { get; set; }

public bool BlockListViews { get; set; } = true;

public bool BlockCommonTypes { get; set; } = true;
Expand Down
24 changes: 24 additions & 0 deletions uSync.Migrations/Context/MigratorsContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,36 @@ namespace uSync.Migrations.Context;
public class MigratorsContext
{
private Dictionary<string, ISyncPropertyMigrator> _migrators { get; set; } = new(StringComparer.OrdinalIgnoreCase);
private Dictionary<string, ISyncPropertyMigrator> _propertyMigrators { get; set; } = new(StringComparer.OrdinalIgnoreCase);

public ISyncPropertyMigrator? TryGetMigrator(string? editorAlias)
=> string.IsNullOrEmpty(editorAlias)
? null
: _migrators.TryGetValue(editorAlias, out var migrator) == true ? migrator : null;

public ISyncPropertyMigrator? TryGetPropertyAliasMigrator(string? propertyAlias)
{
if (string.IsNullOrEmpty(propertyAlias)) return null;

// search for the full pattern
if (_propertyMigrators.TryGetValue(propertyAlias, out var migrator))
return migrator;

// if we haven't found - but its a split (contentType_alias) value split the value and look just for the
// propertyAlias

if (propertyAlias.IndexOf('_') > 0)
{
var propertyEditorAlias = propertyAlias.Substring(propertyAlias.IndexOf('_') + 1);
return _propertyMigrators.TryGetValue(propertyEditorAlias, out var propertyAliasMigrator) == true
? propertyAliasMigrator : null;
}
return null;
}

public ISyncPropertyMigrator? TryGetMigrator(string? propertyAlias, string? editorAlias)
=> TryGetPropertyAliasMigrator(propertyAlias) ?? TryGetMigrator(editorAlias);

/// <summary>
/// Add a migrator for a given editorAlias
/// </summary>
Expand Down
11 changes: 10 additions & 1 deletion uSync.Migrations/Handlers/MigrationHandlerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,16 @@ private IEnumerable<MigrationMessage> MigrateFolder(string folder, int level, Sy
catch(Exception ex)
{
_logger.LogError(ex, "Error while processing {file}", file);
throw new Exception($"Error processing {file}", ex);

var name = Path.Combine(
Path.GetFileNameWithoutExtension(Path.GetDirectoryName(file)) ?? "",
Path.GetFileNameWithoutExtension(file));

// throw new Exception($"Error processing {file}", ex);
messages.Add(new MigrationMessage(ItemType, name, MigrationMessageType.Error)
{
Message = ex.Message
});
}
}

Expand Down
63 changes: 39 additions & 24 deletions uSync.Migrations/Handlers/Shared/SharedContentBaseHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Strings;
using Umbraco.Extensions;

using uSync.Core;
using uSync.Migrations.Context;
using uSync.Migrations.Migrators;
Expand Down Expand Up @@ -113,34 +114,45 @@ protected virtual IEnumerable<XElement> ConvertPropertyValue(string itemType, st
{
var editorAlias = context.ContentTypes.GetEditorAliasByTypeAndProperty(contentType, property.Name.LocalName)?.OriginalEditorAlias ?? string.Empty;

// convert the property .
try
{

var migrationProperty = new SyncMigrationContentProperty(
contentType, property.Name.LocalName, editorAlias, property.Value);
// convert the property .
_logger.LogDebug("ConvertPropertyValue: {itemType} {contentType} {editorAlias}", itemType, contentType, editorAlias);

var migrator = context.Migrators.TryGetVariantMigrator(editorAlias);
if (migrator != null && itemType == "Content")
{
// it might be the case that the property needs to be split into variants.
// if this is the case a ISyncVariationPropertyEditor will exist and it can
// split a single value into a collection split by culture
var vortoElement = GetVariedValueNode(migrator, contentType, property.Name.LocalName, migrationProperty, context);
if (vortoElement != null) return vortoElement.AsEnumerableOfOne();
}
var migrationProperty = new SyncMigrationContentProperty(
contentType, property.Name.LocalName, editorAlias, property.Value);

var propertyMigrator = context.Migrators.TryGetMigrator(
$"{migrationProperty.ContentTypeAlias}_{migrationProperty.PropertyAlias}", migrationProperty.EditorAlias);

if (propertyMigrator != null)
{
switch (propertyMigrator)
{
case ISyncVariationPropertyMigrator variationPropertyMigrator:
_logger.LogDebug("Variation Migrator {name}", variationPropertyMigrator.GetType().Name);
var variationResult = GetVariedValueNode(variationPropertyMigrator, contentType, property.Name.LocalName, migrationProperty, context);
if (variationResult != null) return variationResult.AsEnumerableOfOne();
break;
case ISyncPropertySplittingMigrator splittingMigrator:
_logger.LogDebug("Splitting migrator {name}", splittingMigrator.GetType().Name);
return GetSplitPropertyValueNodes(splittingMigrator, contentType, property.Name.LocalName, migrationProperty, context);
}
}

// we might want to split this property into multiple properties
var propertySplittingMigrator = context.Migrators.TryGetPropertySplittingMigrator(editorAlias);
if (propertySplittingMigrator != null)
// default this value doesn't need to be split
// and we can 'just' migrate it on its own.
var migratedValue = MigrateContentValue(migrationProperty, context);
return new XElement(property.Name.LocalName,
new XElement("Value", new XCData(migratedValue))).AsEnumerableOfOne();
}
catch(Exception ex)
{
var splitElements = GetSplitPropertyValueNodes(propertySplittingMigrator, contentType, property.Name.LocalName, migrationProperty, context);
return splitElements;
_logger.LogWarning("Failed to migrate property [{editorAlias} {property}] {ex}",
editorAlias, property.Name.LocalName, ex.Message);
throw new Exception($"Failed migrating [{editorAlias} - {property.Name.LocalName}] : {ex.Message}", ex);
}

// or this value doesn't need to be split
// and we can 'just' migrate it on its own.
var migratedValue = MigrateContentValue(migrationProperty, context);
return new XElement(property.Name.LocalName,
new XElement("Value", new XCData(migratedValue))).AsEnumerableOfOne();
}

protected virtual IEnumerable<XElement> GetSplitPropertyValueNodes(ISyncPropertySplittingMigrator propertySplittingMigrator, string contentType, string propertyAlias, SyncMigrationContentProperty migrationProperty, SyncMigrationContext context)
Expand Down Expand Up @@ -199,7 +211,10 @@ protected virtual string MigrateContentValue(SyncMigrationContentProperty migrat

if (string.IsNullOrWhiteSpace(migrationProperty.EditorAlias)) return migrationProperty.Value;

var migrator = context.Migrators.TryGetMigrator(migrationProperty.EditorAlias);
var migrator = context.Migrators.TryGetMigrator(
$"{migrationProperty.ContentTypeAlias}_{migrationProperty.PropertyAlias}", migrationProperty.EditorAlias);

// var migrator = context.Migrators.TryGetMigrator(migrationProperty.EditorAlias);
if (migrator != null)
{
return migrator?.GetContentValue(migrationProperty, context) ?? migrationProperty.Value;
Expand Down
1 change: 1 addition & 0 deletions uSync.Migrations/Services/SyncMigrationStatusService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ private string GetSiteRelativePath(string folder)
IgnoredPropertiesByContentType = defaultOptions.IgnoredPropertiesByContentType,
MigrationType = defaultOptions.MigrationType,
PreferredMigrators = defaultOptions.PreferredMigrators,
PropertyMigrators = defaultOptions.PropertyMigrators,
};
}

Expand Down

0 comments on commit 62d0e4c

Please sign in to comment.