bliss/migrations/000_bootstrap_db.sql

160 lines
3.6 KiB
MySQL
Raw Permalink Normal View History

2024-02-03 13:22:50 -05:00
CREATE TABLE users (
2024-02-24 11:12:14 -05:00
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL,
password TEXT NOT NULL,
2024-06-02 00:39:30 -04:00
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
2024-02-24 11:12:14 -05:00
UNIQUE(username)
2024-02-03 13:22:50 -05:00
);
CREATE TABLE structures (
2024-02-24 11:12:14 -05:00
id INTEGER PRIMARY KEY AUTOINCREMENT,
2024-02-03 13:22:50 -05:00
name TEXT NOT NULL,
user_id INTEGER,
2024-06-04 03:01:19 -04:00
route_prefix TEXT,
2024-06-08 15:30:28 -04:00
head_injection TEXT,
2024-06-04 03:01:19 -04:00
cloned_from INTEGER,
2024-06-02 00:39:30 -04:00
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
2024-02-24 11:12:14 -05:00
FOREIGN KEY(user_id) REFERENCES users(id),
2024-06-04 03:01:19 -04:00
FOREIGN KEY(cloned_from) REFERENCES structures(id),
2024-02-24 11:12:14 -05:00
UNIQUE(name, user_id)
2024-02-03 13:22:50 -05:00
);
CREATE TABLE dbs (
2024-02-24 11:12:14 -05:00
id INTEGER PRIMARY KEY AUTOINCREMENT,
2024-02-03 13:22:50 -05:00
name TEXT NOT NULL,
structure_id INTEGER NOT NULL,
2024-06-02 00:39:30 -04:00
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
library TEXT NOT NULL DEFAULT "",
FOREIGN KEY(structure_id) REFERENCES structures(id),
UNIQUE(name, structure_id)
2024-02-03 13:22:50 -05:00
);
CREATE TABLE templates (
2024-02-24 11:12:14 -05:00
id INTEGER PRIMARY KEY AUTOINCREMENT,
2024-06-02 00:39:30 -04:00
name TEXT NOT NULL,
content TEXT,
structure_id INTEGER,
2024-06-02 10:20:01 -04:00
test_object TEXT DEFAULT "it = {}",
2024-06-02 00:39:30 -04:00
engine TEXT NOT NULL DEFAULT "eta",
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
2024-06-03 20:19:26 -04:00
FOREIGN KEY(structure_id) REFERENCES structures(id) UNIQUE(name, structure_id)
2024-02-03 13:22:50 -05:00
);
2024-02-24 11:12:14 -05:00
2024-02-03 13:22:50 -05:00
CREATE TABLE routes (
2024-02-24 11:12:14 -05:00
id INTEGER PRIMARY KEY AUTOINCREMENT,
2024-06-08 18:29:31 -04:00
verb TEXT CHECK(verb IN ('POST', 'GET', 'PUT', 'DELETE', 'WS')) NOT NULL,
2024-02-03 13:22:50 -05:00
path TEXT NOT NULL,
structure_id INTEGER,
handler TEXT NOT NULL,
2024-06-08 18:29:31 -04:00
error TEXT,
2024-06-02 00:39:30 -04:00
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
2024-02-24 11:12:14 -05:00
FOREIGN KEY(structure_id) REFERENCES structures(id),
2024-06-02 00:39:30 -04:00
UNIQUE(verb, path, structure_id)
2024-02-03 13:22:50 -05:00
);
2024-02-24 11:12:14 -05:00
2024-02-03 13:22:50 -05:00
CREATE TABLE structure_dbs (
db_id INTEGER,
structure_id INTEGER,
2024-06-02 00:39:30 -04:00
alias TEXT NOT NULL,
2024-06-03 20:19:26 -04:00
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
2024-02-03 13:22:50 -05:00
PRIMARY KEY(db_id, structure_id),
2024-02-24 11:12:14 -05:00
FOREIGN KEY(db_id) REFERENCES dbs(id),
FOREIGN KEY(structure_id) REFERENCES structures(id)
2024-06-08 11:42:27 -04:00
UNIQUE(alias, structure_id)
2024-02-03 13:22:50 -05:00
);
2024-02-24 11:12:14 -05:00
2024-06-03 20:19:26 -04:00
CREATE TABLE files (
id INTEGER PRIMARY KEY,
structure_id INTEGER,
name TEXT,
path TEXT,
mime_type TEXT,
mime_subtype TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (structure_id) REFERENCES structures(id),
UNIQUE(path, structure_id)
);
2024-06-02 00:39:30 -04:00
INSERT INTO
structures (id, name, user_id)
VALUES
(0, 'prime', NULL);
INSERT INTO
dbs (id, name, structure_id, library)
VALUES
(0, 'prime', 0, '');
2024-06-02 00:39:30 -04:00
-- ===========================
-- TRIGGERS
-- ===========================
CREATE TRIGGER update_users_updated_at
2024-06-03 20:19:26 -04:00
AFTER
UPDATE
ON users FOR EACH ROW BEGIN
UPDATE
users
SET
updated_at = CURRENT_TIMESTAMP
WHERE
id = OLD.id;
2024-06-02 00:39:30 -04:00
END;
CREATE TRIGGER update_structures_updated_at
2024-06-03 20:19:26 -04:00
AFTER
UPDATE
ON structures FOR EACH ROW BEGIN
UPDATE
structures
SET
updated_at = CURRENT_TIMESTAMP
WHERE
id = OLD.id;
2024-06-02 00:39:30 -04:00
END;
CREATE TRIGGER update_dbs_updated_at
2024-06-03 20:19:26 -04:00
AFTER
UPDATE
ON dbs FOR EACH ROW BEGIN
UPDATE
dbs
SET
updated_at = CURRENT_TIMESTAMP
WHERE
id = OLD.id;
2024-06-02 00:39:30 -04:00
END;
CREATE TRIGGER update_templates_updated_at
2024-06-03 20:19:26 -04:00
AFTER
UPDATE
ON templates FOR EACH ROW BEGIN
UPDATE
templates
SET
updated_at = CURRENT_TIMESTAMP
WHERE
id = OLD.id;
2024-06-02 00:39:30 -04:00
END;
CREATE TRIGGER update_routes_updated_at
2024-06-03 20:19:26 -04:00
AFTER
UPDATE
ON routes FOR EACH ROW BEGIN
UPDATE
routes
SET
updated_at = CURRENT_TIMESTAMP
WHERE
id = OLD.id;
END;