Skip to content
Source
Sign inSign up
samwillis/neon-durable-streams
Open History to see the last commit

0004_subscriptions.sql

115 lines5.3 KB

1-- Subscription membership, durable wakes, signing keys, and change fanout.
2CREATE TABLE __SCHEMA__.webhook_signing_keys (
3 kid text PRIMARY KEY,
4 public_jwk jsonb NOT NULL CHECK (NOT (public_jwk ? 'd')),
5 private_key bytea NOT NULL,
6 active boolean NOT NULL,
7 created_at timestamptz NOT NULL DEFAULT clock_timestamp()
8);
9CREATE UNIQUE INDEX webhook_one_active_key ON __SCHEMA__.webhook_signing_keys (active) WHERE active;
10​
11CREATE TABLE __SCHEMA__.subscriptions (
12 id uuid PRIMARY KEY,
13 tenant_id text COLLATE "C" NOT NULL,
14 name text COLLATE "C" NOT NULL,
15 config jsonb NOT NULL,
16 config_hash text NOT NULL,
17 pattern_regex text,
18 public_base_url text NOT NULL,
19 generation bigint NOT NULL DEFAULT 0 CHECK (generation BETWEEN 0 AND 9007199254740991),
20 current_wake uuid,
21 holder text,
22 lease_until timestamptz,
23 retry_count integer NOT NULL DEFAULT 0 CHECK (retry_count >= 0),
24 retry_at timestamptz,
25 next_work_at timestamptz,
26 member_count integer NOT NULL DEFAULT 0 CHECK (member_count >= 0),
27 blocked_members integer NOT NULL DEFAULT 0 CHECK (blocked_members >= 0),
28 created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
29 deleted_at timestamptz,
30 UNIQUE (tenant_id, id),
31 CONSTRAINT subscription_lease_owner CHECK ((holder IS NULL) = (lease_until IS NULL)),
32 CONSTRAINT subscription_lease_wake CHECK (current_wake IS NOT NULL OR holder IS NULL)
33);
34CREATE UNIQUE INDEX subscription_active_name ON __SCHEMA__.subscriptions (tenant_id, name) WHERE deleted_at IS NULL;
35CREATE INDEX subscription_work_due ON __SCHEMA__.subscriptions (next_work_at, id) WHERE deleted_at IS NULL AND next_work_at IS NOT NULL;
36CREATE INDEX subscriptions_tenant ON __SCHEMA__.subscriptions (tenant_id, id) WHERE deleted_at IS NULL;
37​
38CREATE TABLE __SCHEMA__.subscription_members (
39 subscription_id uuid NOT NULL,
40 tenant_id text COLLATE "C" NOT NULL,
41 path text COLLATE "C" NOT NULL,
42 stream_id uuid,
43 explicit_link boolean NOT NULL DEFAULT false,
44 glob_link boolean NOT NULL DEFAULT false,
45 acked_offset text COLLATE "C" NOT NULL,
46 PRIMARY KEY (subscription_id, path),
47 FOREIGN KEY (tenant_id, subscription_id) REFERENCES __SCHEMA__.subscriptions (tenant_id, id),
48 FOREIGN KEY (tenant_id, stream_id) REFERENCES __SCHEMA__.streams (tenant_id, id),
49 CHECK (explicit_link OR glob_link),
50 CHECK (__SCHEMA__.valid_offset(acked_offset))
51);
52CREATE INDEX subscription_members_stream ON __SCHEMA__.subscription_members (stream_id, subscription_id);
53​
54CREATE TABLE __SCHEMA__.subscription_wakes (
55 id uuid PRIMARY KEY,
56 subscription_id uuid NOT NULL,
57 tenant_id text COLLATE "C" NOT NULL,
58 generation bigint NOT NULL CHECK (generation BETWEEN 1 AND 9007199254740991),
59 kind text NOT NULL CHECK (kind IN ('webhook', 'pull-wake')),
60 token_version smallint NOT NULL DEFAULT 1 CHECK (token_version IN (1, 2)),
61 token_hash bytea,
62 token_cipher bytea,
63 state text NOT NULL CHECK (state IN ('ready', 'delivered', 'completed', 'expired', 'fenced')),
64 delivery_claim uuid,
65 delivery_until timestamptz,
66 created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
67 UNIQUE (subscription_id, generation),
68 UNIQUE (subscription_id, id),
69 FOREIGN KEY (tenant_id, subscription_id) REFERENCES __SCHEMA__.subscriptions (tenant_id, id),
70 CHECK ((token_hash IS NULL) = (token_cipher IS NULL)),
71 CONSTRAINT wake_delivery_claim CHECK ((delivery_claim IS NULL) = (delivery_until IS NULL))
72);
73ALTER TABLE __SCHEMA__.subscriptions ADD CONSTRAINT current_wake_fk
74 FOREIGN KEY (id, current_wake) REFERENCES __SCHEMA__.subscription_wakes (subscription_id, id);
75​
76CREATE TABLE __SCHEMA__.subscription_wake_streams (
77 wake_id uuid NOT NULL REFERENCES __SCHEMA__.subscription_wakes (id),
78 tenant_id text COLLATE "C" NOT NULL,
79 path text COLLATE "C" NOT NULL,
80 stream_id uuid,
81 link_type text NOT NULL CHECK (link_type IN ('explicit', 'glob')),
82 acked_offset text COLLATE "C" NOT NULL,
83 tail_offset text COLLATE "C" NOT NULL,
84 PRIMARY KEY (wake_id, path),
85 FOREIGN KEY (tenant_id, stream_id) REFERENCES __SCHEMA__.streams (tenant_id, id),
86 CHECK (acked_offset <= tail_offset)
87);
88​
89CREATE TABLE __SCHEMA__.subscription_pending_links (
90 subscription_id uuid NOT NULL,
91 tenant_id text COLLATE "C" NOT NULL,
92 stream_id uuid NOT NULL,
93 before_offset text COLLATE "C" NOT NULL,
94 due_at timestamptz NOT NULL DEFAULT clock_timestamp(),
95 PRIMARY KEY (subscription_id, stream_id),
96 FOREIGN KEY (tenant_id, subscription_id) REFERENCES __SCHEMA__.subscriptions (tenant_id, id),
97 FOREIGN KEY (tenant_id, stream_id) REFERENCES __SCHEMA__.streams (tenant_id, id)
98);
99CREATE INDEX subscription_pending_links_due ON __SCHEMA__.subscription_pending_links (due_at, subscription_id, stream_id);
100​
101-- One bounded scalar preserves increasing generations across name pruning.
102CREATE SEQUENCE __SCHEMA__.wake_generations AS bigint MINVALUE 1 MAXVALUE 9007199254740991 NO CYCLE;
103​
104CREATE TABLE __SCHEMA__.stream_changes (
105 stream_id uuid NOT NULL REFERENCES __SCHEMA__.streams (id),
106 version bigint NOT NULL,
107 before_offset text COLLATE "C" NOT NULL,
108 after_offset text COLLATE "C" NOT NULL,
109 kind text NOT NULL CHECK (kind IN ('created', 'appended', 'closed', 'deleted', 'expired')),
110 created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
111 fanout_after uuid,
112 PRIMARY KEY (stream_id, version)
113);
114CREATE INDEX stream_changes_pending ON __SCHEMA__.stream_changes (created_at);
115CREATE INDEX stream_changes_stream_pending ON __SCHEMA__.stream_changes (stream_id, version);