Skip to content
Source
Sign inSign up
samwillis/jerry
Open History to see the last commit

001_jol.sql

70 lines2.6 KB

1CREATE TABLE IF NOT EXISTS jol_devices (
2 id uuid PRIMARY KEY,
3 owner_id text NOT NULL,
4 name text NOT NULL CHECK (length(name) BETWEEN 1 AND 80),
5 credential_hash text NOT NULL,
6 revoked boolean NOT NULL DEFAULT false,
7 remote_enabled boolean NOT NULL DEFAULT false,
8 last_seen timestamptz,
9 created_at timestamptz NOT NULL DEFAULT now(),
10 UNIQUE(id, owner_id)
11);
12CREATE TABLE IF NOT EXISTS jol_expeditions (
13 id uuid NOT NULL,
14 device_id uuid NOT NULL REFERENCES jol_devices(id),
15 title text NOT NULL DEFAULT 'An untitled adventure',
16 status text NOT NULL DEFAULT 'Catching up',
17 is_running boolean NOT NULL DEFAULT false,
18 is_finished boolean NOT NULL DEFAULT false,
19 created_at timestamptz NOT NULL,
20 cursor integer NOT NULL DEFAULT 0,
21 PRIMARY KEY(device_id,id)
22);
23CREATE TABLE IF NOT EXISTS jol_events (
24 device_id uuid NOT NULL,
25 expedition_id uuid NOT NULL,
26 sequence integer NOT NULL CHECK(sequence > 0),
27 event_id uuid NOT NULL,
28 digest text NOT NULL,
29 payload jsonb NOT NULL,
30 PRIMARY KEY(device_id,expedition_id,sequence),
31 UNIQUE(device_id,expedition_id,event_id),
32 FOREIGN KEY(device_id,expedition_id) REFERENCES jol_expeditions(device_id,id)
33);
34CREATE TABLE IF NOT EXISTS jol_entries (
35 device_id uuid NOT NULL,
36 expedition_id uuid NOT NULL,
37 id uuid NOT NULL,
38 position integer NOT NULL,
39 kind text NOT NULL CHECK(kind IN ('request','response','activity')),
40 text text NOT NULL,
41 details text NOT NULL DEFAULT '',
42 photo text,
43 created_at timestamptz NOT NULL,
44 PRIMARY KEY(device_id,expedition_id,id),
45 FOREIGN KEY(device_id,expedition_id) REFERENCES jol_expeditions(device_id,id)
46);
47CREATE TABLE IF NOT EXISTS jol_commands (
48 id uuid PRIMARY KEY,
49 owner_id text NOT NULL,
50 device_id uuid NOT NULL,
51 expedition_id uuid,
52 kind text NOT NULL CHECK(kind IN ('start','continue','stop','finish')),
53 prompt text NOT NULL DEFAULT '',
54 status text NOT NULL DEFAULT 'queued' CHECK(status IN ('queued','received','started','finished','failed','cancelled','expired','uncertain')),
55 result text NOT NULL DEFAULT '',
56 fence integer NOT NULL DEFAULT 0,
57 lease_until timestamptz,
58 created_at timestamptz NOT NULL DEFAULT now(),
59 expires_at timestamptz NOT NULL,
60 FOREIGN KEY(device_id,owner_id) REFERENCES jol_devices(id,owner_id)
61);
62CREATE INDEX IF NOT EXISTS jol_commands_queue ON jol_commands(device_id,status,created_at);
63CREATE TABLE IF NOT EXISTS jol_assets (
64 device_id uuid NOT NULL,
65 expedition_id uuid NOT NULL,
66 filename text NOT NULL,
67 object_key text NOT NULL UNIQUE,
68 PRIMARY KEY(device_id,expedition_id,filename),
69 FOREIGN KEY(device_id,expedition_id) REFERENCES jol_expeditions(device_id,id)
70);