OXYGENHUB
# c4027e038d25 oxygenhub / crates / hub / migrations / 0006_hooks_checks.sql
1.9 KiB · SQL · last changed Milestone 0.5: API, webhooks, checks 1 month ago source blame history raw
1
-- Webhooks with a delivery log, and checks on state hashes (hub spec
2
-- §12.2, §13.1).
3
4
CREATE TABLE hooks (
5
    id         INTEGER PRIMARY KEY,
6
    repo_id    INTEGER REFERENCES repositories(id) ON DELETE CASCADE,
7
    org_id     INTEGER REFERENCES organisations(id) ON DELETE CASCADE,
8
    url        TEXT NOT NULL,
9
    secret     TEXT NOT NULL DEFAULT '',
10
    events     TEXT NOT NULL DEFAULT '*',
11
    active     INTEGER NOT NULL DEFAULT 1,
12
    created_ms INTEGER NOT NULL,
13
    CHECK ((repo_id IS NULL) <> (org_id IS NULL))
14
);
15
16
CREATE TABLE deliveries (
17
    id          INTEGER PRIMARY KEY,
18
    hook_id     INTEGER NOT NULL REFERENCES hooks(id) ON DELETE CASCADE,
19
    delivery    TEXT NOT NULL,
20
    event       TEXT NOT NULL,
21
    payload     TEXT NOT NULL,
22
    created_ms  INTEGER NOT NULL,
23
    attempts    INTEGER NOT NULL DEFAULT 0,
24
    next_ms     INTEGER NOT NULL,
25
    status      INTEGER,
26
    response    TEXT NOT NULL DEFAULT '',
27
    delivered_ms INTEGER
28
);
29
CREATE INDEX deliveries_due ON deliveries (next_ms) WHERE delivered_ms IS NULL;
30
CREATE INDEX deliveries_by_hook ON deliveries (hook_id, created_ms DESC);
31
32
CREATE TABLE checks (
33
    id          INTEGER PRIMARY KEY,
34
    repo_id     INTEGER NOT NULL REFERENCES repositories(id) ON DELETE CASCADE,
35
    state       TEXT NOT NULL,
36
    name        TEXT NOT NULL,
37
    status      TEXT NOT NULL CHECK (status IN ('queued', 'running', 'passed', 'failed', 'cancelled', 'skipped')),
38
    summary     TEXT NOT NULL DEFAULT '',
39
    url         TEXT NOT NULL DEFAULT '',
40
    reporter_id INTEGER REFERENCES accounts(id) ON DELETE SET NULL,
41
    reporter    TEXT NOT NULL DEFAULT '',
42
    seal        TEXT NOT NULL DEFAULT '',
43
    started_ms  INTEGER NOT NULL,
44
    finished_ms INTEGER,
45
    UNIQUE (repo_id, state, name)
46
);
47
CREATE INDEX checks_by_state ON checks (repo_id, state);
48
49
ALTER TABLE repositories ADD COLUMN required_checks TEXT NOT NULL DEFAULT '';
50
ALTER TABLE tokens ADD COLUMN scopes_note TEXT NOT NULL DEFAULT '';