Commit
Author: Kevin Schoon [me@kevinschoon.com]
Hash: a7b0e2bc2a2ddd6c4898c4ee3bda000807e93a2a
Timestamp: Sat, 16 Nov 2024 17:23:24 +0000 (2 weeks ago)

+101 -107 +/-5 browse
move migrations back to the top level of the repository
1diff --git a/ayllu/migrations/20230602111216_init.sql b/ayllu/migrations/20230602111216_init.sql
2deleted file mode 100644
3index d3754fe..0000000
4--- a/ayllu/migrations/20230602111216_init.sql
5+++ /dev/null
6 @@ -1,51 +0,0 @@
7- CREATE TABLE authors (
8- id INTEGER PRIMARY KEY NOT NULL,
9- username TEXT NOT NULL,
10- email TEXT NOT NULL,
11- UNIQUE (username, email)
12- ) STRICT;
13-
14- CREATE TABLE languages (
15- id INTEGER PRIMARY KEY NOT NULL,
16- git_hash TEXT NOT NULL,
17- repo_path TEXT NOT NULL,
18- language TEXT NOT NULL,
19- loc INTEGER NOT NULL,
20- UNIQUE (git_hash, repo_path, language)
21- ) STRICT;
22-
23- CREATE TABLE jobs (
24- id INTEGER PRIMARY KEY,
25- created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
26- kind TEXT NOT NULL,
27- repo_path TEXT,
28- runtime INTEGER,
29- success INTEGER
30- ) STRICT;
31-
32- CREATE TABLE job_tracking (
33- id INTEGER PRIMARY KEY,
34- job_id INTEGER REFERENCES jobs(id) ON DELETE CASCADE NOT NULL,
35- repo_path TEXT NOT NULL,
36- git_hash TEXT NOT NULL,
37- kind TEXT NOT NULL,
38- UNIQUE (repo_path, git_hash, kind)
39- ) STRICT;
40-
41- CREATE TABLE contributions (
42- id INTEGER PRIMARY KEY,
43- git_hash TEXT NOT NULL,
44- repo_path TEXT NOT NULL,
45- time TEXT NOT NULL,
46- author_id INTEGER REFERENCES authors(id),
47- lines_added INTEGER NOT NULL,
48- lines_removed INTEGER NOT NULL
49- ) STRICT;
50-
51- CREATE TABLE contribution_tally (
52- id INTEGER PRIMARY KEY,
53- git_hash TEXT NOT NULL,
54- repo_path TEXT NOT NULL,
55- author_id INTEGER REFERENCES authors(id),
56- total INTEGER NOT NULL
57- ) STRICT;
58 diff --git a/ayllu/migrations/20241028092919_mail.sql b/ayllu/migrations/20241028092919_mail.sql
59deleted file mode 100644
60index e5f36cb..0000000
61--- a/ayllu/migrations/20241028092919_mail.sql
62+++ /dev/null
63 @@ -1,48 +0,0 @@
64- CREATE TABLE lists (
65- id INTEGER PRIMARY KEY,
66- name TEXT NOT NULL,
67- address TEXT NOT NULL UNIQUE,
68- description TEXT,
69- enabled INTEGER NOT NULL DEFAULT 0 CHECK (enabled IN (0, 1))
70- ) STRICT ;
71-
72- CREATE TABLE participants (
73- id INTEGER PRIMARY KEY,
74- address TEXT NOT NULL UNIQUE,
75- authorized_sender INTEGER NOT NULL DEFAULT 0 CHECK (authorized_sender IN (0, 1))
76- ) STRICT ;
77-
78- CREATE TABLE subscriptions (
79- id INTEGER PRIMARY KEY,
80- participant_id INTEGER NOT NULL REFERENCES participants(id),
81- list_id INTEGER NOT NULL REFERENCES lists(id),
82- UNIQUE(participant_id, list_id) ON CONFLICT REPLACE
83- ) STRICT ;
84-
85- CREATE TABLE outbox (
86- id INTEGER PRIMARY KEY,
87- message_id INTEGER NOT NULL REFERENCES messages(id),
88- recipient INTEGER NOT NULL REFERENCES participants(id),
89- delivered INTEGER NOT NULL DEFAULT 0 CHECK (delivered IN (0, 1)),
90- UNIQUE(message_id, recipient) ON CONFLICT REPLACE
91- ) STRICT ;
92-
93- CREATE TABLE messages (
94- id INTEGER PRIMARY KEY,
95- message_id TEXT NOT NULL UNIQUE,
96- list_id INTEGER NOT NULL REFERENCES lists(id),
97- reply_to INTEGER REFERENCES messages(id),
98- mail_from INTEGER NOT NULL REFERENCES participants(id),
99- message_body BLOB
100- ) STRICT ;
101-
102- CREATE TRIGGER handle_delivery
103- AFTER INSERT ON messages
104- FOR EACH ROW
105- BEGIN
106- INSERT INTO outbox (message_id, recipient)
107- SELECT NEW.id, participants.id FROM participants
108- LEFT JOIN subscriptions ON participants.id = subscriptions.participant_id
109- WHERE
110- subscriptions.list_id = NEW.id AND participants.authorized_sender = 1;
111- END;
112 diff --git a/migrations/20230602111216_init.sql b/migrations/20230602111216_init.sql
113new file mode 100644
114index 0000000..d3754fe
115--- /dev/null
116+++ b/migrations/20230602111216_init.sql
117 @@ -0,0 +1,51 @@
118+ CREATE TABLE authors (
119+ id INTEGER PRIMARY KEY NOT NULL,
120+ username TEXT NOT NULL,
121+ email TEXT NOT NULL,
122+ UNIQUE (username, email)
123+ ) STRICT;
124+
125+ CREATE TABLE languages (
126+ id INTEGER PRIMARY KEY NOT NULL,
127+ git_hash TEXT NOT NULL,
128+ repo_path TEXT NOT NULL,
129+ language TEXT NOT NULL,
130+ loc INTEGER NOT NULL,
131+ UNIQUE (git_hash, repo_path, language)
132+ ) STRICT;
133+
134+ CREATE TABLE jobs (
135+ id INTEGER PRIMARY KEY,
136+ created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
137+ kind TEXT NOT NULL,
138+ repo_path TEXT,
139+ runtime INTEGER,
140+ success INTEGER
141+ ) STRICT;
142+
143+ CREATE TABLE job_tracking (
144+ id INTEGER PRIMARY KEY,
145+ job_id INTEGER REFERENCES jobs(id) ON DELETE CASCADE NOT NULL,
146+ repo_path TEXT NOT NULL,
147+ git_hash TEXT NOT NULL,
148+ kind TEXT NOT NULL,
149+ UNIQUE (repo_path, git_hash, kind)
150+ ) STRICT;
151+
152+ CREATE TABLE contributions (
153+ id INTEGER PRIMARY KEY,
154+ git_hash TEXT NOT NULL,
155+ repo_path TEXT NOT NULL,
156+ time TEXT NOT NULL,
157+ author_id INTEGER REFERENCES authors(id),
158+ lines_added INTEGER NOT NULL,
159+ lines_removed INTEGER NOT NULL
160+ ) STRICT;
161+
162+ CREATE TABLE contribution_tally (
163+ id INTEGER PRIMARY KEY,
164+ git_hash TEXT NOT NULL,
165+ repo_path TEXT NOT NULL,
166+ author_id INTEGER REFERENCES authors(id),
167+ total INTEGER NOT NULL
168+ ) STRICT;
169 diff --git a/migrations/20241028092919_mail.sql b/migrations/20241028092919_mail.sql
170new file mode 100644
171index 0000000..e5f36cb
172--- /dev/null
173+++ b/migrations/20241028092919_mail.sql
174 @@ -0,0 +1,48 @@
175+ CREATE TABLE lists (
176+ id INTEGER PRIMARY KEY,
177+ name TEXT NOT NULL,
178+ address TEXT NOT NULL UNIQUE,
179+ description TEXT,
180+ enabled INTEGER NOT NULL DEFAULT 0 CHECK (enabled IN (0, 1))
181+ ) STRICT ;
182+
183+ CREATE TABLE participants (
184+ id INTEGER PRIMARY KEY,
185+ address TEXT NOT NULL UNIQUE,
186+ authorized_sender INTEGER NOT NULL DEFAULT 0 CHECK (authorized_sender IN (0, 1))
187+ ) STRICT ;
188+
189+ CREATE TABLE subscriptions (
190+ id INTEGER PRIMARY KEY,
191+ participant_id INTEGER NOT NULL REFERENCES participants(id),
192+ list_id INTEGER NOT NULL REFERENCES lists(id),
193+ UNIQUE(participant_id, list_id) ON CONFLICT REPLACE
194+ ) STRICT ;
195+
196+ CREATE TABLE outbox (
197+ id INTEGER PRIMARY KEY,
198+ message_id INTEGER NOT NULL REFERENCES messages(id),
199+ recipient INTEGER NOT NULL REFERENCES participants(id),
200+ delivered INTEGER NOT NULL DEFAULT 0 CHECK (delivered IN (0, 1)),
201+ UNIQUE(message_id, recipient) ON CONFLICT REPLACE
202+ ) STRICT ;
203+
204+ CREATE TABLE messages (
205+ id INTEGER PRIMARY KEY,
206+ message_id TEXT NOT NULL UNIQUE,
207+ list_id INTEGER NOT NULL REFERENCES lists(id),
208+ reply_to INTEGER REFERENCES messages(id),
209+ mail_from INTEGER NOT NULL REFERENCES participants(id),
210+ message_body BLOB
211+ ) STRICT ;
212+
213+ CREATE TRIGGER handle_delivery
214+ AFTER INSERT ON messages
215+ FOR EACH ROW
216+ BEGIN
217+ INSERT INTO outbox (message_id, recipient)
218+ SELECT NEW.id, participants.id FROM participants
219+ LEFT JOIN subscriptions ON participants.id = subscriptions.participant_id
220+ WHERE
221+ subscriptions.list_id = NEW.id AND participants.authorized_sender = 1;
222+ END;
223 diff --git a/scripts/ensure_database.sh b/scripts/ensure_database.sh
224index 4fae5b8..f954193 100755
225--- a/scripts/ensure_database.sh
226+++ b/scripts/ensure_database.sh
227 @@ -3,13 +3,7 @@ set -ex
228 # initialize a local database for builds and testing assuming the package
229 # has database support.
230
231- COMPONENT="$1"
232-
233- if [ -z "$COMPONENT" ] ; then
234- COMPONENT="ayllu"
235- fi
236-
237- DB_PATH="db/$COMPONENT.db"
238+ DB_PATH="db/ayllu.db"
239 DB_URL="sqlite://${DB_PATH}"
240
241 mkdir -p "$(dirname "${DB_PATH}")"
242 @@ -20,4 +14,4 @@ if [ ! -e "${DB_PATH}" ]; then
243 cargo sqlx database create --database-url "${DB_URL}"
244 fi
245
246- cargo sqlx migrate run --source "$COMPONENT/migrations" --database-url "${DB_URL}"
247+ cargo sqlx migrate run --source "migrations" --database-url "${DB_URL}"