mirror of
https://github.com/bitwarden/server.git
synced 2025-07-03 09:02:48 -05:00
[AC-1682] Added back batching system to AccessAllCollectionUsers data migration script
This commit is contained in:
@ -1,34 +1,45 @@
|
|||||||
-- Step 1: Create a temporary table to store the OrganizationUsers with AccessAll = 1
|
-- Step 1: Insert into a temporary table with an additional column for batch processing, update 50 k at a time
|
||||||
SELECT [Id] AS [OrganizationUserId], [OrganizationId]
|
SELECT [Id] AS [OrganizationUserId], [OrganizationId], CAST(ROW_NUMBER() OVER(ORDER BY [Id]) / 50000 AS INT) AS Batch
|
||||||
INTO #TempOrgUser
|
INTO #TempOrgUser
|
||||||
FROM [dbo].[OrganizationUser]
|
FROM [dbo].[OrganizationUser]
|
||||||
WHERE [AccessAll] = 1;
|
WHERE [AccessAll] = 1;
|
||||||
|
|
||||||
-- Step 2: Update existing rows in [dbo].[CollectionUser]
|
-- Step 2: Get the maximum batch number
|
||||||
UPDATE target
|
DECLARE @MaxBatch INT = (SELECT MAX(Batch) FROM #TempOrgUser);
|
||||||
SET
|
DECLARE @CurrentBatch INT = 0;
|
||||||
target.[ReadOnly] = 0,
|
|
||||||
target.[HidePasswords] = 0,
|
-- Step 3: Process each batch
|
||||||
target.[Manage] = 0
|
WHILE @CurrentBatch <= @MaxBatch
|
||||||
FROM [dbo].[CollectionUser] AS target
|
BEGIN
|
||||||
INNER JOIN (
|
-- Update existing rows in [dbo].[CollectionUser]
|
||||||
SELECT C.[Id] AS [CollectionId], T.[OrganizationUserId]
|
UPDATE target
|
||||||
FROM [dbo].[Collection] C
|
SET
|
||||||
INNER JOIN #TempOrgUser T ON C.[OrganizationId] = T.[OrganizationId]
|
target.[ReadOnly] = 0,
|
||||||
) AS source
|
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] AND T.Batch = @CurrentBatch
|
||||||
|
) AS source
|
||||||
ON target.[CollectionId] = source.[CollectionId] AND target.[OrganizationUserId] = source.[OrganizationUserId];
|
ON target.[CollectionId] = source.[CollectionId] AND target.[OrganizationUserId] = source.[OrganizationUserId];
|
||||||
|
|
||||||
-- Step 3: Insert new rows into [dbo].[CollectionUser]
|
-- Insert new rows into [dbo].[CollectionUser]
|
||||||
INSERT INTO [dbo].[CollectionUser] ([CollectionId], [OrganizationUserId], [ReadOnly], [HidePasswords], [Manage])
|
INSERT INTO [dbo].[CollectionUser] ([CollectionId], [OrganizationUserId], [ReadOnly], [HidePasswords], [Manage])
|
||||||
SELECT source.[CollectionId], source.[OrganizationUserId], 0, 0, 0
|
SELECT source.[CollectionId], source.[OrganizationUserId], 0, 0, 0
|
||||||
FROM (
|
FROM (
|
||||||
SELECT C.[Id] AS [CollectionId], T.[OrganizationUserId]
|
SELECT C.[Id] AS [CollectionId], T.[OrganizationUserId]
|
||||||
FROM [dbo].[Collection] C
|
FROM [dbo].[Collection] C
|
||||||
INNER JOIN #TempOrgUser T ON C.[OrganizationId] = T.[OrganizationId]
|
INNER JOIN #TempOrgUser T ON C.[OrganizationId] = T.[OrganizationId] AND T.Batch = @CurrentBatch
|
||||||
) AS source
|
) AS source
|
||||||
LEFT JOIN [dbo].[CollectionUser] AS target
|
LEFT JOIN [dbo].[CollectionUser] AS target
|
||||||
ON target.[CollectionId] = source.[CollectionId] AND target.[OrganizationUserId] = source.[OrganizationUserId]
|
ON target.[CollectionId] = source.[CollectionId] AND target.[OrganizationUserId] = source.[OrganizationUserId]
|
||||||
WHERE target.[CollectionId] IS NULL;
|
WHERE target.[CollectionId] IS NULL;
|
||||||
|
|
||||||
|
-- Move to the next batch
|
||||||
|
SET @CurrentBatch = @CurrentBatch + 1;
|
||||||
|
END;
|
||||||
|
|
||||||
-- Step 4: Update [dbo].[OrganizationUser] to clear AccessAll flag
|
-- Step 4: Update [dbo].[OrganizationUser] to clear AccessAll flag
|
||||||
UPDATE OU
|
UPDATE OU
|
||||||
|
Reference in New Issue
Block a user