New project_role table to represent possible roles

This commit is contained in:
Imbus 2024-03-08 07:33:31 +01:00
parent 1c5cbd768d
commit 553ba2c7c6
2 changed files with 12 additions and 10 deletions

View file

@ -0,0 +1,9 @@
-- This table represents the possible role a user can have in a project.
-- It has nothing to do with the site admin table.
CREATE TABLE IF NOT EXISTS project_role (
p_role TEXT PRIMARY KEY
);
-- Insert the possible roles a user can have in a project.
INSERT OR IGNORE INTO project_role (p_role) VALUES ('admin');
INSERT OR IGNORE INTO project_role (p_role) VALUES ('member');

View file

@ -1,16 +1,9 @@
CREATE TABLE IF NOT EXISTS user_roles ( CREATE TABLE IF NOT EXISTS user_roles (
user_id INTEGER NOT NULL, user_id INTEGER NOT NULL,
project_id INTEGER NOT NULL, project_id INTEGER NOT NULL,
role STRING NOT NULL, -- 'admin' or 'member' p_role TEXT NOT NULL, -- 'admin' or 'member'
FOREIGN KEY (user_id) REFERENCES users (id) FOREIGN KEY (user_id) REFERENCES users (id)
FOREIGN KEY (project_id) REFERENCES projects (id) FOREIGN KEY (project_id) REFERENCES projects (id)
FOREIGN KEY (p_role) REFERENCES project_role (p_role)
PRIMARY KEY (user_id, project_id) PRIMARY KEY (user_id, project_id)
); );
-- Make sure that the role is either 'admin' or 'member'
CREATE TRIGGER IF NOT EXISTS user_role_admin_or_member
BEFORE INSERT ON user_roles
FOR EACH ROW
BEGIN
SELECT RAISE(ABORT, 'Invalid role') WHERE NEW.role NOT IN ('admin', 'member');
END;