mirror of
https://github.com/bitwarden/server.git
synced 2025-07-01 08:02:49 -05:00
[AC-1682] mySql scripts formatting
This commit is contained in:
@ -6,20 +6,22 @@ WHERE `AccessAll` = 1;
|
|||||||
|
|
||||||
-- Step 2: Update existing rows in `CollectionGroup`
|
-- Step 2: Update existing rows in `CollectionGroup`
|
||||||
UPDATE `CollectionGroups` CG
|
UPDATE `CollectionGroups` CG
|
||||||
INNER JOIN `Collection` C ON CG.`CollectionId` = C.`Id`
|
INNER JOIN `Collection` C ON CG.`CollectionId` = C.`Id`
|
||||||
INNER JOIN TempGroup TG ON CG.`GroupId` = TG.`GroupId`
|
INNER JOIN TempGroup TG ON CG.`GroupId` = TG.`GroupId`
|
||||||
SET
|
SET
|
||||||
CG.`ReadOnly` = 0,
|
CG.`ReadOnly` = 0,
|
||||||
CG.`HidePasswords` = 0,
|
CG.`HidePasswords` = 0,
|
||||||
CG.`Manage` = 0
|
CG.`Manage` = 0
|
||||||
WHERE C.`OrganizationId` = TG.`OrganizationId`;
|
WHERE C.`OrganizationId` = TG.`OrganizationId`;
|
||||||
|
|
||||||
-- Step 3: Insert new rows into `CollectionGroup`
|
-- Step 3: Insert new rows into `CollectionGroup`
|
||||||
INSERT INTO `CollectionGroups` (`CollectionId`, `GroupId`, `ReadOnly`, `HidePasswords`, `Manage`)
|
INSERT INTO `CollectionGroups` (`CollectionId`, `GroupId`, `ReadOnly`, `HidePasswords`, `Manage`)
|
||||||
SELECT C.`Id`, TG.`GroupId`, 0, 0, 0
|
SELECT C.`Id`, TG.`GroupId`, 0, 0, 0
|
||||||
FROM `Collection` C
|
FROM `Collection` C
|
||||||
INNER JOIN TempGroup TG ON C.`OrganizationId` = TG.`OrganizationId`
|
INNER JOIN TempGroup TG
|
||||||
LEFT JOIN `CollectionGroups` CG ON CG.`CollectionId` = C.`Id` AND CG.`GroupId` = TG.`GroupId`
|
ON C.`OrganizationId` = TG.`OrganizationId`
|
||||||
|
LEFT JOIN `CollectionGroups` CG
|
||||||
|
ON CG.`CollectionId` = C.`Id` AND CG.`GroupId` = TG.`GroupId`
|
||||||
WHERE CG.`CollectionId` IS NULL;
|
WHERE CG.`CollectionId` IS NULL;
|
||||||
|
|
||||||
-- Step 4: Drop the temporary table
|
-- Step 4: Drop the temporary table
|
||||||
|
@ -1,51 +1,26 @@
|
|||||||
-- Step 1: Create a temporary table with an additional column for batch processing, update 50 k at a time
|
-- Update existing rows in CollectionUsers
|
||||||
CREATE TEMPORARY TABLE TempOrgUser AS
|
UPDATE CollectionUsers AS target
|
||||||
SELECT OrganizationUser.Id AS OrganizationUserId, OrganizationUser.OrganizationId,
|
INNER JOIN (
|
||||||
CAST(ROW_NUMBER() OVER(ORDER BY OrganizationUser.Id) / 50000 AS SIGNED) AS Batch
|
SELECT C.Id AS CollectionId, T.OrganizationUserId
|
||||||
FROM OrganizationUser
|
FROM Collection C
|
||||||
WHERE AccessAll = 1;
|
INNER JOIN OrganizationUser T ON C.OrganizationId = T.OrganizationId
|
||||||
|
WHERE T.AccessAll = 1
|
||||||
|
) AS source
|
||||||
|
ON target.CollectionId = source.CollectionId AND target.OrganizationUserId = source.OrganizationUserId
|
||||||
|
SET
|
||||||
|
target.ReadOnly = 0,
|
||||||
|
target.HidePasswords = 0,
|
||||||
|
target.Manage = 0;
|
||||||
|
|
||||||
-- Step 2: Get the maximum batch number
|
-- Insert new rows into CollectionUsers
|
||||||
SET @MaxBatch := (SELECT MAX(Batch) FROM TempOrgUser);
|
INSERT INTO CollectionUsers (CollectionId, OrganizationUserId, ReadOnly, HidePasswords, Manage)
|
||||||
SET @CurrentBatch := 0;
|
SELECT source.CollectionId, source.OrganizationUserId, 0, 0, 0
|
||||||
|
FROM (
|
||||||
-- Create the stored procedure
|
SELECT C.Id AS CollectionId, T.OrganizationUserId
|
||||||
DROP PROCEDURE IF EXISTS ProcessBatches;
|
FROM Collection C
|
||||||
CREATE PROCEDURE ProcessBatches(INOUT currentBatch INT, IN maxBatch INT)
|
INNER JOIN OrganizationUser T ON C.OrganizationId = T.OrganizationId
|
||||||
BEGIN
|
WHERE T.AccessAll = 1
|
||||||
WHILE currentBatch <= maxBatch DO
|
) AS source
|
||||||
-- Update existing rows in CollectionUsers
|
LEFT JOIN CollectionUsers AS target
|
||||||
UPDATE CollectionUsers AS target
|
ON target.CollectionId = source.CollectionId AND target.OrganizationUserId = source.OrganizationUserId
|
||||||
INNER JOIN (
|
WHERE target.CollectionId IS NULL;
|
||||||
SELECT C.Id AS CollectionId, T.OrganizationUserId
|
|
||||||
FROM 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
|
|
||||||
SET
|
|
||||||
target.ReadOnly = 0,
|
|
||||||
target.HidePasswords = 0,
|
|
||||||
target.Manage = 0;
|
|
||||||
|
|
||||||
-- Insert new rows into CollectionUsers
|
|
||||||
INSERT INTO CollectionUsers (CollectionId, OrganizationUserId, ReadOnly, HidePasswords, Manage)
|
|
||||||
SELECT source.CollectionId, source.OrganizationUserId, 0, 0, 0
|
|
||||||
FROM (
|
|
||||||
SELECT C.Id AS CollectionId, T.OrganizationUserId
|
|
||||||
FROM Collection C
|
|
||||||
INNER JOIN TempOrgUser T ON C.OrganizationId = T.OrganizationId AND T.Batch = currentBatch
|
|
||||||
) AS source
|
|
||||||
LEFT JOIN CollectionUsers 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 WHILE;
|
|
||||||
END;
|
|
||||||
|
|
||||||
-- Step 3: Process each batch by calling the stored procedure
|
|
||||||
CALL ProcessBatches(@CurrentBatch, @MaxBatch);
|
|
||||||
|
|
||||||
-- Step 4: Drop the temporary table
|
|
||||||
DROP TEMPORARY TABLE IF EXISTS TempOrgUser;
|
|
||||||
|
Reference in New Issue
Block a user