OXYGENHUB
◆ diffusion-pin-bookmark oxygenhub / crates / hub / migrations / 0003_accounts.sql
4.2 KiB · SQL · last changed Milestone 0.2: accounts and access 1 month ago source blame history raw
1
-- Accounts, organisations, roles, tokens, linked identities and the audit
2
-- log (hub spec §5). Repository content stays in the object stores.
3
4
CREATE TABLE accounts (
5
    id             INTEGER PRIMARY KEY,
6
    username       TEXT NOT NULL UNIQUE COLLATE NOCASE,
7
    display_name   TEXT NOT NULL DEFAULT '',
8
    email          TEXT NOT NULL DEFAULT '',
9
    email_verified INTEGER NOT NULL DEFAULT 0,
10
    password_hash  TEXT NOT NULL,
11
    session_salt   TEXT NOT NULL,
12
    totp_secret    TEXT,
13
    recovery_codes TEXT NOT NULL DEFAULT '',
14
    is_admin       INTEGER NOT NULL DEFAULT 0,
15
    banned         INTEGER NOT NULL DEFAULT 0,
16
    created_ms     INTEGER NOT NULL,
17
    last_seen_ms   INTEGER NOT NULL DEFAULT 0
18
);
19
20
CREATE TABLE organisations (
21
    id           INTEGER PRIMARY KEY,
22
    name         TEXT NOT NULL UNIQUE COLLATE NOCASE,
23
    display_name TEXT NOT NULL DEFAULT '',
24
    description  TEXT NOT NULL DEFAULT '',
25
    default_role TEXT NOT NULL DEFAULT 'read' CHECK (default_role IN ('none', 'read', 'triage', 'write', 'maintain', 'admin')),
26
    created_ms   INTEGER NOT NULL
27
);
28
29
CREATE TABLE org_members (
30
    org_id     INTEGER NOT NULL REFERENCES organisations(id) ON DELETE CASCADE,
31
    account_id INTEGER NOT NULL REFERENCES accounts(id) ON DELETE CASCADE,
32
    role       TEXT NOT NULL DEFAULT 'member' CHECK (role IN ('owner', 'member')),
33
    PRIMARY KEY (org_id, account_id)
34
);
35
36
CREATE TABLE teams (
37
    id     INTEGER PRIMARY KEY,
38
    org_id INTEGER NOT NULL REFERENCES organisations(id) ON DELETE CASCADE,
39
    name   TEXT NOT NULL COLLATE NOCASE,
40
    UNIQUE (org_id, name)
41
);
42
43
CREATE TABLE team_members (
44
    team_id    INTEGER NOT NULL REFERENCES teams(id) ON DELETE CASCADE,
45
    account_id INTEGER NOT NULL REFERENCES accounts(id) ON DELETE CASCADE,
46
    PRIMARY KEY (team_id, account_id)
47
);
48
49
-- A collaborator is an account or a team with a role on one repository.
50
CREATE TABLE collaborators (
51
    repo_id    INTEGER NOT NULL REFERENCES repositories(id) ON DELETE CASCADE,
52
    account_id INTEGER REFERENCES accounts(id) ON DELETE CASCADE,
53
    team_id    INTEGER REFERENCES teams(id) ON DELETE CASCADE,
54
    role       TEXT NOT NULL CHECK (role IN ('read', 'triage', 'write', 'maintain', 'admin')),
55
    CHECK ((account_id IS NULL) <> (team_id IS NULL))
56
);
57
CREATE UNIQUE INDEX collaborators_account ON collaborators (repo_id, account_id) WHERE account_id IS NOT NULL;
58
CREATE UNIQUE INDEX collaborators_team ON collaborators (repo_id, team_id) WHERE team_id IS NOT NULL;
59
60
CREATE TABLE tokens (
61
    id           INTEGER PRIMARY KEY,
62
    account_id   INTEGER NOT NULL REFERENCES accounts(id) ON DELETE CASCADE,
63
    name         TEXT NOT NULL,
64
    hash         TEXT NOT NULL UNIQUE,
65
    scopes       TEXT NOT NULL,
66
    repos        TEXT NOT NULL DEFAULT '',
67
    created_ms   INTEGER NOT NULL,
68
    expires_ms   INTEGER,
69
    last_used_ms INTEGER
70
);
71
72
-- Oxygen identities an account has proven it holds.
73
CREATE TABLE identities (
74
    key        TEXT PRIMARY KEY,
75
    account_id INTEGER NOT NULL REFERENCES accounts(id) ON DELETE CASCADE,
76
    name       TEXT NOT NULL,
77
    email      TEXT NOT NULL,
78
    object     BLOB NOT NULL,
79
    linked_ms  INTEGER NOT NULL
80
);
81
82
CREATE TABLE challenges (
83
    id         INTEGER PRIMARY KEY,
84
    account_id INTEGER NOT NULL REFERENCES accounts(id) ON DELETE CASCADE,
85
    bytes      TEXT NOT NULL,
86
    object     BLOB NOT NULL DEFAULT X'',
87
    created_ms INTEGER NOT NULL
88
);
89
90
CREATE TABLE invitations (
91
    code       TEXT PRIMARY KEY,
92
    created_by INTEGER REFERENCES accounts(id) ON DELETE SET NULL,
93
    used_by    INTEGER REFERENCES accounts(id) ON DELETE SET NULL,
94
    created_ms INTEGER NOT NULL
95
);
96
97
CREATE TABLE audit (
98
    id         INTEGER PRIMARY KEY,
99
    at_ms      INTEGER NOT NULL,
100
    actor_id   INTEGER REFERENCES accounts(id) ON DELETE SET NULL,
101
    actor      TEXT NOT NULL DEFAULT '',
102
    action     TEXT NOT NULL,
103
    target     TEXT NOT NULL DEFAULT '',
104
    detail     TEXT NOT NULL DEFAULT ''
105
);
106
CREATE INDEX audit_by_time ON audit (at_ms DESC);
107
108
-- Site settings that an administrator changes at run time.
109
CREATE TABLE settings (
110
    key   TEXT PRIMARY KEY,
111
    value TEXT NOT NULL
112
);
113
114
ALTER TABLE pushes ADD COLUMN account_id INTEGER REFERENCES accounts(id) ON DELETE SET NULL;
115
ALTER TABLE pushes ADD COLUMN pusher TEXT NOT NULL DEFAULT '';