mirror of
https://github.com/bitwarden/server.git
synced 2025-07-01 16:12:49 -05:00
[AC-1682] Sqlite migrations + dotnet format
This commit is contained in:
@ -1,10 +1,10 @@
|
|||||||
-- Create a temporary table to store the groups with AccessAll = 1
|
-- Step 1: Create a temporary table to store the groups with AccessAll = 1
|
||||||
CREATE TEMPORARY TABLE TempGroup AS
|
CREATE TEMPORARY TABLE TempGroup AS
|
||||||
SELECT "Id" AS "GroupId", "OrganizationId"
|
SELECT "Id" AS "GroupId", "OrganizationId"
|
||||||
FROM "Group"
|
FROM "Group"
|
||||||
WHERE "AccessAll" = 1;
|
WHERE "AccessAll" = 1;
|
||||||
|
|
||||||
-- Update existing rows in "CollectionGroup"
|
-- Step 2: Update existing rows in "CollectionGroup"
|
||||||
UPDATE "CollectionGroups"
|
UPDATE "CollectionGroups"
|
||||||
SET
|
SET
|
||||||
"ReadOnly" = 0,
|
"ReadOnly" = 0,
|
||||||
@ -17,7 +17,7 @@ WHERE EXISTS (
|
|||||||
WHERE "CollectionGroups"."CollectionId" = C."Id" AND C."OrganizationId" = TG."OrganizationId"
|
WHERE "CollectionGroups"."CollectionId" = C."Id" AND C."OrganizationId" = TG."OrganizationId"
|
||||||
);
|
);
|
||||||
|
|
||||||
-- 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
|
||||||
@ -25,5 +25,5 @@ FROM "Collection" C
|
|||||||
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;
|
||||||
|
|
||||||
-- Drop the temporary table
|
-- Step 4: Drop the temporary table
|
||||||
DROP TABLE IF EXISTS TempGroup;
|
DROP TABLE IF EXISTS TempGroup;
|
||||||
|
@ -1,35 +1,24 @@
|
|||||||
-- Step 1: Insert into a temporary table without batching
|
-- Update existing rows in CollectionUsers
|
||||||
CREATE TEMPORARY TABLE TempOrgUser AS
|
UPDATE "CollectionUsers"
|
||||||
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
|
SET
|
||||||
ReadOnly = 0,
|
"ReadOnly" = 0,
|
||||||
HidePasswords = 0,
|
"HidePasswords" = 0,
|
||||||
Manage = 0
|
"Manage" = 0
|
||||||
FROM CollectionUsers AS target
|
WHERE "CollectionId" IN (
|
||||||
INNER JOIN (
|
SELECT "C"."Id"
|
||||||
SELECT C.Id AS CollectionId, T.OrganizationUserId
|
FROM "Collection" "C"
|
||||||
FROM Collection C
|
INNER JOIN "OrganizationUser" "OU" ON "C"."OrganizationId" = "OU"."OrganizationId"
|
||||||
INNER JOIN TempOrgUser T ON C.OrganizationId = T.OrganizationId
|
WHERE "OU"."AccessAll" = 1
|
||||||
) AS source
|
);
|
||||||
ON target.CollectionId = source.CollectionId AND target.OrganizationUserId = source.OrganizationUserId;
|
|
||||||
|
|
||||||
-- Insert new rows into CollectionUser
|
-- Insert new rows into CollectionUsers
|
||||||
INSERT INTO CollectionUsers (CollectionId, OrganizationUserId, ReadOnly, HidePasswords, Manage)
|
INSERT INTO "CollectionUsers" ("CollectionId", "OrganizationUserId", "ReadOnly", "HidePasswords", "Manage")
|
||||||
SELECT source.CollectionId, source.OrganizationUserId, 0, 0, 0
|
SELECT "C"."Id" AS "CollectionId", "OU"."Id" AS "OrganizationUserId", 0, 0, 0
|
||||||
FROM (
|
FROM "Collection" "C"
|
||||||
SELECT C.Id AS CollectionId, T.OrganizationUserId
|
INNER JOIN "OrganizationUser" "OU" ON "C"."OrganizationId" = "OU"."OrganizationId"
|
||||||
FROM Collection C
|
WHERE "OU"."AccessAll" = 1
|
||||||
INNER JOIN TempOrgUser T ON C.OrganizationId = T.OrganizationId
|
AND NOT EXISTS (
|
||||||
) AS source
|
SELECT 1
|
||||||
LEFT JOIN CollectionUsers AS target
|
FROM "CollectionUsers" "CU"
|
||||||
ON target.CollectionId = source.CollectionId AND target.OrganizationUserId = source.OrganizationUserId
|
WHERE "CU"."CollectionId" = "C"."Id" AND "CU"."OrganizationUserId" = "OU"."Id"
|
||||||
WHERE target.CollectionId IS NULL;
|
);
|
||||||
|
|
||||||
-- Step 3: Drop the temporary table
|
|
||||||
DROP TABLE TempOrgUser;
|
|
||||||
|
@ -1,12 +1,28 @@
|
|||||||
-- Update "CollectionUser" with "Manage" = 1 for all users with Manager role or 'EditAssignedCollections' permission
|
-- Update `CollectionUser` with `Manage` = 1 for all users with Manager role or 'EditAssignedCollections' permission
|
||||||
UPDATE "CollectionUsers"
|
UPDATE "CollectionUsers" AS cu
|
||||||
SET "ReadOnly" = 0,
|
SET
|
||||||
|
"ReadOnly" = 0,
|
||||||
"HidePasswords" = 0,
|
"HidePasswords" = 0,
|
||||||
"Manage" = 1
|
"Manage" = 1
|
||||||
WHERE "OrganizationUserId" IN (
|
WHERE "OrganizationUserId" IN (
|
||||||
SELECT cu."OrganizationUserId"
|
SELECT ou."Id"
|
||||||
FROM "CollectionUsers" cu
|
FROM "OrganizationUser" AS ou
|
||||||
INNER JOIN "OrganizationUser" ou ON cu."OrganizationUserId" = ou."Id"
|
WHERE ou."Type" = 3 OR (
|
||||||
WHERE ou."Type" = 3 OR (ou."Permissions" IS NOT NULL AND
|
ou."Permissions" IS NOT NULL AND
|
||||||
JSON_VALID(ou."Permissions") AND json_extract(ou."Permissions", '$.editAssignedCollections') = 'true')
|
JSON_VALID(ou."Permissions") > 0 AND
|
||||||
|
JSON_EXTRACT(ou."Permissions", '$.editAssignedCollections') = 'true'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Insert rows to CollectionUser for Managers and users with 'EditAssignedCollections' permission assigned to groups with collection access
|
||||||
|
INSERT INTO "CollectionUsers" ("CollectionId", "OrganizationUserId", "ReadOnly", "HidePasswords", "Manage")
|
||||||
|
SELECT cg."CollectionId", ou."Id", 0, 0, 1
|
||||||
|
FROM "CollectionGroups" AS cg
|
||||||
|
INNER JOIN "GroupUser" AS gu ON cg."GroupId" = gu."GroupId"
|
||||||
|
INNER JOIN "OrganizationUser" AS ou ON gu."OrganizationUserId" = ou."Id"
|
||||||
|
WHERE (ou."Type" = 3 OR
|
||||||
|
(ou."Permissions" IS NOT NULL AND JSON_VALID(ou."Permissions") > 0 AND JSON_EXTRACT(ou."Permissions", '$.editAssignedCollections')) = 'true')
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT 1 FROM "CollectionUsers" AS cu
|
||||||
|
WHERE cu."CollectionId" = cg."CollectionId" AND cu."OrganizationUserId" = ou."Id"
|
||||||
);
|
);
|
||||||
|
2320
util/SqliteMigrations/Migrations/20231219154710_FCAccessAllCollectionGroups.Designer.cs
generated
Normal file
2320
util/SqliteMigrations/Migrations/20231219154710_FCAccessAllCollectionGroups.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,21 @@
|
|||||||
|
using Bit.Core.Utilities;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace Bit.SqliteMigrations.Migrations;
|
||||||
|
|
||||||
|
public partial class FCAccessAllCollectionGroups : Migration
|
||||||
|
{
|
||||||
|
private const string _accessAllCollectionGroupsScript = "SqliteMigrations.HelperScripts.2023-12-06_00_AccessAllCollectionGroups.sql";
|
||||||
|
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.Sql(CoreHelpers.GetEmbeddedResourceContentsAsync(_accessAllCollectionGroupsScript));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
throw new Exception("Irreversible migration");
|
||||||
|
}
|
||||||
|
}
|
@ -11,8 +11,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
|||||||
namespace Bit.SqliteMigrations.Migrations
|
namespace Bit.SqliteMigrations.Migrations
|
||||||
{
|
{
|
||||||
[DbContext(typeof(DatabaseContext))]
|
[DbContext(typeof(DatabaseContext))]
|
||||||
[Migration("20231217125605_FlexibleCollections")]
|
[Migration("20231219154737_FCAccessAllCollectionUsers")]
|
||||||
partial class FlexibleCollections
|
partial class FCAccessAllCollectionUsers
|
||||||
{
|
{
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
@ -0,0 +1,21 @@
|
|||||||
|
using Bit.Core.Utilities;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace Bit.SqliteMigrations.Migrations;
|
||||||
|
|
||||||
|
public partial class FCAccessAllCollectionUsers : Migration
|
||||||
|
{
|
||||||
|
private const string _accessAllCollectionUsersScript = "SqliteMigrations.HelperScripts.2023-12-06_01_AccessAllCollectionUsers.sql";
|
||||||
|
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.Sql(CoreHelpers.GetEmbeddedResourceContentsAsync(_accessAllCollectionUsersScript));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
throw new Exception("Irreversible migration");
|
||||||
|
}
|
||||||
|
}
|
2320
util/SqliteMigrations/Migrations/20231219154820_FCManagersEditAssignedCollectionUsers.Designer.cs
generated
Normal file
2320
util/SqliteMigrations/Migrations/20231219154820_FCManagersEditAssignedCollectionUsers.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -5,16 +5,12 @@ using Microsoft.EntityFrameworkCore.Migrations;
|
|||||||
|
|
||||||
namespace Bit.SqliteMigrations.Migrations;
|
namespace Bit.SqliteMigrations.Migrations;
|
||||||
|
|
||||||
public partial class FlexibleCollections : Migration
|
public partial class FCManagersEditAssignedCollectionUsers : 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";
|
private const string _managersEditAssignedCollectionUsersScript = "SqliteMigrations.HelperScripts.2023-12-06_02_ManagersEditAssignedCollectionUsers.sql";
|
||||||
|
|
||||||
protected override void Up(MigrationBuilder migrationBuilder)
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
{
|
{
|
||||||
migrationBuilder.Sql(CoreHelpers.GetEmbeddedResourceContentsAsync(_accessAllCollectionGroupsScript));
|
|
||||||
migrationBuilder.Sql(CoreHelpers.GetEmbeddedResourceContentsAsync(_accessAllCollectionUsersScript));
|
|
||||||
migrationBuilder.Sql(CoreHelpers.GetEmbeddedResourceContentsAsync(_managersEditAssignedCollectionUsersScript));
|
migrationBuilder.Sql(CoreHelpers.GetEmbeddedResourceContentsAsync(_managersEditAssignedCollectionUsersScript));
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue
Block a user