diff options
author | Zach Berwaldt <zberwaldt@tutamail.com> | 2024-03-16 10:48:52 -0400 |
---|---|---|
committer | Zach Berwaldt <zberwaldt@tutamail.com> | 2024-03-16 10:48:58 -0400 |
commit | 968481312058aa58261d41cf3460b45109fec240 (patch) | |
tree | 9704a77e53968fe1c6094fa8eb9714b59ed59b8d /db/sql | |
parent | 947bbd510ea104d3a631b3200da9ed239cfd6e80 (diff) |
clean up database scripts, start readmes.
Diffstat (limited to 'db/sql')
-rw-r--r-- | db/sql/seed.sql | 5 | ||||
-rw-r--r-- | db/sql/tables.sql | 39 | ||||
-rw-r--r-- | db/sql/triggers.sql | 11 | ||||
-rw-r--r-- | db/sql/views.sql | 27 |
4 files changed, 82 insertions, 0 deletions
diff --git a/db/sql/seed.sql b/db/sql/seed.sql new file mode 100644 index 0000000..41d6e46 --- /dev/null +++ b/db/sql/seed.sql | |||
@@ -0,0 +1,5 @@ | |||
1 | -- create default sizes for sizes lookup table. | ||
2 | INSERT OR IGNORE INTO Sizes (id, size) VALUES (1, 8), (2, 16), (3, 24), (4, 32), (5, 40), (6, 48); | ||
3 | |||
4 | -- create default preferences. | ||
5 | INSERT OR IGNORE INTO Preferences (user_id) VALUES (1), (2); | ||
diff --git a/db/sql/tables.sql b/db/sql/tables.sql new file mode 100644 index 0000000..0fbfef8 --- /dev/null +++ b/db/sql/tables.sql | |||
@@ -0,0 +1,39 @@ | |||
1 | -- user table for users. | ||
2 | CREATE TABLE IF NOT EXISTS Users ( | ||
3 | id INTEGER PRIMARY KEY, | ||
4 | password TEXT UNIQUE NOT NULL, | ||
5 | uuid TEXT UNIQUE NOT NULL, | ||
6 | name TEXT UNIQUE NOT NULL | ||
7 | ); | ||
8 | |||
9 | -- statistics table for users to log their consumption | ||
10 | CREATE TABLE IF NOT EXISTS Statistics ( | ||
11 | id INTEGER PRIMARY KEY, | ||
12 | date DATETIME NOT NULL, | ||
13 | user_id INT NOT NULL, | ||
14 | quantity INT | ||
15 | ); | ||
16 | |||
17 | -- preferences table for a user. | ||
18 | CREATE TABLE IF NOT EXISTS Preferences ( | ||
19 | id INTEGER PRIMARY KEY, | ||
20 | color TEXT NOT NULL DEFAULT "#000000", | ||
21 | user_id INT UNIQUE NOT NULL, | ||
22 | size_id INT NOT NULL DEFAULT 1, | ||
23 | FOREIGN KEY(user_id) REFERENCES Users(id) ON DELETE CASCADE, | ||
24 | FOREIGN KEY(size_id) REFERENCES Sizes(id) | ||
25 | ); | ||
26 | |||
27 | -- lookup table for sizes. | ||
28 | CREATE TABLE IF NOT EXISTS Sizes ( | ||
29 | id INTEGER PRIMARY KEY, | ||
30 | size INT NOT NULL, | ||
31 | unit TEXT DEFAULT "oz" | ||
32 | ); | ||
33 | |||
34 | CREATE TABLE IF NOT EXISTS APIToken ( | ||
35 | id INTEGER PRIMARY KEY, | ||
36 | token TEXT NOT NULL, | ||
37 | user_id INTEGER NOT NULL, | ||
38 | FOREIGN KEY(user_id) REFERENCES Users(id) ON DELETE CASCADE | ||
39 | ); \ No newline at end of file | ||
diff --git a/db/sql/triggers.sql b/db/sql/triggers.sql new file mode 100644 index 0000000..3827642 --- /dev/null +++ b/db/sql/triggers.sql | |||
@@ -0,0 +1,11 @@ | |||
1 | CREATE TRIGGER IF NOT EXISTS enforce_size_id | ||
2 | BEFORE INSERT ON Preferences | ||
3 | BEGIN | ||
4 | SELECT | ||
5 | CASE | ||
6 | WHEN ( | ||
7 | SELECT COUNT(*) FROM Sizes WHERE id = new.size_id | ||
8 | ) = 0 | ||
9 | THEN RAISE(ABORT, 'Size does not exist') | ||
10 | END; | ||
11 | END; | ||
diff --git a/db/sql/views.sql b/db/sql/views.sql new file mode 100644 index 0000000..c56286d --- /dev/null +++ b/db/sql/views.sql | |||
@@ -0,0 +1,27 @@ | |||
1 | CREATE VIEW IF NOT EXISTS aggregated_stats AS | ||
2 | SELECT u.uuid, SUM(s.quantity * s.size) from Statistics s INNER JOIN Users u ON u.id = s.user_id INNER JOIN Preferences p ON p.user_id = u.id INNER JOIN Size s ON s.id = p.size_id; | ||
3 | |||
4 | CREATE VIEW IF NOT EXISTS `DailyUserStatistics` AS | ||
5 | SELECT users.name, IFNULL(SUM(statistics.quantity), 0) as total, preferences.color as color | ||
6 | FROM users | ||
7 | LEFT JOIN statistics ON users.id = statistics.user_id AND DATE(statistics.date) = DATE('now', '-1 day') | ||
8 | LEFT JOIN preferences ON users.id = preferences.user_id | ||
9 | GROUP BY users.name; | ||
10 | |||
11 | |||
12 | CREATE VIEW IF NOT EXISTS `WeeklyStatisticsView` AS | ||
13 | WITH DateSequence(Dates) AS | ||
14 | ( | ||
15 | SELECT Date(CURRENT_DATE, '-7 day') | ||
16 | UNION ALL | ||
17 | SELECT Date(Dates, '+1 day') | ||
18 | FROM DateSequence | ||
19 | WHERE Date(Dates, '+1 day') < Date(CURRENT_DATE) | ||
20 | ) | ||
21 | SELECT DateSequence.Dates as 'date', | ||
22 | IFNULL(SUM(statistics.quantity), 0) AS 'total' | ||
23 | FROM DateSequence | ||
24 | LEFT JOIN statistics | ||
25 | ON Date(statistics.date) = DateSequence.Dates | ||
26 | GROUP BY DateSequence.Dates | ||
27 | ORDER BY DateSequence.Dates; \ No newline at end of file | ||