mirror of
https://github.com/bitwarden/server.git
synced 2025-07-01 16:12:49 -05:00
[AC-1682] Deleted old data migration scripts
This commit is contained in:
@ -1,57 +0,0 @@
|
|||||||
-- Step 1: Create a temporary table to store the Groups with AccessAll = 1
|
|
||||||
SELECT [Id] AS [GroupId], [OrganizationId]
|
|
||||||
INTO #TempGroup
|
|
||||||
FROM [dbo].[Group]
|
|
||||||
WHERE [AccessAll] = 1;
|
|
||||||
|
|
||||||
-- Step 2: Update existing rows in [dbo].[CollectionGroup]
|
|
||||||
UPDATE CG
|
|
||||||
SET
|
|
||||||
CG.[ReadOnly] = 0,
|
|
||||||
CG.[HidePasswords] = 0,
|
|
||||||
CG.[Manage] = 0
|
|
||||||
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];
|
|
||||||
|
|
||||||
-- Step 3: Insert new rows into [dbo].[CollectionGroup]
|
|
||||||
INSERT INTO [dbo].[CollectionGroup] ([CollectionId], [GroupId], [ReadOnly], [HidePasswords], [Manage])
|
|
||||||
SELECT C.[Id], TG.[GroupId], 0, 0, 0
|
|
||||||
FROM [dbo].[Collection] C
|
|
||||||
INNER 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;
|
|
||||||
|
|
||||||
-- Step 4: Update [dbo].[Group] to clear AccessAll flag
|
|
||||||
UPDATE G
|
|
||||||
SET [AccessAll] = 0
|
|
||||||
FROM [dbo].[Group] G
|
|
||||||
INNER JOIN #TempGroup TG ON G.[Id] = TG.[GroupId]
|
|
||||||
|
|
||||||
-- Step 5: Bump the account revision date for each unique CollectionId and OrganizationId
|
|
||||||
DECLARE @CollectionId UNIQUEIDENTIFIER
|
|
||||||
DECLARE @OrganizationId UNIQUEIDENTIFIER
|
|
||||||
|
|
||||||
DECLARE CollectionIdCursor CURSOR FOR
|
|
||||||
SELECT DISTINCT C.[Id], C.[OrganizationId]
|
|
||||||
FROM [dbo].[Collection] C
|
|
||||||
INNER JOIN #TempGroup TG ON C.[OrganizationId] = TG.[OrganizationId]
|
|
||||||
|
|
||||||
OPEN CollectionIdCursor
|
|
||||||
FETCH NEXT FROM CollectionIdCursor INTO @CollectionId, @OrganizationId
|
|
||||||
|
|
||||||
WHILE (@@FETCH_STATUS = 0)
|
|
||||||
BEGIN
|
|
||||||
-- Execute the stored procedure for the current CollectionId and OrganizationId
|
|
||||||
EXEC [dbo].[User_BumpAccountRevisionDateByCollectionId] @CollectionId, @OrganizationId
|
|
||||||
|
|
||||||
-- Fetch the next CollectionId and OrganizationId
|
|
||||||
FETCH NEXT FROM CollectionIdCursor INTO @CollectionId, @OrganizationId
|
|
||||||
END
|
|
||||||
|
|
||||||
CLOSE CollectionIdCursor
|
|
||||||
DEALLOCATE CollectionIdCursor;
|
|
||||||
|
|
||||||
-- Step 6: Drop the temporary table
|
|
||||||
DROP TABLE #TempGroup;
|
|
@ -1,73 +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] = 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];
|
|
||||||
|
|
||||||
-- 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] 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: 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 OrganizationUserId in #TempOrgUser
|
|
||||||
DECLARE @OrganizationUserId UNIQUEIDENTIFIER
|
|
||||||
|
|
||||||
DECLARE OrgUserIdCursor CURSOR FOR
|
|
||||||
SELECT DISTINCT [OrganizationUserId]
|
|
||||||
FROM #TempOrgUser
|
|
||||||
|
|
||||||
OPEN OrgUserIdCursor
|
|
||||||
FETCH NEXT FROM OrgUserIdCursor INTO @OrganizationUserId
|
|
||||||
|
|
||||||
WHILE (@@FETCH_STATUS = 0)
|
|
||||||
BEGIN
|
|
||||||
-- Execute the stored procedure for the current OrganizationUserId
|
|
||||||
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserId] @OrganizationUserId
|
|
||||||
|
|
||||||
-- Fetch the next OrganizationUserId
|
|
||||||
FETCH NEXT FROM OrgUserIdCursor INTO @OrganizationUserId
|
|
||||||
END
|
|
||||||
|
|
||||||
CLOSE OrgUserIdCursor
|
|
||||||
DEALLOCATE OrgUserIdCursor;
|
|
||||||
|
|
||||||
-- Step 6: Drop the temporary table
|
|
||||||
DROP TABLE #TempOrgUser;
|
|
@ -1,82 +0,0 @@
|
|||||||
-- Step 1: Update [dbo].[CollectionUser] with [Manage] = 1 for all users with Manager role or 'EditAssignedCollections' permission
|
|
||||||
-- Store the results in a temporary table
|
|
||||||
SELECT ou.[Id] AS [OrganizationUserId]
|
|
||||||
INTO #TempStep1
|
|
||||||
FROM [dbo].[OrganizationUser] ou
|
|
||||||
WHERE (ou.[Type] = 3 OR (ou.[Permissions] IS NOT NULL AND
|
|
||||||
ISJSON(ou.[Permissions]) > 0 AND JSON_VALUE(ou.[Permissions], '$.editAssignedCollections') = 'true'));
|
|
||||||
|
|
||||||
-- Update [dbo].[CollectionUser] with [Manage] = 1 using the temporary table
|
|
||||||
UPDATE cu
|
|
||||||
SET cu.[ReadOnly] = 0,
|
|
||||||
cu.[HidePasswords] = 0,
|
|
||||||
cu.[Manage] = 1
|
|
||||||
FROM [dbo].[CollectionUser] cu
|
|
||||||
INNER JOIN #TempStep1 temp ON cu.[OrganizationUserId] = temp.[OrganizationUserId];
|
|
||||||
|
|
||||||
-- Step 2: Insert rows to [dbo].[CollectionUser] for Managers and users with 'EditAssignedCollections' permission assigned to groups with collection access
|
|
||||||
-- Store the results in a temporary table
|
|
||||||
SELECT cg.[CollectionId], ou.[Id] AS [OrganizationUserId]
|
|
||||||
INTO #TempStep2
|
|
||||||
FROM [dbo].[CollectionGroup] cg
|
|
||||||
INNER JOIN [dbo].[GroupUser] gu ON cg.GroupId = gu.GroupId
|
|
||||||
INNER JOIN [dbo].[OrganizationUser] ou ON gu.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'))
|
|
||||||
AND NOT EXISTS (
|
|
||||||
SELECT 1 FROM [dbo].[CollectionUser] cu
|
|
||||||
WHERE cu.[CollectionId] = cg.[CollectionId] AND cu.[OrganizationUserId] = ou.[Id]
|
|
||||||
);
|
|
||||||
|
|
||||||
-- Insert rows into [dbo].[CollectionUser] using the temporary table
|
|
||||||
INSERT INTO [dbo].[CollectionUser] ([CollectionId], [OrganizationUserId], [ReadOnly], [HidePasswords], [Manage])
|
|
||||||
SELECT [CollectionId], [OrganizationUserId], 0, 0, 1
|
|
||||||
FROM #TempStep2;
|
|
||||||
|
|
||||||
-- Step 3: Set all Managers to Users
|
|
||||||
-- Store the results in a temporary table
|
|
||||||
SELECT [Id] AS [OrganizationUserId]
|
|
||||||
INTO #TempStep3
|
|
||||||
FROM [dbo].[OrganizationUser]
|
|
||||||
WHERE [Type] = 3; -- Manager
|
|
||||||
|
|
||||||
-- Update [dbo].[OrganizationUser] based on the temporary table
|
|
||||||
UPDATE ou
|
|
||||||
SET ou.[Type] = 2 -- User
|
|
||||||
FROM [dbo].[OrganizationUser] ou
|
|
||||||
INNER JOIN #TempStep3 temp ON ou.[Id] = temp.[OrganizationUserId];
|
|
||||||
|
|
||||||
-- Step 4: Bump the account revision date for each unique OrganizationUserId in #TempStep1, #TempStep2, and #TempStep3
|
|
||||||
-- Join the three temporary tables to get unique OrganizationUserId
|
|
||||||
SELECT DISTINCT temp1.[OrganizationUserId]
|
|
||||||
INTO #TempUniqueOrganizationUser
|
|
||||||
FROM #TempStep1 temp1
|
|
||||||
JOIN #TempStep2 temp2 ON temp1.[OrganizationUserId] = temp2.[OrganizationUserId]
|
|
||||||
JOIN #TempStep3 temp3 ON temp1.[OrganizationUserId] = temp3.[OrganizationUserId];
|
|
||||||
|
|
||||||
-- Execute User_BumpAccountRevisionDateByOrganizationUserId for each unique OrganizationUserId
|
|
||||||
DECLARE @OrganizationUserId UNIQUEIDENTIFIER
|
|
||||||
|
|
||||||
DECLARE UniqueOrgUserIdCursor CURSOR FOR
|
|
||||||
SELECT [OrganizationUserId]
|
|
||||||
FROM #TempUniqueOrganizationUser
|
|
||||||
|
|
||||||
OPEN UniqueOrgUserIdCursor
|
|
||||||
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @OrganizationUserId
|
|
||||||
|
|
||||||
WHILE (@@FETCH_STATUS = 0)
|
|
||||||
BEGIN
|
|
||||||
-- Execute the stored procedure for the current OrganizationUserId
|
|
||||||
EXEC [dbo].[User_BumpAccountRevisionDateByOrganizationUserId] @OrganizationUserId
|
|
||||||
|
|
||||||
-- Fetch the next row
|
|
||||||
FETCH NEXT FROM UniqueOrgUserIdCursor INTO @OrganizationUserId
|
|
||||||
END
|
|
||||||
|
|
||||||
CLOSE UniqueOrgUserIdCursor
|
|
||||||
DEALLOCATE UniqueOrgUserIdCursor;
|
|
||||||
|
|
||||||
-- Step 5: Clean up temporary tables
|
|
||||||
DROP TABLE #TempStep1;
|
|
||||||
DROP TABLE #TempStep2;
|
|
||||||
DROP TABLE #TempStep3;
|
|
||||||
DROP TABLE #TempUniqueOrganizationUser;
|
|
@ -1,28 +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 [OrganizationId], CAST(ROW_NUMBER() OVER(ORDER BY [Id]) / 50000 AS INT) AS Batch
|
|
||||||
INTO #TempOrg
|
|
||||||
FROM [dbo].[Organization]
|
|
||||||
WHERE [FlexibleCollections] = 0;
|
|
||||||
|
|
||||||
-- Step 2: Get the maximum batch number
|
|
||||||
DECLARE @MaxBatch INT = (SELECT MAX(Batch) FROM #TempOrg);
|
|
||||||
DECLARE @CurrentBatch INT = 0;
|
|
||||||
|
|
||||||
-- Step 3: Process each batch
|
|
||||||
WHILE @CurrentBatch <= @MaxBatch
|
|
||||||
BEGIN
|
|
||||||
-- Update existing rows in [dbo].[CollectionUser]
|
|
||||||
UPDATE target
|
|
||||||
SET
|
|
||||||
target.[FlexibleCollections] = 1
|
|
||||||
FROM [dbo].[Organization] AS target
|
|
||||||
INNER JOIN #TempOrg AS source
|
|
||||||
ON target.[Id] = source.[OrganizationId]
|
|
||||||
WHERE source.[Batch] = @CurrentBatch;
|
|
||||||
|
|
||||||
-- Move to the next batch
|
|
||||||
SET @CurrentBatch = @CurrentBatch + 1;
|
|
||||||
END;
|
|
||||||
|
|
||||||
-- Step 4: Drop the temporary table
|
|
||||||
DROP TABLE #TempOrg;
|
|
Reference in New Issue
Block a user