From e14ba4ddb171f68c5d1661edae394d32e3ae89c6 Mon Sep 17 00:00:00 2001 From: Rui Tome Date: Fri, 10 Nov 2023 15:28:39 +0000 Subject: [PATCH] [AC-1682] Data migrations for OrgUsers or Groups with AccessAll enabled --- ...023-11-10_00_AccessAllCollectionGroups.sql | 45 +++++++++++++++++++ ...2023-11-10_00_AccessAllCollectionUsers.sql | 45 +++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 util/Migrator/DbScripts/2023-11-10_00_AccessAllCollectionGroups.sql create mode 100644 util/Migrator/DbScripts/2023-11-10_00_AccessAllCollectionUsers.sql diff --git a/util/Migrator/DbScripts/2023-11-10_00_AccessAllCollectionGroups.sql b/util/Migrator/DbScripts/2023-11-10_00_AccessAllCollectionGroups.sql new file mode 100644 index 0000000000..61c2a757da --- /dev/null +++ b/util/Migrator/DbScripts/2023-11-10_00_AccessAllCollectionGroups.sql @@ -0,0 +1,45 @@ +-- Step 1: Retrieve relevant data from [dbo].[Group] where [AccessAll] is 1 +SELECT [Id] AS [GroupId], [OrganizationId] +INTO #TempGroup +FROM [dbo].[Group] +WHERE [AccessAll] = 1; + +-- Step 2: Declare variables for group and organization IDs +DECLARE @GroupId UNIQUEIDENTIFIER; +DECLARE @OrganizationId UNIQUEIDENTIFIER; + +-- Step 3: Create a cursor to iterate through the results of the temporary table +DECLARE GroupCursor CURSOR FOR +SELECT [GroupId], [OrganizationId] +FROM #TempGroup; + +OPEN GroupCursor; + +-- Step 4: Loop through the groups +FETCH NEXT FROM GroupCursor INTO @GroupId, @OrganizationId; + +WHILE @@FETCH_STATUS = 0 +BEGIN + -- Step 5: Use MERGE to insert or update into [dbo].[CollectionGroup] for each [dbo].[Collection] entry +MERGE INTO [dbo].[CollectionGroup] AS target + USING (SELECT C.[Id] AS [CollectionId], @GroupId AS [GroupId] FROM [dbo].[Collection] C WHERE C.[OrganizationId] = @OrganizationId) AS source + ON (target.[CollectionId] = source.[CollectionId] AND target.[GroupId] = source.[GroupId]) + WHEN MATCHED THEN +UPDATE SET + target.[ReadOnly] = 0, + target.[HidePasswords] = 0, + target.[Manage] = 1 + WHEN NOT MATCHED THEN +INSERT ([CollectionId], [GroupId], [ReadOnly], [HidePasswords], [Manage]) +VALUES (source.[CollectionId], source.[GroupId], 0, 0, 1); + +-- Step 6: Fetch the next group and organization IDs +FETCH NEXT FROM GroupCursor INTO @GroupId, @OrganizationId; +END; + +-- Step 7: Close and deallocate the cursor +CLOSE GroupCursor; +DEALLOCATE GroupCursor; + +-- Step 8: Drop the temporary table +DROP TABLE #TempGroup; diff --git a/util/Migrator/DbScripts/2023-11-10_00_AccessAllCollectionUsers.sql b/util/Migrator/DbScripts/2023-11-10_00_AccessAllCollectionUsers.sql new file mode 100644 index 0000000000..9901e19fc5 --- /dev/null +++ b/util/Migrator/DbScripts/2023-11-10_00_AccessAllCollectionUsers.sql @@ -0,0 +1,45 @@ +-- Step 1: Retrieve relevant data from [dbo].[OrganizationUser] where [AccessAll] is 1 +SELECT [Id] AS [OrganizationUserId], [OrganizationId] +INTO #TempOrgUser +FROM [dbo].[OrganizationUser] +WHERE [AccessAll] = 1; + +-- Step 2: Declare variables for organization user and organization ID +DECLARE @OrgUserId UNIQUEIDENTIFIER; +DECLARE @OrganizationId UNIQUEIDENTIFIER; + +-- Step 3: Create a cursor to iterate through the results of the temporary table +DECLARE OrgUserCursor CURSOR FOR +SELECT [OrganizationUserId], [OrganizationId] +FROM #TempOrgUser; + +OPEN OrgUserCursor; + +-- Step 4: Loop through the organization users +FETCH NEXT FROM OrgUserCursor INTO @OrgUserId, @OrganizationId; + +WHILE @@FETCH_STATUS = 0 +BEGIN + -- Step 5: Use MERGE to insert or update into [dbo].[CollectionUser] for each [dbo].[Collection] entry +MERGE INTO [dbo].[CollectionUser] AS target + USING (SELECT C.[Id] AS [CollectionId], @OrgUserId AS [OrganizationUserId] FROM [dbo].[Collection] C WHERE C.[OrganizationId] = @OrganizationId) AS source -- Adjusted to use OrganizationId + ON (target.[CollectionId] = source.[CollectionId] AND target.[OrganizationUserId] = source.[OrganizationUserId]) + WHEN MATCHED THEN +UPDATE SET + target.[ReadOnly] = 0, + target.[HidePasswords] = 0, + target.[Manage] = 1 + WHEN NOT MATCHED THEN +INSERT ([CollectionId], [OrganizationUserId], [ReadOnly], [HidePasswords], [Manage]) +VALUES (source.[CollectionId], source.[OrganizationUserId], 0, 0, 1); + +-- Step 6: Fetch the next organization user and organization ID +FETCH NEXT FROM OrgUserCursor INTO @OrgUserId, @OrganizationId; +END; + +-- Step 7: Close and deallocate the cursor +CLOSE OrgUserCursor; +DEALLOCATE OrgUserCursor; + +-- Step 8: Drop the temporary table +DROP TABLE #TempOrgUser;