-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved the migration lookups db calls into their own repository class. (…
…#7) Added a new class that extends MigrationLookupsRepository, which will store migration relations into the Umbraco Runtime Cache. Added a new app setting to disable this feature.
- Loading branch information
Showing
8 changed files
with
285 additions
and
79 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
26 changes: 23 additions & 3 deletions
26
src/Method4.UmbracoMigrator.Target.Core/Options/MigratorTargetSettings.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 |
---|---|---|
@@ -1,8 +1,28 @@ | ||
namespace Method4.UmbracoMigrator.Target.Core.Options | ||
using System.ComponentModel; | ||
|
||
namespace Method4.UmbracoMigrator.Target.Core.Options | ||
{ | ||
public class MigratorTargetSettings | ||
{ | ||
public bool EnableMediaRedirectGeneration { get; set; } | ||
/// <summary> | ||
/// For use with the Skybrud Redirects package. | ||
/// Enables the UniqueMediaPathScheme IMediaPathScheme. | ||
/// This will automatically generate a re-direct if a migrated media item's file is changed. | ||
/// Please see the documentation for more information. | ||
/// </summary> | ||
[DefaultValue(false)] | ||
public bool EnableMediaRedirectGeneration { get; set; } = false; | ||
|
||
/// <summary> | ||
/// The property alias of the Media File Upload property. | ||
/// </summary> | ||
[DefaultValue("umbracoFile")] | ||
public string? MediaFileUploadPropertyAlias { get; set; } | ||
|
||
/// <summary> | ||
/// Enable the Runtime Caching on the MigrationLookups repository | ||
/// </summary> | ||
[DefaultValue(true)] | ||
public bool EnableMigrationLookupTableRuntimeCaching { get; set; } = true; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Method4.UmbracoMigrator.Target.Core/Repositories/IMigrationLookupsRepository.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,11 @@ | ||
using Method4.UmbracoMigrator.Target.Core.Models.DataModels; | ||
|
||
namespace Method4.UmbracoMigrator.Target.Core.Repositories; | ||
|
||
public interface IMigrationLookupsRepository | ||
{ | ||
NodeRelation? Get(string columnName, string value); | ||
void Insert(string newId, string oldId, string newKey, string oldKey); | ||
int Count(); | ||
void DeleteAll(); | ||
} |
94 changes: 94 additions & 0 deletions
94
src/Method4.UmbracoMigrator.Target.Core/Repositories/MigrationLookupsRepository.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,94 @@ | ||
using Method4.UmbracoMigrator.Target.Core.CustomDbTables.NPoco; | ||
using Method4.UmbracoMigrator.Target.Core.Models.DataModels; | ||
using Microsoft.Extensions.Logging; | ||
using Umbraco.Cms.Core.Mapping; | ||
using Umbraco.Cms.Infrastructure.Scoping; | ||
|
||
namespace Method4.UmbracoMigrator.Target.Core.Repositories | ||
{ | ||
public partial class MigrationLookupsRepository : IMigrationLookupsRepository | ||
{ | ||
private readonly IUmbracoMapper _umbracoMapper; | ||
private readonly IScopeProvider _scopeProvider; | ||
private readonly ILogger<MigrationLookupsRepository> _logger; | ||
private const string TableName = "MigrationLookups"; | ||
|
||
public MigrationLookupsRepository(IUmbracoMapper umbracoMapper, IScopeProvider scopeProvider, ILogger<MigrationLookupsRepository> logger) | ||
{ | ||
_umbracoMapper = umbracoMapper; | ||
_scopeProvider = scopeProvider; | ||
_logger = logger; | ||
} | ||
|
||
public virtual NodeRelation? Get(string columnName, string value) | ||
{ | ||
using var scope = _scopeProvider.CreateScope(); | ||
var queryResult = scope.Database.Fetch<NodeRelationLookupPoco>($"SELECT * From {TableName} WHERE {columnName} = @0", value); | ||
scope.Complete(); | ||
|
||
var foundLookup = queryResult.FirstOrDefault(); // There should only ever be 1 | ||
if (foundLookup == null) | ||
{ | ||
_logger.LogDebug("Lookup Not found for {lookupType}: {lookupValue}", columnName, value); | ||
return null; | ||
} | ||
|
||
var relation = _umbracoMapper.Map<NodeRelation>(foundLookup); | ||
if (relation == null) | ||
{ | ||
_logger.LogError("Unable to map NodeRelationLookupPoco to NodeRelation for {lookupType}: {value}", columnName, value); | ||
throw new Exception($"Lookup for {columnName}: '{value}' could not be mapped, result from the Umbraco Mapper was null"); | ||
} | ||
|
||
return relation; | ||
} | ||
|
||
public virtual void Insert(string newId, string oldId, string newKey, string oldKey) | ||
{ | ||
var relationLookup = new NodeRelationLookupPoco() | ||
{ | ||
NewId = newId, | ||
OldId = oldId, | ||
NewKey = newKey, | ||
OldKey = oldKey | ||
}; | ||
|
||
try | ||
{ | ||
using var scope = _scopeProvider.CreateScope(); | ||
scope.Database.Insert<NodeRelationLookupPoco>(relationLookup); | ||
scope.Complete(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Failed to store new migration node relation"); | ||
throw; | ||
} | ||
} | ||
|
||
public virtual int Count() | ||
{ | ||
using var scope = _scopeProvider.CreateScope(); | ||
var queryResult = scope.Database.Fetch<NodeRelationLookupPoco>($"SELECT * From {TableName}"); | ||
scope.Complete(); | ||
|
||
return queryResult.Count; | ||
} | ||
|
||
public virtual void DeleteAll() | ||
{ | ||
_logger.LogInformation("Deleting all Key relations"); | ||
try | ||
{ | ||
using var scope = _scopeProvider.CreateScope(); | ||
var queryResult = scope.Database.Execute($"DELETE FROM {TableName}"); | ||
scope.Complete(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Failed to delete all relations from the {tableName} table", TableName); | ||
throw; | ||
} | ||
} | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
src/Method4.UmbracoMigrator.Target.Core/Repositories/MigrationLookupsRepositoryWithCache.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,118 @@ | ||
using Method4.UmbracoMigrator.Target.Core.Models.DataModels; | ||
using Microsoft.Extensions.Logging; | ||
using Umbraco.Cms.Core.Cache; | ||
using Umbraco.Cms.Core.Mapping; | ||
using Umbraco.Cms.Infrastructure.Scoping; | ||
using Umbraco.Extensions; | ||
|
||
namespace Method4.UmbracoMigrator.Target.Core.Repositories | ||
{ | ||
public class MigrationLookupsRepositoryWithCache : MigrationLookupsRepository | ||
{ | ||
private readonly IAppPolicyCache _runtimeCache; | ||
private readonly ILogger<MigrationLookupsRepositoryWithCache> _logger; | ||
|
||
private const string CacheKey = "MigrationRelationLookup"; | ||
private const int CacheTimeoutDays = 7; | ||
|
||
public MigrationLookupsRepositoryWithCache( | ||
AppCaches appCaches, | ||
IUmbracoMapper umbracoMapper, | ||
IScopeProvider scopeProvider, | ||
ILogger<MigrationLookupsRepository> logger, | ||
ILogger<MigrationLookupsRepositoryWithCache> logger2) | ||
: base(umbracoMapper, scopeProvider, logger) | ||
{ | ||
_runtimeCache = appCaches.RuntimeCache; | ||
_logger = logger2; | ||
} | ||
|
||
public override NodeRelation? Get(string columnName, string value) | ||
{ | ||
var relation = _runtimeCache.GetCacheItem<NodeRelation>($"{CacheKey}_{columnName}_{value}"); | ||
if (relation != null) { return relation; } | ||
|
||
relation = base.Get(columnName, value); | ||
if (relation == null) { return relation; } | ||
|
||
InsertIntoRuntimeCache(relation); | ||
return relation; | ||
} | ||
|
||
public override void Insert(string newId, string oldId, string newKey, string oldKey) | ||
{ | ||
base.Insert(newId, oldId, newKey, oldKey); | ||
var relationLookup = base.Get("NewId", newId); | ||
if (relationLookup != null) | ||
{ | ||
InsertIntoRuntimeCache(relationLookup); | ||
} | ||
} | ||
|
||
public override void DeleteAll() | ||
{ | ||
base.DeleteAll(); | ||
try | ||
{ | ||
_runtimeCache.ClearOfType<NodeRelation>(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Failed to remove Migration Relations from the Umbraco runtime cache"); | ||
throw; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Adds the node relation to the runtime cache if it doesn't exist. | ||
/// </summary> | ||
/// <param name="relation"></param> | ||
private void InsertIntoRuntimeCache(NodeRelation relation) | ||
{ | ||
var newIdCacheKey = $"{CacheKey}_NewId_{relation.NewId}"; | ||
var oldIdCacheKey = $"{CacheKey}_OldId_{relation.OldId}"; | ||
var newKeyCacheKey = $"{CacheKey}_NewKey_{relation.NewKeyAsString}"; | ||
var oldKeyCacheKey = $"{CacheKey}_OldKey_{relation.OldKeyAsString}"; | ||
|
||
try | ||
{ | ||
if (_runtimeCache.GetCacheItem<NodeRelation>(newIdCacheKey) == null) | ||
{ | ||
_runtimeCache.GetCacheItem( | ||
newIdCacheKey, | ||
() => relation, | ||
TimeSpan.FromDays(CacheTimeoutDays)); | ||
} | ||
|
||
if (_runtimeCache.GetCacheItem<NodeRelation>(oldIdCacheKey) == null) | ||
{ | ||
_runtimeCache.GetCacheItem( | ||
oldIdCacheKey, | ||
() => relation, | ||
TimeSpan.FromDays(CacheTimeoutDays)); | ||
} | ||
|
||
if (_runtimeCache.GetCacheItem<NodeRelation>(newKeyCacheKey) == null) | ||
{ | ||
_runtimeCache.GetCacheItem( | ||
newKeyCacheKey, | ||
() => relation, | ||
TimeSpan.FromDays(CacheTimeoutDays)); | ||
} | ||
|
||
if (_runtimeCache.GetCacheItem<NodeRelation>(oldKeyCacheKey) == null) | ||
{ | ||
_runtimeCache.GetCacheItem( | ||
oldKeyCacheKey, | ||
() => relation, | ||
TimeSpan.FromDays(CacheTimeoutDays)); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Failed to insert Migration Relation into the Umbraco runtime cache"); | ||
throw; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.