Compare commits
6 commits
1c5cbd768d
...
944f9b6ccc
Author | SHA1 | Date | |
---|---|---|---|
![]() |
944f9b6ccc | ||
![]() |
12536d5e89 | ||
![]() |
2351a0cb4a | ||
![]() |
a77b388f8e | ||
![]() |
f5d5eee267 | ||
![]() |
553ba2c7c6 |
6 changed files with 42 additions and 16 deletions
25
.github/workflows/sqlite3-migrations.yml
vendored
Normal file
25
.github/workflows/sqlite3-migrations.yml
vendored
Normal file
|
@ -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
|
|
@ -43,6 +43,7 @@ update:
|
||||||
|
|
||||||
# Migration target
|
# Migration target
|
||||||
migrate:
|
migrate:
|
||||||
|
@echo "If this ever fails, run make clean and try again"
|
||||||
@echo "Migrating database $(DB_FILE) using SQL scripts in $(MIGRATIONS_DIR)"
|
@echo "Migrating database $(DB_FILE) using SQL scripts in $(MIGRATIONS_DIR)"
|
||||||
@for file in $(wildcard $(MIGRATIONS_DIR)/*.sql); do \
|
@for file in $(wildcard $(MIGRATIONS_DIR)/*.sql); do \
|
||||||
echo "Applying migration: $$file"; \
|
echo "Applying migration: $$file"; \
|
||||||
|
|
|
@ -2,7 +2,6 @@ package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"embed"
|
"embed"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
@ -33,7 +32,7 @@ type Db struct {
|
||||||
var scripts embed.FS
|
var scripts embed.FS
|
||||||
|
|
||||||
const userInsert = "INSERT INTO users (username, password) VALUES (?, ?)"
|
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 promoteToAdmin = "INSERT INTO site_admin (admin_id) SELECT id FROM users WHERE username = ?"
|
||||||
|
|
||||||
// const addTimeReport = ""
|
// const addTimeReport = ""
|
||||||
|
@ -127,7 +126,6 @@ func (d *Db) Migrate(dirname string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Println("Executed SQL file:", file.Name())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if tr.Commit() != nil {
|
if tr.Commit() != nil {
|
||||||
|
|
|
@ -3,9 +3,9 @@ CREATE TABLE IF NOT EXISTS projects (
|
||||||
projectId TEXT DEFAULT (HEX(RANDOMBLOB(4))) NOT NULL UNIQUE,
|
projectId TEXT DEFAULT (HEX(RANDOMBLOB(4))) NOT NULL UNIQUE,
|
||||||
name VARCHAR(255) NOT NULL UNIQUE,
|
name VARCHAR(255) NOT NULL UNIQUE,
|
||||||
description TEXT NOT NULL,
|
description TEXT NOT NULL,
|
||||||
user_id INTEGER NOT NULL,
|
owner_user_id INTEGER NOT NULL,
|
||||||
FOREIGN KEY (user_id) REFERENCES users (id)
|
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_projectId_index ON projects (projectId);
|
||||||
CREATE INDEX IF NOT EXISTS projects_user_id_index ON projects (user_id);
|
CREATE INDEX IF NOT EXISTS projects_user_id_index ON projects (owner_user_id);
|
|
@ -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');
|
|
@ -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;
|
|
Loading…
Reference in a new issue