From 553ba2c7c64fab798e6065695b9de67bb0528fb0 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Fri, 8 Mar 2024 07:33:31 +0100 Subject: [PATCH 1/6] New project_role table to represent possible roles --- .../database/migrations/0049_project_role.sql | 9 +++++++++ .../database/migrations/0050_user_roles.sql | 13 +++---------- 2 files changed, 12 insertions(+), 10 deletions(-) create mode 100644 backend/internal/database/migrations/0049_project_role.sql diff --git a/backend/internal/database/migrations/0049_project_role.sql b/backend/internal/database/migrations/0049_project_role.sql new file mode 100644 index 0000000..8716800 --- /dev/null +++ b/backend/internal/database/migrations/0049_project_role.sql @@ -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'); diff --git a/backend/internal/database/migrations/0050_user_roles.sql b/backend/internal/database/migrations/0050_user_roles.sql index 56e597b..aad25f7 100644 --- a/backend/internal/database/migrations/0050_user_roles.sql +++ b/backend/internal/database/migrations/0050_user_roles.sql @@ -1,16 +1,9 @@ CREATE TABLE IF NOT EXISTS user_roles ( user_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 (project_id) REFERENCES projects (id) + FOREIGN KEY (p_role) REFERENCES project_role (p_role) 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; \ No newline at end of file +); \ No newline at end of file From f5d5eee2670f13719d20496d888a93152a7a6868 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Fri, 8 Mar 2024 07:33:58 +0100 Subject: [PATCH 2/6] Breaking changes in projects table related to naming --- backend/internal/database/migrations/0020_projects.sql | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/internal/database/migrations/0020_projects.sql b/backend/internal/database/migrations/0020_projects.sql index 8592e75..adfb818 100644 --- a/backend/internal/database/migrations/0020_projects.sql +++ b/backend/internal/database/migrations/0020_projects.sql @@ -3,9 +3,9 @@ CREATE TABLE IF NOT EXISTS projects ( projectId TEXT DEFAULT (HEX(RANDOMBLOB(4))) NOT NULL UNIQUE, name VARCHAR(255) NOT NULL UNIQUE, description TEXT NOT NULL, - user_id INTEGER NOT NULL, - FOREIGN KEY (user_id) REFERENCES users (id) + owner_user_id INTEGER NOT NULL, + FOREIGN KEY (owner_user_id) REFERENCES users (id) ); CREATE INDEX IF NOT EXISTS projects_projectId_index ON projects (projectId); -CREATE INDEX IF NOT EXISTS projects_user_id_index ON projects (user_id); \ No newline at end of file +CREATE INDEX IF NOT EXISTS projects_user_id_index ON projects (owner_user_id); \ No newline at end of file From a77b388f8e7bbac457e48049402de3bc1c684c93 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Fri, 8 Mar 2024 07:34:26 +0100 Subject: [PATCH 3/6] Warning in make migrate target --- backend/Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/backend/Makefile b/backend/Makefile index dcc79b4..cb0bb64 100644 --- a/backend/Makefile +++ b/backend/Makefile @@ -43,6 +43,7 @@ update: # Migration target migrate: + @echo "If this ever fails, run make clean and try again" @echo "Migrating database $(DB_FILE) using SQL scripts in $(MIGRATIONS_DIR)" @for file in $(wildcard $(MIGRATIONS_DIR)/*.sql); do \ echo "Applying migration: $$file"; \ From 2351a0cb4a7ed54e3a9c831c70307968552ad9be Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Fri, 8 Mar 2024 07:42:11 +0100 Subject: [PATCH 4/6] Github actions CI for db migrations --- .github/workflows/sqlite3-migrations.yml | 25 ++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 .github/workflows/sqlite3-migrations.yml diff --git a/.github/workflows/sqlite3-migrations.yml b/.github/workflows/sqlite3-migrations.yml new file mode 100644 index 0000000..0e53b98 --- /dev/null +++ b/.github/workflows/sqlite3-migrations.yml @@ -0,0 +1,25 @@ +name: SQLite3 Migrations + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +jobs: + build: + + runs-on: ubuntu-latest + + defaults: + run: + working-directory: ./backend + + steps: + - uses: actions/checkout@v3 + - name: Install SQLite3 + run: sudo apt-get install sqlite3 + - name: Install Make + run: sudo apt-get install make + - name: Run Migrations + run: make migrate From 12536d5e89ef66628867982fbc7fa1a65b18d80e Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Fri, 8 Mar 2024 07:47:32 +0100 Subject: [PATCH 5/6] Fixing inconsistent sql in db interface --- backend/internal/database/db.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/internal/database/db.go b/backend/internal/database/db.go index 84937f1..3377837 100644 --- a/backend/internal/database/db.go +++ b/backend/internal/database/db.go @@ -33,7 +33,7 @@ type Db struct { var scripts embed.FS const userInsert = "INSERT INTO users (username, password) VALUES (?, ?)" -const projectInsert = "INSERT INTO projects (name, description, user_id) SELECT ?, ?, id FROM users WHERE username = ?" +const projectInsert = "INSERT INTO projects (name, description, owner_user_id) SELECT ?, ?, id FROM users WHERE username = ?" const promoteToAdmin = "INSERT INTO site_admin (admin_id) SELECT id FROM users WHERE username = ?" // const addTimeReport = "" From 944f9b6ccc68566c6d4b6869528d4a9420d4a3c3 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Fri, 8 Mar 2024 07:48:25 +0100 Subject: [PATCH 6/6] Removed overly verbose debug printing from db migration --- backend/internal/database/db.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/backend/internal/database/db.go b/backend/internal/database/db.go index 3377837..6bb113b 100644 --- a/backend/internal/database/db.go +++ b/backend/internal/database/db.go @@ -2,7 +2,6 @@ package database import ( "embed" - "log" "os" "path/filepath" @@ -127,7 +126,6 @@ func (d *Db) Migrate(dirname string) error { if err != nil { return err } - log.Println("Executed SQL file:", file.Name()) } if tr.Commit() != nil {