Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Public all the migrators #256

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 38 additions & 42 deletions uSync.Migrations.Core/Context/ContentTypeMigrationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,35 @@ namespace uSync.Migrations.Core.Context;
/// </summary>
public class ContentTypeMigrationContext
{
public Dictionary<Guid, string> ContentTypeAliases { get; } = new();
public Dictionary<string, Guid> ContentTypeKeys { get; } = new(StringComparer.OrdinalIgnoreCase);
public Dictionary<string, HashSet<string>> ContentTypeCompositions { get; } = new(StringComparer.OrdinalIgnoreCase);
public Dictionary<string, EditorAliasInfo> PropertyTypes { get; } = new(StringComparer.OrdinalIgnoreCase);

private Dictionary<Guid, string> _contentTypeAliases { get; set; } = new();
private Dictionary<string, Guid> _contentTypeKeys { get; set; } = new(StringComparer.OrdinalIgnoreCase);
private Dictionary<string, HashSet<string>> _contentTypeCompositions { get; set; } = new(StringComparer.OrdinalIgnoreCase);
private Dictionary<string, EditorAliasInfo> _propertyTypes { get; set; } = new(StringComparer.OrdinalIgnoreCase);
public HashSet<string> IgnoredProperties { get; } = new(StringComparer.OrdinalIgnoreCase);

private HashSet<string> _ignoredProperties = new(StringComparer.OrdinalIgnoreCase);

private Dictionary<string, NewContentTypeInfo> _newDocTypes
public Dictionary<string, NewContentTypeInfo> NewDocTypes { get; }
= new Dictionary<string, NewContentTypeInfo>(StringComparer.OrdinalIgnoreCase);

/// <summary>
/// allows you to map property aliases in a content type to the specific datatype
/// </summary>
private Dictionary<string, string> _dataTypeAliases = new(StringComparer.OrdinalIgnoreCase);
public Dictionary<string, string> DataTypeAliases { get; } = new(StringComparer.OrdinalIgnoreCase);

// tabs that are to be changed
private List<TabOptions> _changedTabs { get; set; } = new List<TabOptions>();

/// <summary>
/// list of content types that need to be set as element types.
/// </summary>
private HashSet<Guid> _elementContentTypes = new HashSet<Guid>();
public HashSet<Guid> ElementContentTypes { get; } = new HashSet<Guid>();

private Dictionary<string, string> _replacementAliases =
public Dictionary<string, string> ReplacementAliases { get; } =
new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

public Dictionary<string, string> BlockAliases { get; }
= new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

/// <summary>
/// Add a content type key to the context.
/// </summary>
Expand All @@ -44,8 +46,8 @@ public void AddAliasAndKey(string? contentTypeAlias, Guid? contentTypeKey)
{
_ = string.IsNullOrWhiteSpace(contentTypeAlias) == false &&
contentTypeKey.HasValue == true &&
_contentTypeAliases.TryAdd(contentTypeKey.Value, contentTypeAlias) &&
_contentTypeKeys.TryAdd(contentTypeAlias, contentTypeKey.Value);
ContentTypeAliases.TryAdd(contentTypeKey.Value, contentTypeAlias) &&
ContentTypeKeys.TryAdd(contentTypeAlias, contentTypeKey.Value);
}

/// <summary>
Expand All @@ -54,21 +56,21 @@ public void AddAliasAndKey(string? contentTypeAlias, Guid? contentTypeKey)
/// <param name="contentTypeKey"></param>
/// <returns>alias of a content type</returns>
public string GetAliasByKey(Guid contentTypeKey)
=> _contentTypeAliases?.TryGetValue(contentTypeKey, out var alias) == true ? alias : string.Empty;
=> ContentTypeAliases?.TryGetValue(contentTypeKey, out var alias) == true ? alias : string.Empty;

/// <summary>
/// get the key for a given content type alias from the context.
/// </summary>
/// <returns>GUID Key value for a content type</returns>
public Guid GetKeyByAlias(string contentTypeAlias)
=> _contentTypeKeys?.TryGetValue(contentTypeAlias, out var key) == true ? key : Guid.Empty;
=> ContentTypeKeys?.TryGetValue(contentTypeAlias, out var key) == true ? key : Guid.Empty;

/// <summary>
/// return all the content aliases we have loaded in the context.
/// </summary>
/// <returns>array of aliases</returns>
public string[] GetAllAliases()
=> _contentTypeAliases?.Values?.ToArray() ?? Array.Empty<string>();
=> ContentTypeAliases?.Values?.ToArray() ?? Array.Empty<string>();

/// <summary>
/// add content type compositions to the context
Expand All @@ -77,7 +79,7 @@ public void AddCompositions(string? contentTypeAlias, IEnumerable<string>? compo
{
_ = string.IsNullOrWhiteSpace(contentTypeAlias) == false &&
compositionAliases?.Any() == true &&
_contentTypeCompositions.TryAdd(contentTypeAlias, compositionAliases.ToHashSet());
ContentTypeCompositions.TryAdd(contentTypeAlias, compositionAliases.ToHashSet());
}

/// <summary>
Expand All @@ -90,15 +92,14 @@ public bool TryGetCompositionsByAlias(string? contentTypeAlias, out IEnumerable<
{
compositionAliases = null;

if (contentTypeAlias != null && _contentTypeCompositions.TryGetValue(contentTypeAlias, out var compositions))
if (contentTypeAlias != null && ContentTypeCompositions.TryGetValue(contentTypeAlias, out var compositions))
{
compositionAliases = compositions?.ToArray();
}

return compositionAliases != null;
}


/// <summary>
/// Add a editorAlias mapping for a property mapping to the context.
/// </summary>
Expand All @@ -112,7 +113,7 @@ public void AddProperty(string? contentTypeAlias, string? propertyAlias, string?
string.IsNullOrWhiteSpace(propertyAlias) == false &&
string.IsNullOrWhiteSpace(originalAlias) == false &&
string.IsNullOrWhiteSpace(newAlias) == false &&
_propertyTypes.TryAdd($"{contentTypeAlias}_{propertyAlias}",
PropertyTypes.TryAdd($"{contentTypeAlias}_{propertyAlias}",
new EditorAliasInfo(originalAlias, newAlias, dataTypeDefinition));
}

Expand All @@ -129,7 +130,7 @@ public void AddProperty(string? contentTypeAlias, string? propertyAlias, string?
/// </remarks>
public EditorAliasInfo? GetEditorAliasByTypeAndProperty(string contentType, string propertyAlias)
{
if (_propertyTypes?.TryGetValue($"{contentType}_{propertyAlias}", out var alias) == true)
if (PropertyTypes?.TryGetValue($"{contentType}_{propertyAlias}", out var alias) == true)
{
return alias;
}
Expand All @@ -146,11 +147,11 @@ public void AddProperty(string? contentTypeAlias, string? propertyAlias, string?
/// </summary>
private bool TryGetEditorAliasByComposition(string compositionKey, string propertyAlias, out EditorAliasInfo? editorAliasInfo)
{
if (_contentTypeCompositions?.TryGetValue(compositionKey, out var compositions) == true)
if (ContentTypeCompositions?.TryGetValue(compositionKey, out var compositions) == true)
{
foreach (var composition in compositions)
{
if (_propertyTypes?.TryGetValue($"{composition}_{propertyAlias}", out var alias) == true)
if (PropertyTypes?.TryGetValue($"{composition}_{propertyAlias}", out var alias) == true)
{
editorAliasInfo = alias;
return true;
Expand All @@ -170,14 +171,14 @@ private bool TryGetEditorAliasByComposition(string compositionKey, string proper
/// <summary>
/// tells us if a content type is an element type
/// </summary>
public bool IsElementType(Guid key) => _elementContentTypes.Contains(key);
public bool IsElementType(Guid key) => ElementContentTypes.Contains(key);

/// <summary>
/// add an element type to the list of element types.
/// </summary>
public void AddElementType(Guid key)
{
if (!_elementContentTypes.Contains(key)) _elementContentTypes.Add(key);
if (!ElementContentTypes.Contains(key)) ElementContentTypes.Add(key);
}

/// <summary>
Expand Down Expand Up @@ -214,65 +215,60 @@ public void AddElementTypes(IEnumerable<Guid> contentTypeKeys, bool includeCompo
}
}


/// <summary>
/// ignore a property on a specific content type.
/// </summary>
/// <remarks>
/// note this is the final content type, will not calculate compositions.
/// </remarks>
public void AddIgnoredProperty(string contentType, string alias)
=> _ = _ignoredProperties.Add($"{contentType}_{alias}");
=> _ = IgnoredProperties.Add($"{contentType}_{alias}");

/// <summary>
/// add a property to ignore for all content types.
/// </summary>
public void AddIgnoredProperty(string alias)
=> _ = _ignoredProperties.Add($"{alias}");
=> _ = IgnoredProperties.Add($"{alias}");

/// <summary>
/// returns true if a property is to be ignored
/// </summary>
public bool IsIgnoredProperty(string contentType, string alias)
=> _ignoredProperties.Contains($"{contentType}_{alias}")
|| _ignoredProperties.Contains(alias);
=> IgnoredProperties.Contains($"{contentType}_{alias}")
|| IgnoredProperties.Contains(alias);

/// <summary>
/// add a new content type - will then be processed as part of the
/// migration process.
/// </summary>
public void AddNewContentType(NewContentTypeInfo newDocTypeInfo)
{
if (!_newDocTypes.ContainsKey(newDocTypeInfo.Alias))
_newDocTypes.Add(newDocTypeInfo.Alias, newDocTypeInfo);
if (!NewDocTypes.ContainsKey(newDocTypeInfo.Alias))
NewDocTypes.Add(newDocTypeInfo.Alias, newDocTypeInfo);
}

/// <summary>
/// list of all the new content types to be created.
/// </summary>
/// <returns></returns>
public IList<NewContentTypeInfo> GetNewContentTypes()
=> _newDocTypes.Values.ToList();


private Dictionary<string, string> _blockAliases
= new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
=> NewDocTypes.Values.ToList();

/// <summary>
/// add a block editor to by name
/// </summary>
/// <param name="name"></param>
/// <param name="alias"></param>
public void AddBlockEditor(string name, string alias)
=> _blockAliases.TryAdd(name, alias);
=> BlockAliases.TryAdd(name, alias);

/// <summary>
/// get the block editor alias from the name.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public string GetBlockEditorAliasByName(string name)
=> _blockAliases.TryGetValue(name, out var alias) == true
=> BlockAliases.TryGetValue(name, out var alias) == true
? alias
: name;

Expand All @@ -293,7 +289,7 @@ public List<TabOptions> GetChangedTabs()
/// add a mapping for a content types property to the named datatype.
/// </summary>
public void AddDataTypeAlias(string contentTypeAlias, string propertyAlias, string dataTypeAlias)
=> _ = _dataTypeAliases.TryAdd($"{contentTypeAlias}_{propertyAlias}", dataTypeAlias);
=> _ = DataTypeAliases.TryAdd($"{contentTypeAlias}_{propertyAlias}", dataTypeAlias);

/// <summary>
/// get the datatype alias for a property on a content type.
Expand All @@ -302,21 +298,21 @@ public void AddDataTypeAlias(string contentTypeAlias, string propertyAlias, stri
/// <param name="propertyAlias"></param>
/// <returns></returns>
public string GetDataTypeAlias(string contentTypeAlias, string propertyAlias)
=> _dataTypeAliases.TryGetValue($"{contentTypeAlias}_{propertyAlias}", out var alias) == true
=> DataTypeAliases.TryGetValue($"{contentTypeAlias}_{propertyAlias}", out var alias) == true
? alias : string.Empty;

/// <summary>
/// Add a replacement alias for a content type alias
/// </summary>
public void AddReplacementAlias(string original, string replacement)
=> _replacementAliases.TryAdd(original, replacement);
=> ReplacementAliases.TryAdd(original, replacement);

/// <summary>
/// get the replacement alias for an property based on the current alias.
/// </summary>
/// <param name="alias"></param>
/// <returns></returns>
public string GetReplacementAlias(string alias)
=> _replacementAliases.TryGetValue(alias, out var replacement)
=> ReplacementAliases.TryGetValue(alias, out var replacement)
? replacement : alias;
}
26 changes: 13 additions & 13 deletions uSync.Migrations.Core/Context/DataTypeMigrationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,80 +10,80 @@ public class DataTypeMigrationContext
/// <summary>
/// list of keys to editor aliases used to lookup datatypes in content types !
/// </summary>
private Dictionary<Guid, DataTypeInfo> _dataTypeDefinitions { get; set; } = new();
public Dictionary<Guid, DataTypeInfo> DataTypeDefinitions { get; } = new();

/// <summary>
/// when we replace an datatype with something else .
/// </summary>
private Dictionary<Guid, Guid> _dataTypeReplacements { get; set; } = new();
public Dictionary<Guid, Guid> DataTypeReplacements { get; } = new();

/// <summary>
/// datatypes that vary by something (e.g culture)
/// </summary>
private Dictionary<Guid, string> _dataTypeVariations { get; set; } = new();
public Dictionary<Guid, string> DataTypeVariations { get; } = new();


/// <summary>
/// contains a lookup from defition (guid) to alias, so we can pass that along.
/// </summary>
private Dictionary<Guid, string> _dataTypeAliases { get; set; } = new();
public Dictionary<Guid, string> DataTypeAliases { get; } = new();

/// <summary>
/// add a datatype alias to the lookup
/// </summary>
/// <param name="dtd"></param>
/// <param name="alias"></param>
public void AddAlias(Guid dtd, string alias)
=> _ = _dataTypeAliases.TryAdd(dtd, alias);
=> _ = DataTypeAliases.TryAdd(dtd, alias);

/// <summary>
/// get the alias based on the DTD value (which we have in contenttype).
/// </summary>
/// <param name="dtd"></param>
/// <returns></returns>
public string GetAlias(Guid dtd)
=> _dataTypeAliases?.TryGetValue(dtd, out var alias) == true
=> DataTypeAliases?.TryGetValue(dtd, out var alias) == true
? alias : string.Empty;

/// <summary>
/// add a datatypedefinion (aka datatype key) to the context.
/// </summary>
public void AddDefinition(Guid dtd, DataTypeInfo def)
=> _ = _dataTypeDefinitions.TryAdd(dtd, def);
=> _ = DataTypeDefinitions.TryAdd(dtd, def);

/// <summary>
/// get a datatype definiton from the context.
/// </summary>
public DataTypeInfo? GetByDefinition(Guid guid)
=> _dataTypeDefinitions?.TryGetValue(guid, out var def) == true
=> DataTypeDefinitions?.TryGetValue(guid, out var def) == true
? def
: null;

/// <summary>
/// add the key that replaces a datatype to the context.
/// </summary>
public void AddReplacement(Guid original, Guid replacement)
=> _ = _dataTypeReplacements.TryAdd(original, replacement);
=> _ = DataTypeReplacements.TryAdd(original, replacement);

/// <summary>
/// get any replacement key values for a given datatype key
/// </summary>
public Guid GetReplacement(Guid original)
=> _dataTypeReplacements?.TryGetValue(original, out var replacement) == true
=> DataTypeReplacements?.TryGetValue(original, out var replacement) == true
? replacement
: original;

/// <summary>
/// add a variation (e.g culture, segment or nothing) value for a datatype to the context.
/// </summary>
public void AddVariation(Guid guid, string variation)
=> _ = _dataTypeVariations?.TryAdd(guid, variation);
=> _ = DataTypeVariations?.TryAdd(guid, variation);

/// <summary>
/// retrieve the variation that a datatype will ask a doctype property to perform.
/// </summary>
public string GetVariation(Guid guid, string defaultValue)
=> _dataTypeVariations?.TryGetValue(guid, out var variation) == true
=> DataTypeVariations?.TryGetValue(guid, out var variation) == true
? variation : defaultValue;

/// <summary>
Expand All @@ -93,7 +93,7 @@ public string GetVariation(Guid guid, string defaultValue)
/// <returns></returns>
public Guid? GetFirstDefinition(string alias)
{
var dataTypeDefinition = _dataTypeDefinitions?.FirstOrDefault(x => x.Value.EditorAlias == alias);
var dataTypeDefinition = DataTypeDefinitions?.FirstOrDefault(x => x.Value.EditorAlias == alias);
return dataTypeDefinition?.Key ?? null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
using uSync.Migrations.Core.Services;

namespace uSync.Migrations.Core.Handlers.Eight;
internal class ContentBaseMigrationHandler<TEntity> : SharedContentBaseHandler<TEntity>

public class ContentBaseMigrationHandler<TEntity> : SharedContentBaseHandler<TEntity>
where TEntity : ContentBase
{
public ContentBaseMigrationHandler(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace uSync.Migrations.Core.Handlers.Eight;
SourceVersion = 8,
SourceFolderName = "Content",
TargetFolderName = "Content")]
internal class ContentMigrationHandler : ContentBaseMigrationHandler<Content>, ISyncMigrationHandler
public class ContentMigrationHandler : ContentBaseMigrationHandler<Content>, ISyncMigrationHandler
{
public ContentMigrationHandler(
IEventAggregator eventAggregator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
using uSync.Migrations.Core.Services;

namespace uSync.Migrations.Core.Handlers.Eight;
internal class ContentTypeBaseMigrationHandler<TEntity> : SharedContentTypeBaseHandler<TEntity>

public class ContentTypeBaseMigrationHandler<TEntity> : SharedContentTypeBaseHandler<TEntity>
where TEntity : ContentTypeBase
{
public ContentTypeBaseMigrationHandler(
Expand Down
Loading