1
0
mirror of https://github.com/bitwarden/server synced 2025-12-24 12:13:17 +00:00

[AC-1682] Sqlite migrations + dotnet format

This commit is contained in:
Rui Tome
2023-12-20 12:25:55 +00:00
parent 86ba89f230
commit 73a13150f7
9 changed files with 4735 additions and 52 deletions

View File

@@ -1,35 +1,24 @@
-- Step 1: Insert into a temporary table without batching
CREATE TEMPORARY TABLE TempOrgUser AS
SELECT Id AS OrganizationUserId, OrganizationId
FROM OrganizationUser
WHERE AccessAll = 1;
-- Step 2: Process all records at once
-- Update existing rows in CollectionUser
UPDATE CollectionUsers
-- Update existing rows in CollectionUsers
UPDATE "CollectionUsers"
SET
ReadOnly = 0,
HidePasswords = 0,
Manage = 0
FROM CollectionUsers AS target
INNER JOIN (
SELECT C.Id AS CollectionId, T.OrganizationUserId
FROM Collection C
INNER JOIN TempOrgUser T ON C.OrganizationId = T.OrganizationId
) AS source
ON target.CollectionId = source.CollectionId AND target.OrganizationUserId = source.OrganizationUserId;
"ReadOnly" = 0,
"HidePasswords" = 0,
"Manage" = 0
WHERE "CollectionId" IN (
SELECT "C"."Id"
FROM "Collection" "C"
INNER JOIN "OrganizationUser" "OU" ON "C"."OrganizationId" = "OU"."OrganizationId"
WHERE "OU"."AccessAll" = 1
);
-- Insert new rows into CollectionUser
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
) AS source
LEFT JOIN CollectionUsers AS target
ON target.CollectionId = source.CollectionId AND target.OrganizationUserId = source.OrganizationUserId
WHERE target.CollectionId IS NULL;
-- Step 3: Drop the temporary table
DROP TABLE TempOrgUser;
-- Insert new rows into CollectionUsers
INSERT INTO "CollectionUsers" ("CollectionId", "OrganizationUserId", "ReadOnly", "HidePasswords", "Manage")
SELECT "C"."Id" AS "CollectionId", "OU"."Id" AS "OrganizationUserId", 0, 0, 0
FROM "Collection" "C"
INNER JOIN "OrganizationUser" "OU" ON "C"."OrganizationId" = "OU"."OrganizationId"
WHERE "OU"."AccessAll" = 1
AND NOT EXISTS (
SELECT 1
FROM "CollectionUsers" "CU"
WHERE "CU"."CollectionId" = "C"."Id" AND "CU"."OrganizationUserId" = "OU"."Id"
);