Skip to content

Commit

Permalink
improving source sql server compatibility, refactored queries for use…
Browse files Browse the repository at this point in the history
…r, cms tree, cms document
  • Loading branch information
tkrch committed Aug 25, 2024
1 parent ebd26e5 commit 2000973
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Collections.Immutable;

using CMS.Base;
using CMS.MediaLibrary;

Expand Down Expand Up @@ -46,27 +44,42 @@ public async Task<CommandResult> Handle(MigrateMediaLibrariesCommand request, Ca
var skippedMediaLibraries = new HashSet<Guid>();
var unsuitableMediaLibraries =
modelFacade.Select("""
SELECT LibraryName, STRING_AGG(CAST(LibraryGUID AS NVARCHAR(max)), '|') as [LibraryGUIDs]
FROM Media_Library
GROUP BY LibraryName
HAVING COUNT(*) > 1
SELECT LibraryName, LibraryGUID FROM Media_Library [ML]
WHERE EXISTS(
SELECT 1
FROM Media_Library [MLI]
WHERE MLI.LibraryName = ML.LibraryName
GROUP BY LibraryName
HAVING COUNT(*) > 1
)
""",
(reader, _) => new { LibraryName = reader.Unbox<string>("LibraryName"), LibraryGuids = reader.Unbox<string?>("LibraryGUIDs")?.Split('|').Select(Guid.Parse).ToImmutableList() ?? [] });
(reader, _) => new
{
LibraryName = reader.Unbox<string>("LibraryName"),
LibraryGuid = reader.Unbox<Guid?>("LibraryGUID")
});

foreach (var mlg in unsuitableMediaLibraries)
var groupedMls = unsuitableMediaLibraries
.GroupBy(x => x.LibraryName)
.Select(x => new { LibraryGuids = x.Select(y => y.LibraryGuid).ToArray(), LibraryName = x.Key });

foreach (var mlg in groupedMls)
{
logger.LogError(
"Media libraries with LibraryGuid ({LibraryGuids}) have same LibraryName '{LibraryName}', due to removal of sites and media library globalization it is required to set unique LibraryName and LibraryFolder",
string.Join(",", mlg.LibraryGuids), mlg.LibraryName);

foreach (var libraryGuid in mlg.LibraryGuids)
{
skippedMediaLibraries.Add(libraryGuid);
if (libraryGuid is { } lg)
{
skippedMediaLibraries.Add(lg);

protocol.Append(HandbookReferences.NotCurrentlySupportedSkip()
.WithMessage($"Media library '{mlg.LibraryName}' with LibraryGuid '{libraryGuid}' doesn't satisfy unique LibraryName and LibraryFolder condition for migration")
.WithData(new { LibraryGuid = libraryGuid, mlg.LibraryName })
);
protocol.Append(HandbookReferences.NotCurrentlySupportedSkip()
.WithMessage($"Media library '{mlg.LibraryName}' with LibraryGuid '{libraryGuid}' doesn't satisfy unique LibraryName and LibraryFolder condition for migration")
.WithData(new { LibraryGuid = libraryGuid, mlg.LibraryName })
);
}
}
}

Expand Down
55 changes: 33 additions & 22 deletions KVA/Migration.Toolkit.Source/Services/SpoiledGuidContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,49 @@ public class SpoiledGuidContext(ModelFacade modelFacade, ILogger<SpoiledGuidCont

internal IDictionary<Guid, ImmutableList<SpoiledDocumentGuidInfo>> SpoiledDocumentGuids =>
spoiledDocumentGuids ??= modelFacade.Select("""
SELECT DocumentGUID, STRING_AGG(CONCAT(NodeSiteID, '-', NodeID), '|') [SiteID-NodeID]
SELECT DocumentGUID, NodeSiteID, NodeID
FROM View_CMS_Tree_Joined TJ
GROUP BY DocumentGUID
HAVING COUNT(DocumentID) > 1
WHERE EXISTS(
SELECT 1
FROM View_CMS_Tree_Joined TJI
WHERE TJI.DocumentGUID = TJ.DocumentGUID
GROUP BY DocumentGUID
HAVING COUNT(DocumentID) > 1
)
""",
(reader, version) => new { DocumentGUID = reader.Unbox<Guid>("DocumentGuid"), Info = reader.Unbox<string>("SiteID-NodeID") })
.ToFrozenDictionary(
x => x.DocumentGUID,
x => x.Info.Split('|').Select(i =>
(reader, version) => new
{
string[] spl = i.Split('-');

return new SpoiledDocumentGuidInfo(int.Parse(spl[0]), int.Parse(spl[1]));
}).ToImmutableList()
DocumentGUID = reader.Unbox<Guid>("DocumentGUID"),
NodeID = reader.Unbox<int>("NodeID"),
NodeSiteID = reader.Unbox<int>("NodeSiteID"),
})
.GroupBy(x => x.DocumentGUID)
.ToFrozenDictionary(
x => x.Key,
x => x.Select(i => new SpoiledDocumentGuidInfo(i.NodeSiteID, i.NodeID)).ToImmutableList()
);

internal IDictionary<Guid, ImmutableList<SpoiledNodeGuidInfo>> SpoiledNodeGuids =>
spoiledNodeGuids ??= modelFacade.Select("""
SELECT NodeGUID, STRING_AGG(NodeSiteID, '|') [SiteID]
SELECT NodeGUID, NodeSiteID
FROM View_CMS_Tree_Joined TJ
GROUP BY NodeGUID
HAVING COUNT(NodeGUID) > 1
WHERE EXISTS (
SELECT 1
FROM View_CMS_Tree_Joined TJI
WHERE TJI.NodeGUID = TJ.NodeGUID
GROUP BY NodeGUID
HAVING COUNT(NodeGUID) > 1
)
""",
(reader, version) => new { DocumentGUID = reader.Unbox<Guid>("NodeGUID"), Info = reader.Unbox<string>("SiteID") })
.ToFrozenDictionary(
x => x.DocumentGUID,
x => x.Info.Split('|').Select(i =>
(reader, version) => new
{
string[] spl = i.Split('-');

return new SpoiledNodeGuidInfo(int.Parse(spl[0]));
}).ToImmutableList()
NodeGUID = reader.Unbox<Guid>("NodeGUID"),
NodeSiteID = reader.Unbox<int>("NodeSiteID")
})
.GroupBy(x => x.NodeGUID)
.ToFrozenDictionary(
x => x.Key,
x => x.Select(i => new SpoiledNodeGuidInfo(i.NodeSiteID)).ToImmutableList()
);

public Guid EnsureDocumentGuid(Guid documentGuid, int siteId, int nodeId, int documentId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
using Migration.Toolkit.Core.K11.Mappers;
using Migration.Toolkit.K11;
using Migration.Toolkit.K11.Models;
using Migration.Toolkit.KXP.Api.Auxiliary;
using Migration.Toolkit.KXP.Api.Enums;

namespace Migration.Toolkit.Core.K11.Handlers;

Expand All @@ -37,7 +37,7 @@ public async Task<CommandResult> Handle(MigrateMembersCommand request, Cancellat

var k11CmsUsers = k11Context.CmsUsers
.Include(u => u.CmsUserSettingUserSettingsUserNavigation)
.Where(u => UserHelper.PrivilegeLevelsMigratedAsMemberUser.Contains(u.UserPrivilegeLevel))
.Where(u => u.UserPrivilegeLevel == (int)UserPrivilegeLevelEnum.None)
;

foreach (var k11User in k11CmsUsers)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using Migration.Toolkit.Core.K11.Contexts;
using Migration.Toolkit.K11;
using Migration.Toolkit.K11.Models;
using Migration.Toolkit.KXP.Api.Auxiliary;
using Migration.Toolkit.KXP.Api.Enums;

namespace Migration.Toolkit.Core.K11.Handlers;
Expand All @@ -38,7 +37,7 @@ public async Task<CommandResult> Handle(MigrateUsersCommand request, Cancellatio
await using var k11Context = await k11ContextFactory.CreateDbContextAsync(cancellationToken);

var k11CmsUsers = k11Context.CmsUsers
.Where(u => UserHelper.PrivilegeLevelsMigratedAsAdminUser.Contains(u.UserPrivilegeLevel))
.Where(u => u.UserPrivilegeLevel == (int)UserPrivilegeLevelEnum.Admin || u.UserPrivilegeLevel == (int)UserPrivilegeLevelEnum.Editor || u.UserPrivilegeLevel == (int)UserPrivilegeLevelEnum.GlobalAdmin)
;

foreach (var k11User in k11CmsUsers)
Expand Down Expand Up @@ -127,7 +126,7 @@ private async Task MigrateUserCmsRoles(K11Context k11Context, CancellationToken
{
var groupedRoles = k11Context.CmsRoles
.Where(r =>
r.CmsUserRoles.Any(ur => UserHelper.PrivilegeLevelsMigratedAsAdminUser.Contains(ur.User.UserPrivilegeLevel))
r.CmsUserRoles.Any(ur => ur.User.UserPrivilegeLevel == (int)UserPrivilegeLevelEnum.Editor || ur.User.UserPrivilegeLevel == (int)UserPrivilegeLevelEnum.Admin || ur.User.UserPrivilegeLevel == (int)UserPrivilegeLevelEnum.GlobalAdmin)
)
.GroupBy(x => x.RoleName)
.AsNoTracking();
Expand Down Expand Up @@ -157,7 +156,7 @@ Roles with RoleGuid ({RoleGuids}) have same RoleName '{RoleName}', due to remova

var k11CmsRoles = k11Context.CmsRoles
.Where(r =>
r.CmsUserRoles.Any(ur => UserHelper.PrivilegeLevelsMigratedAsAdminUser.Contains(ur.User.UserPrivilegeLevel))
r.CmsUserRoles.Any(ur => ur.User.UserPrivilegeLevel == (int)UserPrivilegeLevelEnum.Editor || ur.User.UserPrivilegeLevel == (int)UserPrivilegeLevelEnum.Admin || ur.User.UserPrivilegeLevel == (int)UserPrivilegeLevelEnum.GlobalAdmin)
)
.AsNoTracking()
.AsAsyncEnumerable();
Expand Down Expand Up @@ -215,8 +214,9 @@ private async Task MigrateUserRole(int k11RoleId)
var k11Context = await k11ContextFactory.CreateDbContextAsync();
var k11UserRoles = k11Context.CmsUserRoles
.Where(ur =>
ur.RoleId == k11RoleId &&
UserHelper.PrivilegeLevelsMigratedAsAdminUser.Contains(ur.User.UserPrivilegeLevel)
ur.RoleId == k11RoleId && (
ur.User.UserPrivilegeLevel == (int)UserPrivilegeLevelEnum.Editor || ur.User.UserPrivilegeLevel == (int)UserPrivilegeLevelEnum.Admin || ur.User.UserPrivilegeLevel == (int)UserPrivilegeLevelEnum.GlobalAdmin
)
)
.AsNoTracking()
.AsAsyncEnumerable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
using Migration.Toolkit.Core.KX12.Contexts;
using Migration.Toolkit.Core.KX12.Mappers;
using Migration.Toolkit.KX12.Context;
using Migration.Toolkit.KXP.Api.Auxiliary;
using Migration.Toolkit.KXP.Api.Enums;

namespace Migration.Toolkit.Core.KX12.Handlers;
Expand All @@ -39,7 +38,7 @@ public async Task<CommandResult> Handle(MigrateMembersCommand request, Cancellat

var k12CmsUsers = kx12Context.CmsUsers
.Include(u => u.CmsUserSettingUserSettingsUserNavigation)
.Where(u => UserHelper.PrivilegeLevelsMigratedAsMemberUser.Contains(u.UserPrivilegeLevel))
.Where(u => u.UserPrivilegeLevel == (int)UserPrivilegeLevelEnum.None)
;

foreach (var k12User in k12CmsUsers)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using Migration.Toolkit.Common.MigrationProtocol;
using Migration.Toolkit.Core.KX12.Contexts;
using Migration.Toolkit.KX12.Context;
using Migration.Toolkit.KXP.Api.Auxiliary;
using Migration.Toolkit.KXP.Api.Enums;
using Migration.Toolkit.KXP.Models;

Expand All @@ -38,7 +37,7 @@ public async Task<CommandResult> Handle(MigrateUsersCommand request, Cancellatio
await using var kx12Context = await kx12ContextFactory.CreateDbContextAsync(cancellationToken);

var k12CmsUsers = kx12Context.CmsUsers
.Where(u => UserHelper.PrivilegeLevelsMigratedAsAdminUser.Contains(u.UserPrivilegeLevel))
.Where(u => u.UserPrivilegeLevel == (int)UserPrivilegeLevelEnum.Admin || u.UserPrivilegeLevel == (int)UserPrivilegeLevelEnum.Editor || u.UserPrivilegeLevel == (int)UserPrivilegeLevelEnum.GlobalAdmin)
;

foreach (var k12User in k12CmsUsers)
Expand Down Expand Up @@ -124,7 +123,7 @@ private async Task MigrateUserCmsRoles(KX12Context kx12Context, CancellationToke
{
var k12CmsRoles = kx12Context.CmsRoles
.Where(r =>
r.CmsUserRoles.Any(ur => UserHelper.PrivilegeLevelsMigratedAsAdminUser.Contains(ur.User.UserPrivilegeLevel))
r.CmsUserRoles.Any(ur => ur.User.UserPrivilegeLevel == (int)UserPrivilegeLevelEnum.Editor || ur.User.UserPrivilegeLevel == (int)UserPrivilegeLevelEnum.Admin || ur.User.UserPrivilegeLevel == (int)UserPrivilegeLevelEnum.GlobalAdmin)
)
.AsNoTracking()
.AsAsyncEnumerable();
Expand Down Expand Up @@ -177,8 +176,9 @@ private async Task MigrateUserRole(int k12RoleId)
var kx12Context = await kx12ContextFactory.CreateDbContextAsync();
var k12UserRoles = kx12Context.CmsUserRoles
.Where(ur =>
ur.RoleId == k12RoleId &&
UserHelper.PrivilegeLevelsMigratedAsAdminUser.Contains(ur.User.UserPrivilegeLevel)
ur.RoleId == k12RoleId && (
ur.User.UserPrivilegeLevel == (int)UserPrivilegeLevelEnum.Editor || ur.User.UserPrivilegeLevel == (int)UserPrivilegeLevelEnum.Admin || ur.User.UserPrivilegeLevel == (int)UserPrivilegeLevelEnum.GlobalAdmin
)
)
.AsNoTracking()
.AsAsyncEnumerable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
using Migration.Toolkit.Core.KX13.Contexts;
using Migration.Toolkit.Core.KX13.Mappers;
using Migration.Toolkit.KX13.Context;
using Migration.Toolkit.KXP.Api.Auxiliary;
using Migration.Toolkit.KXP.Api.Enums;

namespace Migration.Toolkit.Core.KX13.Handlers;
Expand All @@ -39,7 +38,7 @@ public async Task<CommandResult> Handle(MigrateMembersCommand request, Cancellat

var kx13CmsUsers = kx13Context.CmsUsers
.Include(u => u.CmsUserSettingUserSettingsUserNavigation)
.Where(u => UserHelper.PrivilegeLevelsMigratedAsMemberUser.Contains(u.UserPrivilegeLevel))
.Where(u => u.UserPrivilegeLevel == (int)UserPrivilegeLevelEnum.None)
;

foreach (var kx13User in kx13CmsUsers)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using Migration.Toolkit.Common.MigrationProtocol;
using Migration.Toolkit.Core.KX13.Contexts;
using Migration.Toolkit.KX13.Context;
using Migration.Toolkit.KXP.Api.Auxiliary;
using Migration.Toolkit.KXP.Api.Enums;
using Migration.Toolkit.KXP.Models;

Expand All @@ -38,7 +37,7 @@ public async Task<CommandResult> Handle(MigrateUsersCommand request, Cancellatio
await using var kx13Context = await kx13ContextFactory.CreateDbContextAsync(cancellationToken);

var kx13CmsUsers = kx13Context.CmsUsers
.Where(u => UserHelper.PrivilegeLevelsMigratedAsAdminUser.Contains(u.UserPrivilegeLevel))
.Where(u => u.UserPrivilegeLevel == (int)UserPrivilegeLevelEnum.Admin || u.UserPrivilegeLevel == (int)UserPrivilegeLevelEnum.Editor || u.UserPrivilegeLevel == (int)UserPrivilegeLevelEnum.GlobalAdmin)
;

foreach (var kx13User in kx13CmsUsers)
Expand Down Expand Up @@ -127,7 +126,7 @@ private async Task MigrateUserCmsRoles(KX13Context kx13Context, CancellationToke
{
var kx13CmsRoles = kx13Context.CmsRoles
.Where(r =>
r.CmsUserRoles.Any(ur => UserHelper.PrivilegeLevelsMigratedAsAdminUser.Contains(ur.User.UserPrivilegeLevel))
r.CmsUserRoles.Any(ur => ur.User.UserPrivilegeLevel == (int)UserPrivilegeLevelEnum.Editor || ur.User.UserPrivilegeLevel == (int)UserPrivilegeLevelEnum.Admin || ur.User.UserPrivilegeLevel == (int)UserPrivilegeLevelEnum.GlobalAdmin)
)
.AsNoTracking()
.AsAsyncEnumerable();
Expand Down Expand Up @@ -181,7 +180,7 @@ private async Task MigrateUserRole(int kx13RoleId)
var kx13UserRoles = kx13Context.CmsUserRoles
.Where(ur =>
ur.RoleId == kx13RoleId &&
UserHelper.PrivilegeLevelsMigratedAsAdminUser.Contains(ur.User.UserPrivilegeLevel)
(ur.User.UserPrivilegeLevel == (int)UserPrivilegeLevelEnum.Editor || ur.User.UserPrivilegeLevel == (int)UserPrivilegeLevelEnum.Admin || ur.User.UserPrivilegeLevel == (int)UserPrivilegeLevelEnum.GlobalAdmin)
)
.AsNoTracking()
.AsAsyncEnumerable();
Expand Down

0 comments on commit 2000973

Please sign in to comment.