Moving the migrations directory into database for embedding
This commit is contained in:
parent
77e3324f79
commit
34c0712825
7 changed files with 8 additions and 2 deletions
|
@ -1,6 +1,7 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
@ -15,6 +16,9 @@ type Db struct {
|
|||
*sqlx.DB
|
||||
}
|
||||
|
||||
//go:embed migrations
|
||||
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 = ?"
|
||||
|
||||
|
@ -62,7 +66,8 @@ func (d *Db) AddProject(name string, description string, username string) error
|
|||
// Reads a directory of migration files and applies them to the database.
|
||||
// This will eventually be used on an embedded directory
|
||||
func (d *Db) Migrate(dirname string) error {
|
||||
files, err := os.ReadDir(dirname)
|
||||
// Read the embedded scripts directory
|
||||
files, err := scripts.ReadDir("migrations")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -75,7 +80,8 @@ func (d *Db) Migrate(dirname string) error {
|
|||
continue
|
||||
}
|
||||
|
||||
sqlFile := filepath.Join(dirname, file.Name())
|
||||
// This is perhaps not the most elegant way to do this
|
||||
sqlFile := filepath.Join("migrations", file.Name())
|
||||
sqlBytes, err := os.ReadFile(sqlFile)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
9
backend/internal/database/migrations/0010_users.sql
Normal file
9
backend/internal/database/migrations/0010_users.sql
Normal file
|
@ -0,0 +1,9 @@
|
|||
CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER PRIMARY KEY,
|
||||
userId TEXT DEFAULT (HEX(RANDOMBLOB(4))) NOT NULL UNIQUE,
|
||||
username VARCHAR(255) NOT NULL UNIQUE,
|
||||
password VARCHAR(255) NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS users_username_index ON users (username);
|
||||
CREATE INDEX IF NOT EXISTS users_userId_index ON users (userId);
|
11
backend/internal/database/migrations/0020_projects.sql
Normal file
11
backend/internal/database/migrations/0020_projects.sql
Normal file
|
@ -0,0 +1,11 @@
|
|||
CREATE TABLE IF NOT EXISTS projects (
|
||||
id INTEGER PRIMARY KEY,
|
||||
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)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS projects_projectId_index ON projects (projectId);
|
||||
CREATE INDEX IF NOT EXISTS projects_user_id_index ON projects (user_id);
|
19
backend/internal/database/migrations/0030_time_reports.sql
Normal file
19
backend/internal/database/migrations/0030_time_reports.sql
Normal file
|
@ -0,0 +1,19 @@
|
|||
CREATE TABLE IF NOT EXISTS time_reports (
|
||||
id INTEGER PRIMARY KEY,
|
||||
reportId TEXT DEFAULT (HEX(RANDOMBLOB(6))) NOT NULL UNIQUE,
|
||||
project_id INTEGER NOT NULL,
|
||||
start DATETIME NOT NULL,
|
||||
end DATETIME NOT NULL,
|
||||
FOREIGN KEY (project_id) REFERENCES projects (id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE TRIGGER IF NOT EXISTS time_reports_start_before_end
|
||||
BEFORE INSERT ON time_reports
|
||||
FOR EACH ROW
|
||||
BEGIN
|
||||
SELECT
|
||||
CASE
|
||||
WHEN NEW.start >= NEW.end THEN
|
||||
RAISE (ABORT, 'start must be before end')
|
||||
END;
|
||||
END;
|
|
@ -0,0 +1,9 @@
|
|||
CREATE TABLE IF NOT EXISTS report_collection (
|
||||
id INTEGER PRIMARY KEY,
|
||||
owner_id INTEGER NOT NULL,
|
||||
project_id INTEGER NOT NULL,
|
||||
date DATE NOT NULL,
|
||||
signed_by INTEGER, -- NULL if not signed
|
||||
FOREIGN KEY (owner_id) REFERENCES users (id)
|
||||
FOREIGN KEY (signed_by) REFERENCES users (id)
|
||||
);
|
7
backend/internal/database/migrations/0050_user_roles.sql
Normal file
7
backend/internal/database/migrations/0050_user_roles.sql
Normal file
|
@ -0,0 +1,7 @@
|
|||
CREATE TABLE IF NOT EXISTS user_roles (
|
||||
user_id INTEGER NOT NULL,
|
||||
project_id INTEGER NOT NULL,
|
||||
role STRING NOT NULL, -- 'admin' or 'member'
|
||||
FOREIGN KEY (user_id) REFERENCES users (id)
|
||||
FOREIGN KEY (project_id) REFERENCES projects (id)
|
||||
);
|
14
backend/internal/database/migrations/README.md
Normal file
14
backend/internal/database/migrations/README.md
Normal file
|
@ -0,0 +1,14 @@
|
|||
# Database migrations
|
||||
|
||||
This directory contains all the database migrations for the backend.
|
||||
|
||||
[!WARNING]
|
||||
Keep in mind that these migrations are **not yet stable**.
|
||||
|
||||
## Running migrations
|
||||
|
||||
In the root of the backend directory, run:
|
||||
|
||||
```bash
|
||||
make migrate
|
||||
```
|
Loading…
Add table
Add a link
Reference in a new issue