user-role-cluster (#761)
Some checks failed
CI/CD Pipeline / deploy-staging (push) Blocked by required conditions
CI/CD Pipeline / deploy-main (push) Blocked by required conditions
CI/CD Pipeline / test (push) Has been cancelled

Reviewed-on: #761
This commit is contained in:
2024-10-11 12:39:23 +02:00
parent 0cc72f17a1
commit a6a143f238
10 changed files with 199 additions and 32 deletions

View File

@ -27,7 +27,8 @@ CREATE TABLE IF NOT EXISTS "family" (
CREATE TABLE IF NOT EXISTS "role" (
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"name" text NOT NULL UNIQUE
"name" text NOT NULL UNIQUE,
"cluster" text
);
CREATE TABLE IF NOT EXISTS "user_role" (
@ -220,3 +221,20 @@ CREATE TABLE IF NOT EXISTS "distance" (
"distance_in_km" integer NOT NULL
);
CREATE TRIGGER IF NOT EXISTS prevent_multiple_roles_same_cluster
BEFORE INSERT ON user_role
BEGIN
SELECT CASE
WHEN EXISTS (
SELECT 1
FROM user_role ur
JOIN role r1 ON ur.role_id = r1.id
JOIN role r2 ON r1."cluster" = r2."cluster"
WHERE ur.user_id = NEW.user_id
AND r2.id = NEW.role_id
AND r1.id != NEW.role_id
)
THEN RAISE(ABORT, 'User already has a role in this cluster')
END;
END;