1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-02 08:32:50 -05:00

[AC-1682] Added EF migrations

This commit is contained in:
Rui Tome
2023-12-17 21:26:12 +00:00
parent b7773b6fe8
commit 003b6dcc4d
21 changed files with 8790 additions and 1267 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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')
);

File diff suppressed because it is too large Load Diff

View File

@ -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");
}
}
}

View File

@ -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>