mirror of
https://github.com/bitwarden/server.git
synced 2025-02-07 00:21:32 +01:00
[AC-1682] Added EF migrations
This commit is contained in:
parent
b7773b6fe8
commit
003b6dcc4d
@ -0,0 +1,26 @@
|
||||
-- Step 1: Create a temporary table to store the groups with AccessAll = 1
|
||||
CREATE TEMPORARY TABLE IF NOT EXISTS TempGroup AS
|
||||
SELECT `Id` AS `GroupId`, `OrganizationId`
|
||||
FROM `Group`
|
||||
WHERE `AccessAll` = 1;
|
||||
|
||||
-- Step 2: Update existing rows in `CollectionGroup`
|
||||
UPDATE `CollectionGroups` CG
|
||||
INNER JOIN `Collection` C ON CG.`CollectionId` = C.`Id`
|
||||
INNER JOIN TempGroup TG ON CG.`GroupId` = TG.`GroupId`
|
||||
SET
|
||||
CG.`ReadOnly` = 0,
|
||||
CG.`HidePasswords` = 0,
|
||||
CG.`Manage` = 0
|
||||
WHERE C.`OrganizationId` = TG.`OrganizationId`;
|
||||
|
||||
-- Step 3: Insert new rows into `CollectionGroup`
|
||||
INSERT INTO `CollectionGroups` (`CollectionId`, `GroupId`, `ReadOnly`, `HidePasswords`, `Manage`)
|
||||
SELECT C.`Id`, TG.`GroupId`, 0, 0, 0
|
||||
FROM `Collection` C
|
||||
INNER JOIN TempGroup TG ON C.`OrganizationId` = TG.`OrganizationId`
|
||||
LEFT JOIN `CollectionGroups` CG ON CG.`CollectionId` = C.`Id` AND CG.`GroupId` = TG.`GroupId`
|
||||
WHERE CG.`CollectionId` IS NULL;
|
||||
|
||||
-- Step 4: Drop the temporary table
|
||||
DROP TEMPORARY TABLE IF EXISTS TempGroup;
|
@ -0,0 +1,50 @@
|
||||
-- Step 1: Create a temporary table with an additional column for batch processing, update 50 k at a time
|
||||
CREATE TEMPORARY TABLE TempOrgUser AS
|
||||
SELECT OrganizationUser.Id AS OrganizationUserId, OrganizationUser.OrganizationId,
|
||||
CAST(ROW_NUMBER() OVER(ORDER BY OrganizationUser.Id) / 50000 AS SIGNED) AS Batch
|
||||
FROM OrganizationUser
|
||||
WHERE AccessAll = 1;
|
||||
|
||||
-- Step 2: Get the maximum batch number
|
||||
SET @MaxBatch := (SELECT MAX(Batch) FROM TempOrgUser);
|
||||
SET @CurrentBatch := 0;
|
||||
|
||||
-- Create the stored procedure
|
||||
CREATE PROCEDURE ProcessBatches(INOUT currentBatch INT, IN maxBatch INT)
|
||||
BEGIN
|
||||
WHILE currentBatch <= maxBatch DO
|
||||
-- Update existing rows in CollectionUsers
|
||||
UPDATE CollectionUsers AS target
|
||||
INNER JOIN (
|
||||
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;
|
@ -0,0 +1,8 @@
|
||||
-- Update `CollectionUser` with `Manage` = 1 for all users with Manager role or 'EditAssignedCollections' permission
|
||||
UPDATE CollectionUsers cu
|
||||
INNER JOIN OrganizationUser ou ON cu.OrganizationUserId = ou.Id
|
||||
SET cu.ReadOnly = 0,
|
||||
cu.HidePasswords = 0,
|
||||
cu.Manage = 1
|
||||
WHERE ou.Type = 3 OR (ou.Permissions IS NOT NULL AND
|
||||
JSON_VALID(ou.Permissions) AND JSON_UNQUOTE(JSON_EXTRACT(ou.Permissions, '$.editAssignedCollections')) = 'true');
|
2322
util/MySqlMigrations/Migrations/20231217125557_FlexibleCollections.Designer.cs
generated
Normal file
2322
util/MySqlMigrations/Migrations/20231217125557_FlexibleCollections.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using Bit.Core.Utilities;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Bit.MySqlMigrations.Migrations
|
||||
{
|
||||
public partial class FlexibleCollections : Migration
|
||||
{
|
||||
private const string _accessAllCollectionGroupsScript = "MySqlMigrations.HelperScripts.2023-12-06_00_AccessAllCollectionGroups.sql";
|
||||
private const string _accessAllCollectionUsersScript = "MySqlMigrations.HelperScripts.2023-12-06_01_AccessAllCollectionUsers.sql";
|
||||
private const string _managersEditAssignedCollectionUsersScript = "MySqlMigrations.HelperScripts.2023-12-06_02_ManagersEditAssignedCollectionUsers.sql";
|
||||
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.Sql(CoreHelpers.GetEmbeddedResourceContentsAsync(_accessAllCollectionGroupsScript));
|
||||
migrationBuilder.Sql(CoreHelpers.GetEmbeddedResourceContentsAsync(_accessAllCollectionUsersScript));
|
||||
migrationBuilder.Sql(CoreHelpers.GetEmbeddedResourceContentsAsync(_managersEditAssignedCollectionUsersScript));
|
||||
}
|
||||
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
throw new Exception("Irreversible migration");
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -29,5 +29,8 @@
|
||||
<EmbeddedResource Include="HelperScripts\2021-10-21_00_SetMaxAutoscaleSeatCount.sql" />
|
||||
<EmbeddedResource Include="HelperScripts\2022-03-01_00_Up_MigrateOrganizationApiKeys.sql" />
|
||||
<EmbeddedResource Include="HelperScripts\2022-03-01_00_Down_MigrateOrganizationApiKeys.sql" />
|
||||
<EmbeddedResource Include="HelperScripts\2023-12-06_00_AccessAllCollectionGroups.sql" />
|
||||
<EmbeddedResource Include="HelperScripts\2023-12-06_01_AccessAllCollectionUsers.sql" />
|
||||
<EmbeddedResource Include="HelperScripts\2023-12-06_02_ManagersEditAssignedCollectionUsers.sql" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
@ -0,0 +1,27 @@
|
||||
-- Create a temporary table to store the groups with AccessAll = true
|
||||
CREATE TEMPORARY TABLE TempGroup AS
|
||||
SELECT "Id" AS "GroupId", "OrganizationId"
|
||||
FROM "Group"
|
||||
WHERE "AccessAll" = true;
|
||||
|
||||
-- Update existing rows in "CollectionGroups"
|
||||
UPDATE "CollectionGroups" CG
|
||||
SET
|
||||
"ReadOnly" = false,
|
||||
"HidePasswords" = false,
|
||||
"Manage" = false
|
||||
FROM "CollectionGroups" CGUpdate
|
||||
INNER JOIN "Collection" C ON CGUpdate."CollectionId" = C."Id"
|
||||
INNER JOIN TempGroup TG ON CGUpdate."GroupId" = TG."GroupId"
|
||||
WHERE C."OrganizationId" = TG."OrganizationId";
|
||||
|
||||
-- Insert new rows into "CollectionGroups"
|
||||
INSERT INTO "CollectionGroups" ("CollectionId", "GroupId", "ReadOnly", "HidePasswords", "Manage")
|
||||
SELECT C."Id", TG."GroupId", false, false, false
|
||||
FROM "Collection" C
|
||||
INNER JOIN TempGroup TG ON C."OrganizationId" = TG."OrganizationId"
|
||||
LEFT JOIN "CollectionGroups" CG ON CG."CollectionId" = C."Id" AND CG."GroupId" = TG."GroupId"
|
||||
WHERE CG."CollectionId" IS NULL;
|
||||
|
||||
-- Drop the temporary table
|
||||
DROP TABLE IF EXISTS TempGroup;
|
@ -0,0 +1,51 @@
|
||||
-- Step 1: Insert into a temporary table with an additional column for batch processing, update 50 k at a time
|
||||
CREATE TEMPORARY TABLE "TempOrgUser" AS
|
||||
SELECT "Id" AS "OrganizationUserId", "OrganizationId", CAST(ROW_NUMBER() OVER(ORDER BY "Id") / 50000 AS INT) AS "Batch"
|
||||
FROM "OrganizationUser"
|
||||
WHERE "AccessAll" = true;
|
||||
|
||||
-- Step 2: Get the maximum batch number
|
||||
DO $$
|
||||
DECLARE
|
||||
MaxBatch INT;
|
||||
CurrentBatch INT := 0;
|
||||
BEGIN
|
||||
SELECT MAX("Batch") INTO MaxBatch FROM "TempOrgUser";
|
||||
|
||||
-- Step 3: Process each batch
|
||||
WHILE CurrentBatch <= MaxBatch LOOP
|
||||
-- Update existing rows in "CollectionUsers"
|
||||
UPDATE "CollectionUsers" AS target
|
||||
SET
|
||||
"ReadOnly" = false,
|
||||
"HidePasswords" = false,
|
||||
"Manage" = false
|
||||
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
|
||||
WHERE target."CollectionId" = source."CollectionId" AND target."OrganizationUserId" = source."OrganizationUserId";
|
||||
|
||||
-- Insert new rows into "CollectionUsers"
|
||||
INSERT INTO "CollectionUsers" ("CollectionId", "OrganizationUserId", "ReadOnly", "HidePasswords", "Manage")
|
||||
SELECT source."CollectionId", source."OrganizationUserId", false, false, false
|
||||
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
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM "CollectionUsers" target
|
||||
WHERE target."CollectionId" = source."CollectionId" AND target."OrganizationUserId" = source."OrganizationUserId"
|
||||
);
|
||||
|
||||
-- Move to the next batch
|
||||
CurrentBatch := CurrentBatch + 1;
|
||||
END LOOP;
|
||||
|
||||
END $$;
|
||||
|
||||
-- Step 4: Drop the temporary table
|
||||
DROP TABLE "TempOrgUser";
|
@ -0,0 +1,10 @@
|
||||
-- Update "CollectionUser" with "Manage" = 1 for all users with Manager role or 'EditAssignedCollections' permission
|
||||
UPDATE "CollectionUsers" cu
|
||||
SET "ReadOnly" = false,
|
||||
"HidePasswords" = false,
|
||||
"Manage" = true
|
||||
FROM "OrganizationUser" ou
|
||||
WHERE cu."OrganizationUserId" = ou."Id"
|
||||
AND (ou."Type" = 3 OR
|
||||
(ou."Permissions" IS NOT NULL AND
|
||||
(ou."Permissions"::text)::jsonb->>'editAssignedCollections' = 'true'));
|
2333
util/PostgresMigrations/Migrations/20231217125601_FlexibleCollections.Designer.cs
generated
Normal file
2333
util/PostgresMigrations/Migrations/20231217125601_FlexibleCollections.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using Bit.Core.Utilities;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Bit.PostgresMigrations.Migrations
|
||||
{
|
||||
public partial class FlexibleCollections : Migration
|
||||
{
|
||||
private const string _accessAllCollectionGroupsScript = "PostgresMigrations.HelperScripts.2023-12-06_00_AccessAllCollectionGroups.psql";
|
||||
private const string _accessAllCollectionUsersScript = "PostgresMigrations.HelperScripts.2023-12-06_01_AccessAllCollectionUsers.psql";
|
||||
private const string _managersEditAssignedCollectionUsersScript = "PostgresMigrations.HelperScripts.2023-12-06_02_ManagersEditAssignedCollectionUsers.psql";
|
||||
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.Sql(CoreHelpers.GetEmbeddedResourceContentsAsync(_accessAllCollectionGroupsScript));
|
||||
migrationBuilder.Sql(CoreHelpers.GetEmbeddedResourceContentsAsync(_accessAllCollectionUsersScript));
|
||||
migrationBuilder.Sql(CoreHelpers.GetEmbeddedResourceContentsAsync(_managersEditAssignedCollectionUsersScript));
|
||||
}
|
||||
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
throw new Exception("Irreversible migration");
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -24,5 +24,8 @@
|
||||
<EmbeddedResource Include="HelperScripts\2021-10-21_00_SetMaxAutoscaleSeatCount.psql" />
|
||||
<EmbeddedResource Include="HelperScripts\2022-03-01_00_Up_MigrateOrganizationApiKeys.psql" />
|
||||
<EmbeddedResource Include="HelperScripts\2022-03-01_00_Down_MigrateOrganizationApiKeys.psql" />
|
||||
<EmbeddedResource Include="HelperScripts\2023-12-06_00_AccessAllCollectionGroups.psql" />
|
||||
<EmbeddedResource Include="HelperScripts\2023-12-06_01_AccessAllCollectionUsers.psql" />
|
||||
<EmbeddedResource Include="HelperScripts\2023-12-06_02_ManagersEditAssignedCollectionUsers.psql" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
@ -0,0 +1,29 @@
|
||||
-- Create a temporary table to store the groups with AccessAll = 1
|
||||
CREATE TEMPORARY TABLE TempGroup AS
|
||||
SELECT "Id" AS "GroupId", "OrganizationId"
|
||||
FROM "Group"
|
||||
WHERE "AccessAll" = 1;
|
||||
|
||||
-- Update existing rows in "CollectionGroup"
|
||||
UPDATE "CollectionGroups"
|
||||
SET
|
||||
"ReadOnly" = 0,
|
||||
"HidePasswords" = 0,
|
||||
"Manage" = 0
|
||||
WHERE EXISTS (
|
||||
SELECT 1
|
||||
FROM "Collection" C
|
||||
INNER JOIN TempGroup TG ON "CollectionGroups"."GroupId" = TG."GroupId"
|
||||
WHERE "CollectionGroups"."CollectionId" = C."Id" AND C."OrganizationId" = TG."OrganizationId"
|
||||
);
|
||||
|
||||
-- Insert new rows into "CollectionGroup"
|
||||
INSERT INTO "CollectionGroups" ("CollectionId", "GroupId", "ReadOnly", "HidePasswords", "Manage")
|
||||
SELECT C."Id", TG."GroupId", 0, 0, 0
|
||||
FROM "Collection" C
|
||||
INNER JOIN TempGroup TG ON C."OrganizationId" = TG."OrganizationId"
|
||||
LEFT JOIN "CollectionGroups" CG ON CG."CollectionId" = C."Id" AND CG."GroupId" = TG."GroupId"
|
||||
WHERE CG."CollectionId" IS NULL;
|
||||
|
||||
-- Drop the temporary table
|
||||
DROP TABLE IF EXISTS TempGroup;
|
@ -0,0 +1,35 @@
|
||||
-- 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
|
||||
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;
|
||||
|
||||
-- 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;
|
@ -0,0 +1,12 @@
|
||||
-- Update "CollectionUser" with "Manage" = 1 for all users with Manager role or 'EditAssignedCollections' permission
|
||||
UPDATE "CollectionUsers"
|
||||
SET "ReadOnly" = 0,
|
||||
"HidePasswords" = 0,
|
||||
"Manage" = 1
|
||||
WHERE "OrganizationUserId" IN (
|
||||
SELECT cu."OrganizationUserId"
|
||||
FROM "CollectionUsers" cu
|
||||
INNER JOIN "OrganizationUser" ou ON cu."OrganizationUserId" = ou."Id"
|
||||
WHERE ou."Type" = 3 OR (ou."Permissions" IS NOT NULL AND
|
||||
JSON_VALID(ou."Permissions") AND json_extract(ou."Permissions", '$.editAssignedCollections') = 'true')
|
||||
);
|
2320
util/SqliteMigrations/Migrations/20231217125605_FlexibleCollections.Designer.cs
generated
Normal file
2320
util/SqliteMigrations/Migrations/20231217125605_FlexibleCollections.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using Bit.Core.Utilities;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Bit.SqliteMigrations.Migrations
|
||||
{
|
||||
public partial class FlexibleCollections : Migration
|
||||
{
|
||||
private const string _accessAllCollectionGroupsScript = "SqliteMigrations.HelperScripts.2023-12-06_00_AccessAllCollectionGroups.sql";
|
||||
private const string _accessAllCollectionUsersScript = "SqliteMigrations.HelperScripts.2023-12-06_01_AccessAllCollectionUsers.sql";
|
||||
private const string _managersEditAssignedCollectionUsersScript = "SqliteMigrations.HelperScripts.2023-12-06_02_ManagersEditAssignedCollectionUsers.sql";
|
||||
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.Sql(CoreHelpers.GetEmbeddedResourceContentsAsync(_accessAllCollectionGroupsScript));
|
||||
migrationBuilder.Sql(CoreHelpers.GetEmbeddedResourceContentsAsync(_accessAllCollectionUsersScript));
|
||||
migrationBuilder.Sql(CoreHelpers.GetEmbeddedResourceContentsAsync(_managersEditAssignedCollectionUsersScript));
|
||||
}
|
||||
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
throw new Exception("Irreversible migration");
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -22,4 +22,10 @@
|
||||
<Compile Include="..\EfShared\MigrationBuilderExtensions.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="HelperScripts\2023-12-06_00_AccessAllCollectionGroups.sql" />
|
||||
<EmbeddedResource Include="HelperScripts\2023-12-06_01_AccessAllCollectionUsers.sql" />
|
||||
<EmbeddedResource Include="HelperScripts\2023-12-06_02_ManagersEditAssignedCollectionUsers.sql" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
Loading…
Reference in New Issue
Block a user