mirror of
https://github.com/bitwarden/server.git
synced 2025-03-15 13:59:25 +01:00
[AC-1682] Updated postgres migrations
This commit is contained in:
parent
752e2c01ea
commit
d5b41c1b51
@ -4,18 +4,24 @@ SELECT "Id" AS "GroupId", "OrganizationId"
|
|||||||
FROM "Group"
|
FROM "Group"
|
||||||
WHERE "AccessAll" = true;
|
WHERE "AccessAll" = true;
|
||||||
|
|
||||||
-- Step 2: Update existing rows in "CollectionGroup"
|
-- Step 2: Create a temporary table to store distinct OrganizationUserIds
|
||||||
|
CREATE TEMP TABLE IF NOT EXISTS TempOrganizationUsers AS
|
||||||
|
SELECT DISTINCT GU."OrganizationUserId"
|
||||||
|
FROM "GroupUser" GU
|
||||||
|
JOIN TempGroup TG ON GU."GroupId" = TG."GroupId";
|
||||||
|
|
||||||
|
-- Step 3: Update existing rows in "CollectionGroups"
|
||||||
UPDATE "CollectionGroups" CG
|
UPDATE "CollectionGroups" CG
|
||||||
SET
|
SET
|
||||||
"ReadOnly" = false,
|
"ReadOnly" = false,
|
||||||
"HidePasswords" = false,
|
"HidePasswords" = false,
|
||||||
"Manage" = false
|
"Manage" = false
|
||||||
FROM "CollectionGroups" CGUpdate
|
FROM "CollectionGroups" CGUpdate
|
||||||
INNER JOIN "Collection" C ON CGUpdate."CollectionId" = C."Id"
|
INNER JOIN "Collection" C ON CGUpdate."CollectionId" = C."Id"
|
||||||
INNER JOIN TempGroup TG ON CGUpdate."GroupId" = TG."GroupId"
|
INNER JOIN TempGroup TG ON CGUpdate."GroupId" = TG."GroupId"
|
||||||
WHERE C."OrganizationId" = TG."OrganizationId";
|
WHERE C."OrganizationId" = TG."OrganizationId";
|
||||||
|
|
||||||
-- Step 3: Insert new rows into "CollectionGroup"
|
-- Step 4: Insert new rows into "CollectionGroups"
|
||||||
INSERT INTO "CollectionGroups" ("CollectionId", "GroupId", "ReadOnly", "HidePasswords", "Manage")
|
INSERT INTO "CollectionGroups" ("CollectionId", "GroupId", "ReadOnly", "HidePasswords", "Manage")
|
||||||
SELECT C."Id", TG."GroupId", false, false, false
|
SELECT C."Id", TG."GroupId", false, false, false
|
||||||
FROM "Collection" C
|
FROM "Collection" C
|
||||||
@ -23,5 +29,19 @@ INNER JOIN TempGroup TG ON C."OrganizationId" = TG."OrganizationId"
|
|||||||
LEFT JOIN "CollectionGroups" CG ON CG."CollectionId" = C."Id" AND CG."GroupId" = TG."GroupId"
|
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 5: Update Group to clear AccessAll flag
|
||||||
|
UPDATE "Group" G
|
||||||
|
SET "AccessAll" = false
|
||||||
|
FROM TempGroup TG
|
||||||
|
WHERE G."Id" = TG."GroupId";
|
||||||
|
|
||||||
|
-- Step 6: Update User AccountRevisionDate for each unique OrganizationUserId
|
||||||
|
UPDATE "User" U
|
||||||
|
SET "AccountRevisionDate" = current_timestamp
|
||||||
|
FROM "OrganizationUser" OU
|
||||||
|
JOIN TempOrganizationUsers TOU ON OU."Id" = TOU."OrganizationUserId"
|
||||||
|
WHERE U."Id" = OU."UserId" AND OU."Status" = 2;
|
||||||
|
|
||||||
|
-- Step 7: Drop the temporary tables
|
||||||
DROP TABLE IF EXISTS TempGroup;
|
DROP TABLE IF EXISTS TempGroup;
|
||||||
|
DROP TABLE IF EXISTS TempOrganizationUsers;
|
||||||
|
@ -1,22 +1,43 @@
|
|||||||
-- Update existing rows in CollectionUsers
|
-- Step 1: Create a temporary table
|
||||||
UPDATE "CollectionUsers"
|
CREATE TEMP TABLE IF NOT EXISTS TempOrgUser AS
|
||||||
|
SELECT "Id" AS "OrganizationUserId", "OrganizationId"
|
||||||
|
FROM "OrganizationUser"
|
||||||
|
WHERE "AccessAll" = true;
|
||||||
|
|
||||||
|
-- Step 2: Update existing rows in CollectionUsers
|
||||||
|
UPDATE "CollectionUsers" cu
|
||||||
SET
|
SET
|
||||||
"ReadOnly" = false,
|
"ReadOnly" = false,
|
||||||
"HidePasswords" = false,
|
"HidePasswords" = false,
|
||||||
"Manage" = false
|
"Manage" = false
|
||||||
FROM "Collection" AS C
|
FROM "CollectionUsers" cuUpdate
|
||||||
INNER JOIN "CollectionUsers" AS CU ON CU."CollectionId" = C."Id"
|
INNER JOIN "Collection" C ON cuUpdate."CollectionId" = C."Id"
|
||||||
INNER JOIN "OrganizationUser" AS OU ON CU."CollectionId" = C."Id" AND C."OrganizationId" = OU."OrganizationId"
|
INNER JOIN TempOrgUser OU ON cuUpdate."OrganizationUserId" = OU."OrganizationUserId"
|
||||||
WHERE OU."AccessAll" = true;
|
WHERE C."OrganizationId" = OU."OrganizationId";
|
||||||
|
|
||||||
-- Insert new rows into CollectionUsers
|
-- Step 3: Insert new rows into CollectionUsers
|
||||||
INSERT INTO "CollectionUsers" ("CollectionId", "OrganizationUserId", "ReadOnly", "HidePasswords", "Manage")
|
INSERT INTO "CollectionUsers" ("CollectionId", "OrganizationUserId", "ReadOnly", "HidePasswords", "Manage")
|
||||||
SELECT C."Id" AS "CollectionId", OU."Id" AS "OrganizationUserId", false, false, false
|
SELECT C."Id" AS "CollectionId", OU."OrganizationUserId", false, false, false
|
||||||
FROM "Collection" AS C
|
FROM "Collection" AS C
|
||||||
INNER JOIN "OrganizationUser" AS OU ON C."OrganizationId" = OU."OrganizationId"
|
INNER JOIN TempOrgUser AS OU ON C."OrganizationId" = OU."OrganizationId"
|
||||||
WHERE OU."AccessAll" = true
|
WHERE NOT EXISTS (
|
||||||
AND NOT EXISTS (
|
|
||||||
SELECT 1
|
SELECT 1
|
||||||
FROM "CollectionUsers" AS CU
|
FROM "CollectionUsers" AS CU
|
||||||
WHERE CU."CollectionId" = C."Id" AND CU."OrganizationUserId" = OU."Id"
|
WHERE CU."CollectionId" = C."Id" AND CU."OrganizationUserId" = OU."OrganizationUserId"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
-- Step 4: Update OrganizationUser to clear AccessAll flag
|
||||||
|
UPDATE "OrganizationUser" AS OU
|
||||||
|
SET "AccessAll" = false
|
||||||
|
FROM TempOrgUser AS T
|
||||||
|
WHERE OU."Id" = T."OrganizationUserId";
|
||||||
|
|
||||||
|
-- Step 5: Update "User" AccountRevisionDate for each unique OrganizationUserId
|
||||||
|
UPDATE "User" AS U
|
||||||
|
SET "AccountRevisionDate" = current_timestamp
|
||||||
|
FROM "OrganizationUser" AS OU
|
||||||
|
JOIN TempOrgUser AS TOU ON OU."Id" = TOU."OrganizationUserId"
|
||||||
|
WHERE U."Id" = OU."UserId" AND OU."Status" = 2;
|
||||||
|
|
||||||
|
-- Step 6: Drop the temporary table
|
||||||
|
DROP TABLE IF EXISTS TempOrgUser;
|
||||||
|
@ -1,24 +1,80 @@
|
|||||||
-- Update `CollectionUsers` with `Manage` = 1 for all users with Manager role or 'EditAssignedCollections' permission
|
-- Step 1: Update `CollectionUser` with `Manage` = 1 for all users with Manager role or 'EditAssignedCollections' permission
|
||||||
UPDATE "CollectionUsers" cu
|
-- Create a temporary table
|
||||||
SET
|
CREATE TEMP TABLE IF NOT EXISTS TempOrgUser AS
|
||||||
"ReadOnly" = false,
|
SELECT ou."Id" AS "OrganizationUserId"
|
||||||
"HidePasswords" = false,
|
FROM "OrganizationUser" ou
|
||||||
"Manage" = true
|
WHERE (ou."Type" = 3 OR
|
||||||
FROM "OrganizationUser" ou
|
(ou."Permissions" IS NOT NULL AND
|
||||||
WHERE cu."OrganizationUserId" = ou."Id"
|
((ou."Permissions"::text)::jsonb->>'editAssignedCollections') = 'true'));
|
||||||
AND (ou."Type" = 3 OR
|
|
||||||
(ou."Permissions" IS NOT NULL AND
|
|
||||||
((ou."Permissions"::text)::jsonb->>'editAssignedCollections') = 'true'));
|
|
||||||
|
|
||||||
-- Insert rows into CollectionUsers for Managers and users with 'EditAssignedCollections' permission assigned to groups with collection access
|
-- Update CollectionUsers with Manage = 1 using the temporary table
|
||||||
INSERT INTO "CollectionUsers" ("CollectionId", "OrganizationUserId", "ReadOnly", "HidePasswords", "Manage")
|
UPDATE "CollectionUsers" cu
|
||||||
SELECT cg."CollectionId", ou."Id", false, false, true
|
SET
|
||||||
FROM "CollectionGroups" cg
|
"ReadOnly" = false,
|
||||||
INNER JOIN "GroupUser" gu ON cg."GroupId" = gu."GroupId"
|
"HidePasswords" = false,
|
||||||
INNER JOIN "OrganizationUser" ou ON gu."OrganizationUserId" = ou."Id"
|
"Manage" = true
|
||||||
WHERE (ou."Type" = 3 OR
|
FROM TempOrgUser temp
|
||||||
(ou."Permissions" IS NOT NULL AND
|
WHERE cu."OrganizationUserId" = temp."OrganizationUserId";
|
||||||
((ou."Permissions"::text)::jsonb->>'editAssignedCollections') = 'true'))
|
|
||||||
AND NOT EXISTS (
|
-- Update `User` AccountRevisionDate for each unique OrganizationUserId
|
||||||
SELECT 1 FROM "CollectionUsers" cu
|
UPDATE "User" AS U
|
||||||
WHERE cu."CollectionId" = cg."CollectionId" AND cu."OrganizationUserId" = ou."Id");
|
SET "AccountRevisionDate" = current_timestamp
|
||||||
|
FROM "OrganizationUser" AS OU
|
||||||
|
JOIN TempOrgUser AS TOU ON OU."Id" = TOU."OrganizationUserId"
|
||||||
|
WHERE OU."Status" = 2 AND U."Id" = OU."UserId";
|
||||||
|
|
||||||
|
-- Drop the temporary table
|
||||||
|
DROP TABLE IF EXISTS TempOrgUser;
|
||||||
|
|
||||||
|
-- Step 2: Insert rows to CollectionUser for Managers and users with 'EditAssignedCollections' permission assigned to groups with collection access
|
||||||
|
-- Store the results in a temporary table
|
||||||
|
CREATE TEMP TABLE IF NOT EXISTS TempCol AS
|
||||||
|
SELECT cg."CollectionId", ou."Id" AS "OrganizationUserId"
|
||||||
|
FROM "CollectionGroups" cg
|
||||||
|
INNER JOIN "GroupUser" gu ON cg."GroupId" = gu."GroupId"
|
||||||
|
INNER JOIN "OrganizationUser" ou ON gu."OrganizationUserId" = ou."Id"
|
||||||
|
WHERE (ou."Type" = 3 OR
|
||||||
|
(ou."Permissions" IS NOT NULL AND
|
||||||
|
((ou."Permissions"::text)::jsonb->>'editAssignedCollections') = 'true'))
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT 1 FROM "CollectionUsers" cu
|
||||||
|
WHERE cu."CollectionId" = cg."CollectionId" AND cu."OrganizationUserId" = ou."Id"
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Insert rows into CollectionUsers using the temporary table
|
||||||
|
INSERT INTO "CollectionUsers" ("CollectionId", "OrganizationUserId", "ReadOnly", "HidePasswords", "Manage")
|
||||||
|
SELECT "CollectionId", "OrganizationUserId", false, false, true
|
||||||
|
FROM TempCol;
|
||||||
|
|
||||||
|
-- Update `User` AccountRevisionDate for each unique OrganizationUserId
|
||||||
|
UPDATE "User" AS U
|
||||||
|
SET "AccountRevisionDate" = current_timestamp
|
||||||
|
FROM "OrganizationUser" AS OU
|
||||||
|
JOIN TempCol AS TC ON OU."Id" = TC."OrganizationUserId"
|
||||||
|
WHERE OU."Status" = 2 AND U."Id" = OU."UserId";
|
||||||
|
|
||||||
|
-- Drop the temporary table
|
||||||
|
DROP TABLE IF EXISTS TempCol;
|
||||||
|
|
||||||
|
-- Step 3: Set all Managers to Users
|
||||||
|
-- Create a temporary table
|
||||||
|
CREATE TEMP TABLE IF NOT EXISTS TempOrgUser AS
|
||||||
|
SELECT ou."Id" AS "OrganizationUserId"
|
||||||
|
FROM "OrganizationUser" ou
|
||||||
|
WHERE ou."Type" = 3;
|
||||||
|
|
||||||
|
-- Update OrganizationUser with Type = 2 using the temporary table
|
||||||
|
UPDATE "OrganizationUser" ou
|
||||||
|
SET "Type" = 2
|
||||||
|
FROM TempOrgUser temp
|
||||||
|
WHERE ou."Id" = temp."OrganizationUserId";
|
||||||
|
|
||||||
|
-- Update `User` AccountRevisionDate for each unique OrganizationUserId
|
||||||
|
UPDATE "User" AS U
|
||||||
|
SET "AccountRevisionDate" = current_timestamp
|
||||||
|
FROM "OrganizationUser" AS OU
|
||||||
|
JOIN TempOrgUser AS TOU ON OU."Id" = TOU."OrganizationUserId"
|
||||||
|
WHERE OU."Status" = 2 AND U."Id" = OU."UserId";
|
||||||
|
|
||||||
|
-- Drop the temporary table
|
||||||
|
DROP TABLE IF EXISTS TempOrgUser;
|
||||||
|
@ -0,0 +1,4 @@
|
|||||||
|
-- Set "FlexibleCollections" = true for all organizations that have not yet been migrated.
|
||||||
|
UPDATE "Organization"
|
||||||
|
SET "FlexibleCollections" = true
|
||||||
|
WHERE "FlexibleCollections" = false;
|
Loading…
Reference in New Issue
Block a user