Open History to see the last commit
1CREATE TABLE __SCHEMA__.webhook_signing_keys (
2 kid text PRIMARY KEY,
3 public_jwk jsonb NOT NULL CHECK (NOT (public_jwk ? 'd')),
4 private_key bytea NOT NULL,
5 active boolean NOT NULL,
6 created_at timestamptz NOT NULL DEFAULT clock_timestamp()
7);
8CREATE UNIQUE INDEX webhook_one_active_key ON __SCHEMA__.webhook_signing_keys (active) WHERE active;
9
10CREATE TABLE __SCHEMA__.subscriptions (
11 id uuid PRIMARY KEY,
12 tenant_id text COLLATE "C" NOT NULL,
13 name text COLLATE "C" NOT NULL,
14 config jsonb NOT NULL,
15 config_hash text NOT NULL,
16 pattern_regex text,
17 public_base_url text NOT NULL,
18 generation bigint NOT NULL DEFAULT 0 CHECK (generation BETWEEN 0 AND 9007199254740991),
19 current_wake uuid,
20 holder text,
21 lease_until timestamptz,
22 retry_count integer NOT NULL DEFAULT 0 CHECK (retry_count >= 0),
23 retry_at timestamptz,
24 next_work_at timestamptz,
25 member_count integer NOT NULL DEFAULT 0 CHECK (member_count >= 0),
26 blocked_members integer NOT NULL DEFAULT 0 CHECK (blocked_members >= 0),
27 created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
28 deleted_at timestamptz,
29 UNIQUE (tenant_id, id)
30);
31CREATE UNIQUE INDEX subscription_active_name ON __SCHEMA__.subscriptions (tenant_id, name) WHERE deleted_at IS NULL;
32CREATE INDEX subscription_work_due ON __SCHEMA__.subscriptions (next_work_at, id) WHERE deleted_at IS NULL AND next_work_at IS NOT NULL;
33CREATE INDEX subscriptions_tenant ON __SCHEMA__.subscriptions (tenant_id, id) WHERE deleted_at IS NULL;
34
35CREATE TABLE __SCHEMA__.subscription_members (
36 subscription_id uuid NOT NULL,
37 tenant_id text COLLATE "C" NOT NULL,
38 path text COLLATE "C" NOT NULL,
39 stream_id uuid,
40 explicit_link boolean NOT NULL DEFAULT false,
41 glob_link boolean NOT NULL DEFAULT false,
42 acked_offset text COLLATE "C" NOT NULL,
43 PRIMARY KEY (subscription_id, path),
44 FOREIGN KEY (tenant_id, subscription_id) REFERENCES __SCHEMA__.subscriptions (tenant_id, id),
45 FOREIGN KEY (tenant_id, stream_id) REFERENCES __SCHEMA__.streams (tenant_id, id),
46 CHECK (explicit_link OR glob_link),
47 CHECK (acked_offset ~ '^[0-9]{16}_[0-9]{16}$')
48);
49CREATE INDEX subscription_members_stream ON __SCHEMA__.subscription_members (stream_id, subscription_id);
50
51CREATE TABLE __SCHEMA__.subscription_wakes (
52 id uuid PRIMARY KEY,
53 subscription_id uuid NOT NULL,
54 tenant_id text COLLATE "C" NOT NULL,
55 generation bigint NOT NULL CHECK (generation BETWEEN 1 AND 9007199254740991),
56 kind text NOT NULL CHECK (kind IN ('webhook', 'pull-wake')),
57 token_hash bytea,
58 token_cipher bytea,
59 state text NOT NULL CHECK (state IN ('ready', 'delivered', 'completed', 'expired', 'fenced')),
60 delivery_claim uuid,
61 delivery_until timestamptz,
62 pull_published boolean NOT NULL DEFAULT false,
63 created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
64 UNIQUE (subscription_id, generation),
65 UNIQUE (subscription_id, id),
66 FOREIGN KEY (tenant_id, subscription_id) REFERENCES __SCHEMA__.subscriptions (tenant_id, id),
67 CHECK ((token_hash IS NULL) = (token_cipher IS NULL))
68);
69ALTER TABLE __SCHEMA__.subscriptions ADD CONSTRAINT current_wake_fk
70 FOREIGN KEY (id, current_wake) REFERENCES __SCHEMA__.subscription_wakes (subscription_id, id);
71
72CREATE TABLE __SCHEMA__.subscription_wake_streams (
73 wake_id uuid NOT NULL REFERENCES __SCHEMA__.subscription_wakes (id),
74 tenant_id text COLLATE "C" NOT NULL,
75 path text COLLATE "C" NOT NULL,
76 stream_id uuid,
77 link_type text NOT NULL CHECK (link_type IN ('explicit', 'glob')),
78 acked_offset text COLLATE "C" NOT NULL,
79 tail_offset text COLLATE "C" NOT NULL,
80 PRIMARY KEY (wake_id, path),
81 FOREIGN KEY (tenant_id, stream_id) REFERENCES __SCHEMA__.streams (tenant_id, id),
82 CHECK (acked_offset <= tail_offset)
83);
84
85CREATE TABLE __SCHEMA__.subscription_pending_links (
86 subscription_id uuid NOT NULL,
87 tenant_id text COLLATE "C" NOT NULL,
88 stream_id uuid NOT NULL,
89 before_offset text COLLATE "C" NOT NULL,
90 due_at timestamptz NOT NULL DEFAULT clock_timestamp(),
91 PRIMARY KEY (subscription_id, stream_id),
92 FOREIGN KEY (tenant_id, subscription_id) REFERENCES __SCHEMA__.subscriptions (tenant_id, id),
93 FOREIGN KEY (tenant_id, stream_id) REFERENCES __SCHEMA__.streams (tenant_id, id)
94);
95CREATE INDEX subscription_pending_links_due ON __SCHEMA__.subscription_pending_links (due_at, subscription_id, stream_id);
96ALTER TABLE __SCHEMA__.stream_changes ADD COLUMN fanout_after uuid;
97CREATE INDEX stream_changes_stream_pending ON __SCHEMA__.stream_changes (stream_id, version) WHERE NOT processed;