mirror of
https://github.com/bitwarden/server.git
synced 2025-07-03 17:12:49 -05:00
[AC-1682] Moved data migration scripts to DbScripts_transition folder
This commit is contained in:
@ -1,27 +0,0 @@
|
||||
-- Create a temporary table to store the groups with AccessAll = 1
|
||||
SELECT [Id] AS [GroupId], [OrganizationId]
|
||||
INTO #TempGroup
|
||||
FROM [dbo].[Group]
|
||||
WHERE [AccessAll] = 1;
|
||||
|
||||
-- Update existing rows in [dbo].[CollectionGroup]
|
||||
UPDATE CG
|
||||
SET
|
||||
CG.[ReadOnly] = 0,
|
||||
CG.[HidePasswords] = 0,
|
||||
CG.[Manage] = 1
|
||||
FROM [dbo].[CollectionGroup] CG
|
||||
INNER JOIN [dbo].[Collection] C ON CG.[CollectionId] = C.[Id]
|
||||
INNER JOIN #TempGroup TG ON CG.[GroupId] = TG.[GroupId]
|
||||
WHERE C.[OrganizationId] = TG.[OrganizationId];
|
||||
|
||||
-- Insert new rows into [dbo].[CollectionGroup]
|
||||
INSERT INTO [dbo].[CollectionGroup] ([CollectionId], [GroupId], [ReadOnly], [HidePasswords], [Manage])
|
||||
SELECT C.[Id], TG.[GroupId], 0, 0, 1
|
||||
FROM [dbo].[Collection] C
|
||||
JOIN #TempGroup TG ON C.[OrganizationId] = TG.[OrganizationId]
|
||||
LEFT JOIN [dbo].[CollectionGroup] CG ON CG.[CollectionId] = C.[Id] AND CG.[GroupId] = TG.[GroupId]
|
||||
WHERE CG.[CollectionId] IS NULL;
|
||||
|
||||
-- Drop the temporary table
|
||||
DROP TABLE #TempGroup;
|
@ -1,45 +0,0 @@
|
||||
-- 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], CAST(ROW_NUMBER() OVER(ORDER BY [Id]) / 50000 AS INT) AS Batch
|
||||
INTO #TempOrgUser
|
||||
FROM [dbo].[OrganizationUser]
|
||||
WHERE [AccessAll] = 1;
|
||||
|
||||
-- Step 2: Get the maximum batch number
|
||||
DECLARE @MaxBatch INT = (SELECT MAX(Batch) FROM #TempOrgUser);
|
||||
DECLARE @CurrentBatch INT = 0;
|
||||
|
||||
-- Step 3: Process each batch
|
||||
WHILE @CurrentBatch <= @MaxBatch
|
||||
BEGIN
|
||||
-- Update existing rows in [dbo].[CollectionUser]
|
||||
UPDATE target
|
||||
SET
|
||||
target.[ReadOnly] = 0,
|
||||
target.[HidePasswords] = 0,
|
||||
target.[Manage] = 1
|
||||
FROM [dbo].[CollectionUser] AS target
|
||||
JOIN (
|
||||
SELECT C.[Id] AS [CollectionId], T.[OrganizationUserId]
|
||||
FROM [dbo].[Collection] C
|
||||
JOIN #TempOrgUser T ON C.[OrganizationId] = T.[OrganizationId] AND T.Batch = @CurrentBatch
|
||||
) AS source
|
||||
ON target.[CollectionId] = source.[CollectionId] AND target.[OrganizationUserId] = source.[OrganizationUserId];
|
||||
|
||||
-- Insert new rows into [dbo].[CollectionUser]
|
||||
INSERT INTO [dbo].[CollectionUser] ([CollectionId], [OrganizationUserId], [ReadOnly], [HidePasswords], [Manage])
|
||||
SELECT source.[CollectionId], source.[OrganizationUserId], 0, 0, 1
|
||||
FROM (
|
||||
SELECT C.[Id] AS [CollectionId], T.[OrganizationUserId]
|
||||
FROM [dbo].[Collection] C
|
||||
JOIN #TempOrgUser T ON C.[OrganizationId] = T.[OrganizationId] AND T.Batch = @CurrentBatch
|
||||
) AS source
|
||||
LEFT JOIN [dbo].[CollectionUser] AS target
|
||||
ON target.[CollectionId] = source.[CollectionId] AND target.[OrganizationUserId] = source.[OrganizationUserId]
|
||||
WHERE target.[CollectionId] IS NULL;
|
||||
|
||||
-- Move to the next batch
|
||||
SET @CurrentBatch = @CurrentBatch + 1;
|
||||
END;
|
||||
|
||||
-- Step 4: Drop the temporary table
|
||||
DROP TABLE #TempOrgUser;
|
@ -1,12 +0,0 @@
|
||||
-- Update [dbo].[CollectionUser] with [Manage] = 1 for all users with Manager role or 'EditAssignedCollections' permission
|
||||
UPDATE cu
|
||||
SET cu.[ReadOnly] = 0,
|
||||
cu.[HidePasswords] = 0,
|
||||
cu.[Manage] = 1
|
||||
FROM [dbo].[CollectionUser] cu
|
||||
JOIN [dbo].[Collection] c
|
||||
ON cu.[CollectionId] = c.[Id]
|
||||
JOIN [dbo].[OrganizationUser] ou
|
||||
ON cu.[OrganizationUserId] = ou.[Id]
|
||||
WHERE (ou.[Type] = 3 OR (ou.[Permissions] IS NOT NULL AND
|
||||
ISJSON(ou.[Permissions]) > 0 AND JSON_VALUE(ou.[Permissions], '$.editAssignedCollections') = 'true'))
|
Reference in New Issue
Block a user