2023-12-27 22:23:10 +01:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
)
|
|
|
|
|
|
|
|
var db *sql.DB
|
|
|
|
|
2024-01-04 02:08:48 +01:00
|
|
|
const DB_VERSION = 1
|
|
|
|
|
2023-12-27 22:23:10 +01:00
|
|
|
func InitDB(driver string, connStr string) error {
|
|
|
|
var err error
|
|
|
|
db, err = sql.Open(driver, connStr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.Exec(`
|
2024-01-04 02:08:48 +01:00
|
|
|
CREATE TABLE IF NOT EXISTS schema_version (
|
|
|
|
version INTEGER PRIMARY KEY,
|
|
|
|
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
|
|
);
|
2023-12-27 22:23:10 +01:00
|
|
|
CREATE TABLE IF NOT EXISTS projects (
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
title TEXT
|
|
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS cards (
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
2023-12-29 01:49:09 +01:00
|
|
|
project_id INTEGER,
|
2023-12-27 22:23:10 +01:00
|
|
|
title TEXT,
|
|
|
|
content TEXT,
|
2023-12-29 01:49:09 +01:00
|
|
|
FOREIGN KEY(project_id) REFERENCES projects(id)
|
2023-12-29 03:08:08 +01:00
|
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS tags (
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
project_id INTEGER,
|
|
|
|
title TEXT,
|
|
|
|
type int,
|
|
|
|
FOREIGN KEY(project_id) REFERENCES projects(id)
|
|
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS cardtags (
|
|
|
|
card_id INTEGER,
|
|
|
|
tag_id INTEGER,
|
2023-12-31 04:35:52 +01:00
|
|
|
option_id TEXT,
|
2023-12-29 03:08:08 +01:00
|
|
|
value TEXT,
|
2023-12-30 16:03:44 +01:00
|
|
|
PRIMARY KEY(card_id, tag_id),
|
2023-12-29 03:08:08 +01:00
|
|
|
FOREIGN KEY(card_id) REFERENCES cards(id)
|
|
|
|
FOREIGN KEY(tag_id) REFERENCES tags(id)
|
2023-12-31 04:35:52 +01:00
|
|
|
FOREIGN KEY(option_id) REFERENCES tagsoptions(id)
|
2023-12-31 01:38:17 +01:00
|
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS tagsoptions (
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
tag_id INTEGER,
|
|
|
|
value TEXT,
|
|
|
|
FOREIGN KEY(tag_id) REFERENCES tags(id)
|
2023-12-27 22:23:10 +01:00
|
|
|
);
|
2023-12-31 05:53:46 +01:00
|
|
|
CREATE TABLE IF NOT EXISTS views (
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
project_id INTEGER,
|
|
|
|
primary_tag_id INTEGER,
|
|
|
|
secondary_tag_id INTEGER,
|
|
|
|
title TEXT,
|
2024-01-04 02:08:48 +01:00
|
|
|
sort_tag_id INTEGER,
|
|
|
|
sort_direction INTEGER,
|
2023-12-31 05:53:46 +01:00
|
|
|
FOREIGN KEY(project_id) REFERENCES projects(id),
|
|
|
|
FOREIGN KEY(primary_tag_id) REFERENCES tags(id),
|
|
|
|
FOREIGN KEY(secondary_tag_id) REFERENCES tags(id)
|
|
|
|
);
|
2024-01-04 02:08:48 +01:00
|
|
|
|
|
|
|
INSERT INTO schema_version (version)
|
|
|
|
SELECT ? WHERE NOT EXISTS (SELECT 1 FROM schema_version);
|
|
|
|
`, DB_VERSION)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return updateDB()
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateDB() error {
|
|
|
|
|
|
|
|
var currentVersion int
|
|
|
|
|
|
|
|
err := db.QueryRow("SELECT MAX(version) FROM schema_version").Scan(¤tVersion)
|
2023-12-27 22:23:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-04 02:08:48 +01:00
|
|
|
|
|
|
|
if currentVersion < 2 {
|
|
|
|
_, err := db.Exec(`
|
|
|
|
ALTER TABLE views
|
|
|
|
ADD COLUMN sort_tag_id INTEGER;
|
|
|
|
ALTER TABLE views
|
|
|
|
ADD COLUMN sort_direction INTEGER;
|
|
|
|
|
|
|
|
INSERT INTO schema_version (version)
|
|
|
|
SELECT ? WHERE NOT EXISTS (SELECT 1 FROM schema_version WHERE version = ?);
|
|
|
|
`, 2, 2)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-27 22:23:10 +01:00
|
|
|
return nil
|
|
|
|
}
|