From 200097372470323ad5dd373b49473fd7ab5c2837 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Krch?= Date: Sun, 25 Aug 2024 23:33:36 +0200 Subject: [PATCH] improving source sql server compatibility, refactored queries for user, cms tree, cms document --- .../MigrateMediaLibrariesCommandHandler.cs | 39 ++++++++----- .../Services/SpoiledGuidContext.cs | 55 +++++++++++-------- .../Handlers/MigrateMembersCommandHandler.cs | 4 +- .../Handlers/MigrateUsersCommandHandler.cs | 12 ++-- .../Handlers/MigrateMembersCommandHandler.cs | 3 +- .../Handlers/MigrateUsersCommandHandler.cs | 10 ++-- .../Handlers/MigrateMembersCommandHandler.cs | 3 +- .../Handlers/MigrateUsersCommandHandler.cs | 7 +-- 8 files changed, 77 insertions(+), 56 deletions(-) diff --git a/KVA/Migration.Toolkit.Source/Handlers/MigrateMediaLibrariesCommandHandler.cs b/KVA/Migration.Toolkit.Source/Handlers/MigrateMediaLibrariesCommandHandler.cs index 6d55a960..fedd3ae2 100644 --- a/KVA/Migration.Toolkit.Source/Handlers/MigrateMediaLibrariesCommandHandler.cs +++ b/KVA/Migration.Toolkit.Source/Handlers/MigrateMediaLibrariesCommandHandler.cs @@ -1,5 +1,3 @@ -using System.Collections.Immutable; - using CMS.Base; using CMS.MediaLibrary; @@ -46,14 +44,26 @@ public async Task Handle(MigrateMediaLibrariesCommand request, Ca var skippedMediaLibraries = new HashSet(); 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("LibraryName"), LibraryGuids = reader.Unbox("LibraryGUIDs")?.Split('|').Select(Guid.Parse).ToImmutableList() ?? [] }); + (reader, _) => new + { + LibraryName = reader.Unbox("LibraryName"), + LibraryGuid = reader.Unbox("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", @@ -61,12 +71,15 @@ HAVING COUNT(*) > 1 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 }) + ); + } } } diff --git a/KVA/Migration.Toolkit.Source/Services/SpoiledGuidContext.cs b/KVA/Migration.Toolkit.Source/Services/SpoiledGuidContext.cs index bef4de56..a65750cb 100644 --- a/KVA/Migration.Toolkit.Source/Services/SpoiledGuidContext.cs +++ b/KVA/Migration.Toolkit.Source/Services/SpoiledGuidContext.cs @@ -24,38 +24,49 @@ public class SpoiledGuidContext(ModelFacade modelFacade, ILogger> 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("DocumentGuid"), Info = reader.Unbox("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("DocumentGUID"), + NodeID = reader.Unbox("NodeID"), + NodeSiteID = reader.Unbox("NodeSiteID"), + }) + .GroupBy(x => x.DocumentGUID) + .ToFrozenDictionary( + x => x.Key, + x => x.Select(i => new SpoiledDocumentGuidInfo(i.NodeSiteID, i.NodeID)).ToImmutableList() ); internal IDictionary> 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("NodeGUID"), Info = reader.Unbox("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("NodeGUID"), + NodeSiteID = reader.Unbox("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) diff --git a/Migration.Toolkit.Core.K11/Handlers/MigrateMembersCommandHandler.cs b/Migration.Toolkit.Core.K11/Handlers/MigrateMembersCommandHandler.cs index 057f97f3..4992bdcf 100644 --- a/Migration.Toolkit.Core.K11/Handlers/MigrateMembersCommandHandler.cs +++ b/Migration.Toolkit.Core.K11/Handlers/MigrateMembersCommandHandler.cs @@ -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; @@ -37,7 +37,7 @@ public async Task 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) diff --git a/Migration.Toolkit.Core.K11/Handlers/MigrateUsersCommandHandler.cs b/Migration.Toolkit.Core.K11/Handlers/MigrateUsersCommandHandler.cs index 15c08e45..a3496412 100644 --- a/Migration.Toolkit.Core.K11/Handlers/MigrateUsersCommandHandler.cs +++ b/Migration.Toolkit.Core.K11/Handlers/MigrateUsersCommandHandler.cs @@ -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; @@ -38,7 +37,7 @@ public async Task 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) @@ -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(); @@ -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(); @@ -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(); diff --git a/Migration.Toolkit.Core.KX12/Handlers/MigrateMembersCommandHandler.cs b/Migration.Toolkit.Core.KX12/Handlers/MigrateMembersCommandHandler.cs index a0f1b766..ae977144 100644 --- a/Migration.Toolkit.Core.KX12/Handlers/MigrateMembersCommandHandler.cs +++ b/Migration.Toolkit.Core.KX12/Handlers/MigrateMembersCommandHandler.cs @@ -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; @@ -39,7 +38,7 @@ public async Task 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) diff --git a/Migration.Toolkit.Core.KX12/Handlers/MigrateUsersCommandHandler.cs b/Migration.Toolkit.Core.KX12/Handlers/MigrateUsersCommandHandler.cs index b0c2f475..2783eaea 100644 --- a/Migration.Toolkit.Core.KX12/Handlers/MigrateUsersCommandHandler.cs +++ b/Migration.Toolkit.Core.KX12/Handlers/MigrateUsersCommandHandler.cs @@ -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; @@ -38,7 +37,7 @@ public async Task 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) @@ -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(); @@ -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(); diff --git a/Migration.Toolkit.Core.KX13/Handlers/MigrateMembersCommandHandler.cs b/Migration.Toolkit.Core.KX13/Handlers/MigrateMembersCommandHandler.cs index 8dbf7b11..f64a4c63 100644 --- a/Migration.Toolkit.Core.KX13/Handlers/MigrateMembersCommandHandler.cs +++ b/Migration.Toolkit.Core.KX13/Handlers/MigrateMembersCommandHandler.cs @@ -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; @@ -39,7 +38,7 @@ public async Task 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) diff --git a/Migration.Toolkit.Core.KX13/Handlers/MigrateUsersCommandHandler.cs b/Migration.Toolkit.Core.KX13/Handlers/MigrateUsersCommandHandler.cs index 582eefcf..01e1937f 100644 --- a/Migration.Toolkit.Core.KX13/Handlers/MigrateUsersCommandHandler.cs +++ b/Migration.Toolkit.Core.KX13/Handlers/MigrateUsersCommandHandler.cs @@ -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; @@ -38,7 +37,7 @@ public async Task 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) @@ -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(); @@ -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();