1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-04 01:22:50 -05:00

Revert "[AC-1682] Deleted old data migration scripts"

This reverts commit 54cc6fab8f.
This commit is contained in:
Rui Tome
2024-01-11 11:55:16 +00:00
parent 54cc6fab8f
commit a214c60268
3 changed files with 199 additions and 0 deletions

View File

@ -0,0 +1,62 @@
-- Step 1: Create a temporary table to store the OrganizationUsers with AccessAll = 1
SELECT [Id] AS [OrganizationUserId], [OrganizationId]
INTO #TempOrgUser
FROM [dbo].[OrganizationUser]
WHERE [AccessAll] = 1;
-- Step 2: Update existing rows in [dbo].[CollectionUser]
UPDATE target
SET
target.[ReadOnly] = 0,
target.[HidePasswords] = 0,
target.[Manage] = 0
FROM [dbo].[CollectionUser] AS target
INNER JOIN (
SELECT C.[Id] AS [CollectionId], T.[OrganizationUserId]
FROM [dbo].[Collection] C
INNER JOIN #TempOrgUser T ON C.[OrganizationId] = T.[OrganizationId]
) AS source
ON target.[CollectionId] = source.[CollectionId] AND target.[OrganizationUserId] = source.[OrganizationUserId];
-- Step 3: Insert new rows into [dbo].[CollectionUser]
INSERT INTO [dbo].[CollectionUser] ([CollectionId], [OrganizationUserId], [ReadOnly], [HidePasswords], [Manage])
SELECT source.[CollectionId], source.[OrganizationUserId], 0, 0, 0
FROM (
SELECT C.[Id] AS [CollectionId], T.[OrganizationUserId]
FROM [dbo].[Collection] C
INNER JOIN #TempOrgUser T ON C.[OrganizationId] = T.[OrganizationId]
) AS source
LEFT JOIN [dbo].[CollectionUser] AS target
ON target.[CollectionId] = source.[CollectionId] AND target.[OrganizationUserId] = source.[OrganizationUserId]
WHERE target.[CollectionId] IS NULL;
-- Step 4: Update [dbo].[OrganizationUser] to clear AccessAll flag
UPDATE OU
SET [AccessAll] = 0
FROM [dbo].[OrganizationUser] OU
INNER JOIN #TempOrgUser T ON OU.[Id] = T.[OrganizationUserId]
-- Step 5: Bump the account revision date for each unique OrganizationId in #TempOrgUser
DECLARE @OrganizationId UNIQUEIDENTIFIER
DECLARE OrgIdCursor CURSOR FOR
SELECT DISTINCT [OrganizationId]
FROM #TempOrgUser
OPEN OrgIdCursor
FETCH NEXT FROM OrgIdCursor INTO @OrganizationId
WHILE (@@FETCH_STATUS = 0)
BEGIN
-- Execute the stored procedure for the current OrganizationId
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationId] @OrganizationId
-- Fetch the next OrganizationId
FETCH NEXT FROM OrgIdCursor INTO @OrganizationId
END
CLOSE OrgIdCursor
DEALLOCATE OrgIdCursor;
-- Step 6: Drop the temporary table
DROP TABLE #TempOrgUser;