@marktiderman/genesis-supabase 0.4.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,412 @@
1
+ -- Genesis feature registry in the database: the mirror, entitlements, flag values and the gate.
2
+ --
3
+ -- Feature and flag definitions live in code (`features.ts`, `flags.json`). This file gives the
4
+ -- app's database the rows the gate needs (ADR 0001, "One feature gate"):
5
+ --
6
+ -- public.features, public.feature_flags
7
+ -- A mirror of the registry, written only by public.sync_feature_registry with the service
8
+ -- key, by production deploys only (a preview must never sync production). Each sync carries a
9
+ -- version: the time the deployment started serving, not its build time, so an instant
10
+ -- rollback to an older build syncs as the newest. An older version than the last one synced is
11
+ -- refused (two instances racing), and the same hash again changes nothing. An empty registry,
12
+ -- or one that drops more than a fifth of the features (at least 3), is refused unless the call
13
+ -- says allow_shrink: a broken features.json must not switch everything off. Nothing is ever
14
+ -- deleted: a feature or flag gone from the registry is marked in_registry = false, and a
15
+ -- flag's per-tenant values stay until it comes back or you delete them.
16
+ --
17
+ -- public.feature_entitlements
18
+ -- Which tenant may use which paid feature (`access: { plan }`), by source (a billing webhook,
19
+ -- a manual grant), optionally until a time. Written by the service role only.
20
+ --
21
+ -- public.tenant_flag_values, feature_flags.platform_value
22
+ -- A flag's value for one tenant, overriding its default; and a value for every tenant at once
23
+ -- (a kill switch), overriding tenant values. Written by the service role only.
24
+ --
25
+ -- public.feature_on(tenant, feature)
26
+ -- The gate in SQL, for row security and functions: shipped (beta, live or deprecated) and,
27
+ -- when paid, entitled. Nothing else: platform, tenant switches and flags are exposure, which
28
+ -- server code enforces with the TypeScript gate (`requireFeature`).
29
+ --
30
+ -- public.feature_gate_state(tenant)
31
+ -- A tenant's runtime state in one call (switches off, entitlements held, flag values), for the
32
+ -- server to build the gate's snapshot. For the tenant's members, and the service role.
33
+ --
34
+ -- Tenants and membership: as in 0004_genesis_tenant_features, a tenant is any uuid your app
35
+ -- groups people by, and private.genesis_tenant_member / private.genesis_tenant_admin (your
36
+ -- hooks) decide who belongs. This file needs 0004 (it reads tenant_features and its hooks).
37
+ -- Add your tenant foreign keys yourself in a later migration:
38
+ --
39
+ -- alter table public.feature_entitlements add foreign key (tenant_id) references public.teams (id) on delete cascade;
40
+ -- alter table public.tenant_flag_values add foreign key (tenant_id) references public.teams (id) on delete cascade;
41
+ --
42
+ -- Use: copy this file into your app's `supabase/migrations/` under a new timestamped name
43
+ -- (`genesis migrate` does it). Never edit it once applied; change it with a new migration.
44
+ --
45
+ -- Verify: the pgTAP test beside this file (`tests/0008_genesis_feature_registry.test.sql`).
46
+
47
+ create schema if not exists private;
48
+
49
+ -- The mirror -----------------------------------------------------------------------------------
50
+
51
+ create table public.features (
52
+ id text primary key
53
+ constraint features_id_format check (id ~ '^[a-z][a-z0-9]*([-_.][a-z0-9]+)*$' and char_length(id) <= 100),
54
+ name text not null,
55
+ status text not null
56
+ constraint features_status check (status in ('idea', 'planned', 'building', 'beta', 'live', 'deprecated', 'retired')),
57
+ visibility text not null default 'customer',
58
+ -- The plan a paid feature needs (`access: { plan }`); null when everyone gets it.
59
+ access_plan text,
60
+ -- The status per platform (`platforms`), when the registry gives one.
61
+ platforms jsonb,
62
+ -- The whole registry entry, as code defines it.
63
+ record jsonb not null,
64
+ in_registry boolean not null default true,
65
+ synced_at timestamptz not null default now()
66
+ );
67
+
68
+ comment on table public.features is
69
+ 'Mirror of features.ts, written only by sync_feature_registry. Ids are permanent: rows are never deleted. Genesis feature registry.';
70
+
71
+ create table public.feature_flags (
72
+ key text primary key,
73
+ feature_id text not null references public.features (id),
74
+ kind text not null,
75
+ default_value jsonb not null,
76
+ scopes text[] not null default '{}',
77
+ expires date,
78
+ record jsonb not null,
79
+ in_registry boolean not null default true,
80
+ -- A value for every tenant, set by the service role: a kill switch or a global rollout. It
81
+ -- overrides tenant values, and sync never touches it.
82
+ platform_value jsonb,
83
+ synced_at timestamptz not null default now()
84
+ );
85
+
86
+ create index feature_flags_feature on public.feature_flags (feature_id);
87
+
88
+ comment on table public.feature_flags is
89
+ 'Mirror of flags.json, written only by sync_feature_registry. A flag gone from the registry is kept, marked in_registry = false. Genesis feature registry.';
90
+
91
+ create table private.feature_registry_state (
92
+ id boolean primary key default true constraint feature_registry_state_one_row check (id),
93
+ version bigint not null,
94
+ hash text not null,
95
+ synced_at timestamptz not null default now()
96
+ );
97
+
98
+ create or replace function public.sync_feature_registry(version bigint, hash text, features jsonb, flags jsonb, allow_shrink boolean default false)
99
+ returns boolean
100
+ language plpgsql
101
+ security definer
102
+ set search_path = ''
103
+ as $$
104
+ declare
105
+ current_count int;
106
+ gone_count int;
107
+ begin
108
+ if version is null or hash is null
109
+ or jsonb_typeof(features) is distinct from 'array' or jsonb_typeof(flags) is distinct from 'array' then
110
+ raise exception 'sync_feature_registry: needs a version, a hash and two arrays' using errcode = '22023';
111
+ end if;
112
+ if exists (select 1 from jsonb_array_elements(features) r where coalesce(r ->> 'id', '') = '')
113
+ or jsonb_array_length(features) <> (select count(distinct r ->> 'id') from jsonb_array_elements(features) r) then
114
+ raise exception 'sync_feature_registry: every feature needs a unique id' using errcode = '22023';
115
+ end if;
116
+ if exists (select 1 from jsonb_array_elements(flags) r where coalesce(r ->> 'key', '') = '')
117
+ or jsonb_array_length(flags) <> (select count(distinct r ->> 'key') from jsonb_array_elements(flags) r) then
118
+ raise exception 'sync_feature_registry: every flag needs a unique key' using errcode = '22023';
119
+ end if;
120
+ -- One sync at a time: a second deploy waits, then finds itself already current.
121
+ perform pg_advisory_xact_lock(hashtext('public.sync_feature_registry'));
122
+ -- Never backwards, and nothing to do for the same registry.
123
+ if exists (select 1 from private.feature_registry_state s where s.version > sync_feature_registry.version or s.hash = sync_feature_registry.hash) then
124
+ return false;
125
+ end if;
126
+ -- A broken or foreign registry must not switch everything off.
127
+ select count(*) into current_count from public.features f where f.in_registry;
128
+ select count(*) into gone_count from public.features f
129
+ where f.in_registry and not exists (select 1 from jsonb_array_elements(features) r where r ->> 'id' = f.id);
130
+ if not coalesce(allow_shrink, false) and (jsonb_array_length(features) = 0 or (gone_count >= 3 and gone_count * 5 > current_count)) then
131
+ raise exception 'sync_feature_registry: this registry drops % of % features', gone_count, current_count
132
+ using errcode = '22023', hint = 'Check features.json; if the removal is intended, call again with allow_shrink => true.';
133
+ end if;
134
+
135
+ insert into public.features as f (id, name, status, visibility, access_plan, platforms, record, in_registry, synced_at)
136
+ select r ->> 'id', r ->> 'name', r ->> 'status', coalesce(r ->> 'visibility', 'customer'),
137
+ case when jsonb_typeof(r -> 'access') = 'object' then nullif(btrim(r -> 'access' ->> 'plan'), '') end,
138
+ case when jsonb_typeof(r -> 'platforms') = 'object' then r -> 'platforms' end,
139
+ r, true, now()
140
+ from jsonb_array_elements(features) r
141
+ on conflict (id) do update set
142
+ name = excluded.name, status = excluded.status, visibility = excluded.visibility, access_plan = excluded.access_plan,
143
+ platforms = excluded.platforms, record = excluded.record, in_registry = true, synced_at = excluded.synced_at;
144
+
145
+ -- Ids are permanent: a feature the registry no longer lists stays, marked as gone.
146
+ update public.features f set in_registry = false, synced_at = now()
147
+ where f.in_registry and not exists (select 1 from jsonb_array_elements(features) r where r ->> 'id' = f.id);
148
+
149
+ update public.feature_flags fl set in_registry = false, synced_at = now()
150
+ where fl.in_registry and not exists (select 1 from jsonb_array_elements(flags) r where r ->> 'key' = fl.key);
151
+ insert into public.feature_flags as fl (key, feature_id, kind, default_value, scopes, expires, record, in_registry, synced_at)
152
+ select r ->> 'key', r ->> 'feature', r ->> 'kind', coalesce(r -> 'default', 'false'::jsonb),
153
+ coalesce(array(select jsonb_array_elements_text(r -> 'scopes')), '{}'),
154
+ (r ->> 'expires')::date, r, true, now()
155
+ from jsonb_array_elements(flags) r
156
+ on conflict (key) do update set
157
+ feature_id = excluded.feature_id, kind = excluded.kind, default_value = excluded.default_value,
158
+ scopes = excluded.scopes, expires = excluded.expires, record = excluded.record, in_registry = true,
159
+ synced_at = excluded.synced_at;
160
+
161
+ insert into private.feature_registry_state (id, version, hash, synced_at)
162
+ values (true, sync_feature_registry.version, sync_feature_registry.hash, now())
163
+ on conflict (id) do update set version = excluded.version, hash = excluded.hash, synced_at = excluded.synced_at;
164
+ return true;
165
+ end;
166
+ $$;
167
+
168
+ comment on function public.sync_feature_registry(bigint, text, jsonb, jsonb, boolean) is
169
+ 'Mirrors features.ts and flags.json into features and feature_flags. Service role, production deploys only. Refuses an older version, an empty registry or a sharp shrink (unless allow_shrink); the same hash is a no-op; nothing is deleted. Genesis feature registry.';
170
+
171
+ create or replace function public.feature_registry_synced()
172
+ returns table (version bigint, hash text, synced_at timestamptz)
173
+ language sql
174
+ stable
175
+ security definer
176
+ set search_path = ''
177
+ as $$
178
+ select s.version, s.hash, s.synced_at from private.feature_registry_state s
179
+ $$;
180
+
181
+ comment on function public.feature_registry_synced() is
182
+ 'The version, hash and time of the last registry sync. Service role only. Genesis feature registry.';
183
+
184
+ -- Paid access and flag values ------------------------------------------------------------------
185
+
186
+ create table public.feature_entitlements (
187
+ tenant_id uuid not null,
188
+ -- No foreign key: a billing webhook may grant a feature before the deploy that adds it syncs.
189
+ feature_id text not null
190
+ constraint feature_entitlements_feature_id_format check (feature_id ~ '^[a-z][a-z0-9]*([-_.][a-z0-9]+)*$' and char_length(feature_id) <= 100),
191
+ -- Where the grant comes from: 'stripe', 'manual', 'trial' … One row per source.
192
+ source text not null
193
+ constraint feature_entitlements_source_format check (source ~ '^[a-z][a-z0-9_-]*$' and char_length(source) <= 40),
194
+ active boolean not null,
195
+ -- Optional end of the grant; past it the entitlement no longer counts.
196
+ ends_at timestamptz,
197
+ updated_at timestamptz not null default now(),
198
+ primary key (tenant_id, feature_id, source)
199
+ );
200
+
201
+ comment on table public.feature_entitlements is
202
+ 'Which tenant may use which paid feature, by source, optionally until ends_at. The feature id doubles as the billing lookup key. Service role only. Genesis feature registry.';
203
+
204
+ create table public.tenant_flag_values (
205
+ tenant_id uuid not null,
206
+ flag_key text not null references public.feature_flags (key),
207
+ value jsonb not null,
208
+ updated_at timestamptz not null default now(),
209
+ primary key (tenant_id, flag_key)
210
+ );
211
+
212
+ comment on table public.tenant_flag_values is
213
+ 'A flag''s value for one tenant, overriding its default. Kept while its flag is out of the registry. Service role only. Genesis feature registry.';
214
+
215
+ -- The gate -------------------------------------------------------------------------------------
216
+
217
+ -- Who is asking: server code (the service role, or a direct database connection with no request
218
+ -- claims) answers for any tenant; everyone else must be a member. Decided on the request's role,
219
+ -- never on a missing person: an authenticated request without `sub` is refused, not trusted.
220
+ create or replace function private.genesis_feature_caller_is_server()
221
+ returns boolean
222
+ language plpgsql
223
+ stable
224
+ security definer
225
+ set search_path = ''
226
+ as $$
227
+ declare
228
+ claims jsonb := coalesce(nullif(current_setting('request.jwt.claims', true), '')::jsonb, '{}'::jsonb);
229
+ request_role text := coalesce(nullif(claims ->> 'role', ''), nullif(current_setting('request.jwt.claim.role', true), ''));
230
+ begin
231
+ return request_role is null or request_role = 'service_role';
232
+ end;
233
+ $$;
234
+
235
+ create or replace function private.genesis_feature_may_read(tenant uuid)
236
+ returns boolean
237
+ language plpgsql
238
+ stable
239
+ security definer
240
+ set search_path = ''
241
+ as $$
242
+ begin
243
+ return private.genesis_feature_caller_is_server() or private.genesis_is_tenant_member(tenant);
244
+ end;
245
+ $$;
246
+
247
+ -- Shipped and, when paid, entitled: the part of the gate that row security enforces. A signed-in
248
+ -- caller gets an answer only for a tenant they belong to (false otherwise), so a policy can't be
249
+ -- used to probe another tenant's plan. Server code (the service role, no signed-in person) gets
250
+ -- an answer for any tenant.
251
+ create or replace function public.feature_on(tenant uuid, feature text)
252
+ returns boolean
253
+ language plpgsql
254
+ stable
255
+ security definer
256
+ set search_path = ''
257
+ as $$
258
+ begin
259
+ if tenant is null or feature is null then
260
+ return false;
261
+ end if;
262
+ if not private.genesis_feature_may_read(tenant) then
263
+ return false;
264
+ end if;
265
+ return exists (
266
+ select 1 from public.features f
267
+ where f.id = feature and f.in_registry and f.status in ('beta', 'live', 'deprecated')
268
+ and (f.access_plan is null or exists (
269
+ select 1 from public.feature_entitlements e
270
+ where e.tenant_id = tenant and e.feature_id = f.id and e.active and (e.ends_at is null or e.ends_at > now())
271
+ ))
272
+ );
273
+ end;
274
+ $$;
275
+
276
+ comment on function public.feature_on(uuid, text) is
277
+ 'The gate in SQL: shipped (beta, live, deprecated) and entitled when paid. For row security. A signed-in caller outside the tenant gets false. Genesis feature registry.';
278
+
279
+ -- For row security over many rows, two answers Postgres computes once per statement instead of
280
+ -- once per row (wrap them in a subquery):
281
+ -- free feature: using ((select public.feature_shipped('notes')) and <your row rule>)
282
+ -- paid feature: using ((select public.feature_shipped('exports'))
283
+ -- and tenant_id = any (array(select public.feature_entitled_tenants('exports'))) and <your row rule>)
284
+ create or replace function public.feature_shipped(feature text)
285
+ returns boolean
286
+ language sql
287
+ stable
288
+ security definer
289
+ set search_path = ''
290
+ as $$
291
+ select exists (
292
+ select 1 from public.features f
293
+ where f.id = feature and f.in_registry and f.status in ('beta', 'live', 'deprecated')
294
+ )
295
+ $$;
296
+
297
+ comment on function public.feature_shipped(text) is
298
+ 'Whether a feature has shipped (beta, live, deprecated), whatever the tenant. For row security, once per statement. Genesis feature registry.';
299
+
300
+ -- The tenants with an active entitlement to a feature: every one for server code, only the
301
+ -- caller's own for a signed-in person.
302
+ create or replace function public.feature_entitled_tenants(feature text)
303
+ returns setof uuid
304
+ language plpgsql
305
+ stable
306
+ security definer
307
+ set search_path = ''
308
+ as $$
309
+ begin
310
+ return query
311
+ select distinct e.tenant_id from public.feature_entitlements e
312
+ where e.feature_id = feature and e.active and (e.ends_at is null or e.ends_at > now())
313
+ and private.genesis_feature_may_read(e.tenant_id);
314
+ end;
315
+ $$;
316
+
317
+ comment on function public.feature_entitled_tenants(text) is
318
+ 'Tenants holding an active entitlement to a feature (the caller''s own when signed in). For row security, once per statement. Genesis feature registry.';
319
+
320
+ -- A tenant's runtime state for the gate's snapshot: { off: [ids], entitled: [ids], flags: { key: value } }.
321
+ create or replace function public.feature_gate_state(tenant uuid)
322
+ returns jsonb
323
+ language plpgsql
324
+ stable
325
+ security definer
326
+ set search_path = ''
327
+ as $$
328
+ begin
329
+ if tenant is null then
330
+ raise exception 'feature_gate_state: tenant is required' using errcode = '22004';
331
+ end if;
332
+ if not private.genesis_feature_may_read(tenant) then
333
+ raise exception 'feature_gate_state: only the tenant''s members can read its features' using errcode = '42501';
334
+ end if;
335
+ return jsonb_build_object(
336
+ 'off', coalesce((
337
+ select jsonb_agg(t.feature_id order by t.feature_id) from public.tenant_features t
338
+ where t.tenant_id = tenant and not t.enabled
339
+ ), '[]'::jsonb),
340
+ 'entitled', coalesce((
341
+ select jsonb_agg(distinct e.feature_id) from public.feature_entitlements e
342
+ where e.tenant_id = tenant and e.active and (e.ends_at is null or e.ends_at > now())
343
+ ), '[]'::jsonb),
344
+ -- The tenant's own values, then platform values on top: a platform value (a kill switch)
345
+ -- applies to every tenant, whatever the tenant's own value.
346
+ 'flags', coalesce((
347
+ select jsonb_object_agg(v.flag_key, v.value) from public.tenant_flag_values v
348
+ join public.feature_flags fl on fl.key = v.flag_key and fl.in_registry
349
+ where v.tenant_id = tenant
350
+ ), '{}'::jsonb) || coalesce((
351
+ select jsonb_object_agg(fl.key, fl.platform_value) from public.feature_flags fl
352
+ where fl.in_registry and fl.platform_value is not null
353
+ ), '{}'::jsonb)
354
+ );
355
+ end;
356
+ $$;
357
+
358
+ comment on function public.feature_gate_state(uuid) is
359
+ 'A tenant''s switches off, entitlements held and flag values, for the gate''s snapshot. Its members and the service role. Genesis feature registry.';
360
+
361
+ -- Access ---------------------------------------------------------------------------------------
362
+
363
+ alter table public.features enable row level security;
364
+ alter table public.feature_flags enable row level security;
365
+ alter table public.feature_entitlements enable row level security;
366
+ alter table public.tenant_flag_values enable row level security;
367
+ alter table private.feature_registry_state enable row level security;
368
+
369
+ -- Signed-in people (not anonymous sign-ins) read shipped, customer-facing features, and only the
370
+ -- columns a screen needs: the full record (owners, plans, acceptance, links) stays server side.
371
+ -- Flags, entitlements and flag values are read through feature_gate_state.
372
+ revoke all on table public.features from public, anon, authenticated, service_role;
373
+ grant select on table public.features to service_role;
374
+ grant select (id, name, status, visibility, access_plan, platforms, in_registry) on table public.features to authenticated;
375
+ create policy "Signed-in people read shipped features" on public.features for select to authenticated
376
+ using (
377
+ in_registry and status in ('beta', 'live', 'deprecated') and visibility in ('customer', 'public')
378
+ and not coalesce((select (auth.jwt() ->> 'is_anonymous')::boolean), false)
379
+ );
380
+
381
+ revoke all on table public.feature_flags from public, anon, authenticated, service_role;
382
+ grant select on table public.feature_flags to service_role;
383
+ -- The service role sets platform values; everything else in the row belongs to the sync.
384
+ grant update (platform_value) on table public.feature_flags to service_role;
385
+
386
+ revoke all on table public.feature_entitlements from public, anon, authenticated, service_role;
387
+ grant select, insert, update, delete on table public.feature_entitlements to service_role;
388
+
389
+ revoke all on table public.tenant_flag_values from public, anon, authenticated, service_role;
390
+ grant select, insert, update, delete on table public.tenant_flag_values to service_role;
391
+
392
+ revoke all on table private.feature_registry_state from public, anon, authenticated, service_role;
393
+ revoke execute on function private.genesis_feature_caller_is_server() from public, anon, authenticated;
394
+ revoke execute on function private.genesis_feature_may_read(uuid) from public, anon, authenticated;
395
+
396
+ revoke execute on function public.sync_feature_registry(bigint, text, jsonb, jsonb, boolean) from public, anon, authenticated;
397
+ grant execute on function public.sync_feature_registry(bigint, text, jsonb, jsonb, boolean) to service_role;
398
+ revoke execute on function public.feature_registry_synced() from public, anon, authenticated;
399
+ grant execute on function public.feature_registry_synced() to service_role;
400
+ revoke execute on function public.feature_gate_state(uuid) from public, anon;
401
+ grant execute on function public.feature_gate_state(uuid) to authenticated, service_role;
402
+
403
+ -- For one row or a function: `public.feature_on(tenant_id, 'my-feature')`; over many rows prefer
404
+ -- feature_shipped and feature_entitled_tenants (above). Policies run as the
405
+ -- querying role, so authenticated needs execute. Anon gets nothing: without a signed-in person
406
+ -- the function answers for any tenant, which is for server code only.
407
+ revoke execute on function public.feature_on(uuid, text) from public, anon;
408
+ grant execute on function public.feature_on(uuid, text) to authenticated, service_role;
409
+ revoke execute on function public.feature_shipped(text) from public, anon;
410
+ grant execute on function public.feature_shipped(text) to authenticated, service_role;
411
+ revoke execute on function public.feature_entitled_tenants(text) from public, anon;
412
+ grant execute on function public.feature_entitled_tenants(text) to authenticated, service_role;
@@ -0,0 +1,129 @@
1
+ -- @feat:jobs
2
+ -- pgTAP test for 0007_genesis_jobs.sql.
3
+ -- Copy into your app's `supabase/tests/database/` and run `supabase test db`.
4
+ -- It writes only its own jobs and rolls everything back. The schedule checks run when pg_cron
5
+ -- is installed and pass as skipped when it isn't.
6
+ begin;
7
+ create extension if not exists pgtap with schema extensions;
8
+ select plan(46);
9
+
10
+ -- Access: only the service role reaches the queue, and only through the functions.
11
+ select ok(not has_function_privilege('anon', 'public.genesis_job_enqueue(text, jsonb, timestamptz, text, integer)', 'execute'), 'anon cannot enqueue');
12
+ select ok(not has_function_privilege('authenticated', 'public.genesis_job_enqueue(text, jsonb, timestamptz, text, integer)', 'execute'), 'authenticated cannot enqueue');
13
+ select ok(not has_function_privilege('authenticated', 'public.genesis_jobs_claim(text[], integer, integer)', 'execute'), 'authenticated cannot claim');
14
+ select ok(not has_function_privilege('authenticated', 'public.genesis_jobs_settle(uuid, uuid, text, integer, text)', 'execute'), 'authenticated cannot settle');
15
+ select ok(not has_function_privilege('authenticated', 'public.genesis_schedule_job(text, text, text)', 'execute'), 'authenticated cannot schedule');
16
+ select ok(not has_function_privilege('authenticated', 'private.genesis_jobs_call(text)', 'execute'), 'authenticated cannot call a job route');
17
+ select ok(has_function_privilege('service_role', 'public.genesis_jobs_claim(text[], integer, integer)', 'execute'), 'service_role claims');
18
+ select ok(not has_table_privilege('authenticated', 'private.genesis_jobs', 'select'), 'authenticated cannot read the queue');
19
+ select ok((select relrowsecurity from pg_class where oid = 'private.genesis_jobs'::regclass), 'the queue has row level security on');
20
+
21
+ -- Start from an empty queue so other jobs in the app's database don't change the counts.
22
+ create temp table saved_jobs as select * from private.genesis_jobs;
23
+ delete from private.genesis_jobs;
24
+
25
+ -- Enqueue.
26
+ select throws_ok($$ select public.genesis_job_enqueue('Bad Kind') $$, '23514', null, 'a kind is lowercase');
27
+ select throws_ok($$ select public.genesis_job_enqueue('test.a', '[1]'::jsonb) $$, '22023', null, 'a payload is an object');
28
+
29
+ create temp table ids (name text primary key, id uuid);
30
+ insert into ids values ('a1', public.genesis_job_enqueue('test.a', '{"n": 1}'));
31
+ insert into ids values ('a2', public.genesis_job_enqueue('test.a', '{"n": 2}', p_idempotency_key => 'k'));
32
+ select is(public.genesis_job_enqueue('test.a', '{"n": 3}', p_idempotency_key => 'k'), (select id from ids where name = 'a2'),
33
+ 'an enqueue with a queued key returns the queued job');
34
+ select isnt(public.genesis_job_enqueue('test.b', '{}', p_idempotency_key => 'k'), (select id from ids where name = 'a2'),
35
+ 'the same key on another kind is another job');
36
+ insert into ids values ('later', public.genesis_job_enqueue('test.a', '{}', now() + interval '1 hour'));
37
+ select is((select count(*)::int from private.genesis_jobs), 4, 'four jobs queued');
38
+
39
+ -- Claim.
40
+ select throws_ok($$ select * from public.genesis_jobs_claim(null, 0) $$, '22023', null, 'a claim takes 1 or more');
41
+ create temp table claim1 as select * from public.genesis_jobs_claim(array['test.a'], 10, 60);
42
+ select is((select count(*)::int from claim1), 2, 'a claim hands out the due jobs of its kinds, not the future one');
43
+ select ok((select bool_and(attempt = 1 and not redelivery) from claim1), 'first attempts, not redeliveries');
44
+ select is((select count(distinct claim_id)::int from claim1), 1, 'one claim id for the batch');
45
+ select is((select count(*)::int from public.genesis_jobs_claim(array['test.a'], 10, 60)), 0, 'leased jobs are not handed out again');
46
+ select is((select count(*)::int from public.genesis_jobs_claim(null, 10, 60)), 1, 'no kinds claims every kind (test.b)');
47
+
48
+ -- Settle.
49
+ select ok(public.genesis_jobs_settle((select id from ids where name = 'a1'), (select claim_id from claim1 limit 1), 'done'), 'done settles');
50
+ select is((select status from private.genesis_jobs where id = (select id from ids where name = 'a1')), 'done', 'the job is done');
51
+ select ok(not public.genesis_jobs_settle((select id from ids where name = 'a1'), (select claim_id from claim1 limit 1), 'done'), 'settling twice is stale');
52
+ select ok(not public.genesis_jobs_settle((select id from ids where name = 'a2'), gen_random_uuid(), 'done'), 'another claim id is stale');
53
+ select throws_ok(format('select public.genesis_jobs_settle(%L, %L, %L)', (select id from ids where name = 'a2'), (select claim_id from claim1 limit 1), 'retry'),
54
+ '22023', null, 'a retry needs a delay');
55
+ select ok(public.genesis_jobs_settle((select id from ids where name = 'a2'), (select claim_id from claim1 limit 1), 'retry', 0, 'timeout'), 'retry settles');
56
+ select is((select status || ':' || last_error from private.genesis_jobs where id = (select id from ids where name = 'a2')), 'pending:timeout', 'a retry is pending again with its error');
57
+ select ok(public.genesis_job_enqueue('test.a', '{}', p_idempotency_key => 'k') = (select id from ids where name = 'a2'), 'a retrying job still holds its key');
58
+
59
+ -- Out of attempts: a retry on the last attempt fails.
60
+ update private.genesis_jobs set max_attempts = 2 where id = (select id from ids where name = 'a2');
61
+ create temp table claim2 as select * from public.genesis_jobs_claim(array['test.a'], 10, 60);
62
+ select is((select attempt from claim2 where id = (select id from ids where name = 'a2')), 2, 'the second attempt');
63
+ select ok(public.genesis_jobs_settle((select id from ids where name = 'a2'), (select claim_id from claim2 limit 1), 'retry', 0, 'timeout again'), 'the last retry settles');
64
+ select is((select status from private.genesis_jobs where id = (select id from ids where name = 'a2')), 'failed', 'out of attempts, it fails');
65
+ select isnt(public.genesis_job_enqueue('test.a', '{}', p_idempotency_key => 'k'), (select id from ids where name = 'a2'), 'a failed job frees its key');
66
+
67
+ -- Lease expiry: redelivered, then failed as lease_expired when out of attempts.
68
+ delete from private.genesis_jobs;
69
+ insert into ids values ('lease', public.genesis_job_enqueue('test.lease', '{}', p_max_attempts => 2));
70
+ select is((select count(*)::int from public.genesis_jobs_claim(array['test.lease'], 1, 60)), 1, 'the first attempt is claimed');
71
+ update private.genesis_jobs set lease_until = now() - interval '1 second' where id = (select id from ids where name = 'lease');
72
+ select is((select redelivery from public.genesis_jobs_claim(array['test.lease'], 1, 60)), true, 'a job whose lease ran out comes back as a redelivery');
73
+ update private.genesis_jobs set lease_until = now() - interval '1 second' where id = (select id from ids where name = 'lease');
74
+ select is((select count(*)::int from public.genesis_jobs_claim(array['test.lease'], 1, 60)), 0, 'out of attempts with the lease gone, it is not handed out');
75
+ select is((select status || ':' || last_error from private.genesis_jobs where id = (select id from ids where name = 'lease')), 'failed:lease_expired', 'it fails as lease_expired');
76
+
77
+ -- Cancel and purge.
78
+ insert into ids values ('cancel', public.genesis_job_enqueue('test.c', '{}'));
79
+ select ok(public.genesis_job_cancel((select id from ids where name = 'cancel')), 'a pending job cancels');
80
+ update private.genesis_jobs set finished_at = now() - interval '30 days';
81
+ select is(private.genesis_jobs_purge(), 2, 'finished jobs older than 14 days are purged');
82
+
83
+ -- Schedules. cron.job is read through a function so this file parses without pg_cron.
84
+ create function pg_temp.cron_has(p_name text) returns boolean language plpgsql as $$
85
+ declare found boolean;
86
+ begin
87
+ execute 'select exists (select 1 from cron.job where jobname = $1)' into found using p_name;
88
+ return found;
89
+ end $$;
90
+ select throws_ok($$ select public.genesis_schedule_job('Bad Name', '* * * * *', 'x') $$, '22023', null, 'a schedule name is lowercase');
91
+ select throws_ok($$ select private.genesis_jobs_call('../admin') $$, '22023', null, 'a job name is never a path');
92
+ select case when to_regnamespace('cron') is null
93
+ then skip('pg_cron is not installed', 1)
94
+ else ok(public.genesis_schedule_job('genesis-jobs-test', '*/5 * * * *', 'digests') > 0, 'schedules through pg_cron') end;
95
+ select case when to_regnamespace('cron') is null
96
+ then skip('pg_cron is not installed', 1)
97
+ else ok(pg_temp.cron_has('genesis-job:genesis-jobs-test'), 'a schedule is the cron job genesis-job:<name>') end;
98
+ create temp table unscheduled as select case when to_regnamespace('cron') is null then null else public.genesis_unschedule_job('genesis-jobs-test') end as removed;
99
+ select case when to_regnamespace('cron') is null
100
+ then skip('pg_cron is not installed', 1)
101
+ else ok((select removed from unscheduled)
102
+ and not pg_temp.cron_has('genesis-job:genesis-jobs-test'), 'unschedules it') end;
103
+ select case when to_regnamespace('cron') is null
104
+ then skip('pg_cron is not installed', 1)
105
+ else ok(not public.genesis_unschedule_job('genesis-jobs-purge'), 'never touches a cron job it did not make') end;
106
+
107
+ -- A call is signed, never carrying the secret. Needs Vault, pg_net and pgcrypto; skipped without.
108
+ -- pg_net's queue, read through a function so this file parses without pg_net.
109
+ create function pg_temp.last_call_signed() returns boolean language plpgsql as $$
110
+ declare ok boolean;
111
+ begin
112
+ execute $q$select url = 'http://127.0.0.1:9/api/jobs/digests'
113
+ and headers->>'Authorization' ~ '^GenesisJob t=[0-9]+,sig=[0-9a-f]{64}$'
114
+ and headers::text !~ 'genesis-test-secret'
115
+ from net.http_request_queue order by id desc limit 1$q$ into ok;
116
+ return ok;
117
+ end $$;
118
+ create temp table call_ready as select to_regclass('vault.secrets') is not null and to_regnamespace('net') is not null
119
+ and to_regprocedure('extensions.hmac(text, text, text)') is not null as ready;
120
+ select case when not (select ready from call_ready)
121
+ then skip('Vault, pg_net or pgcrypto is not installed', 1)
122
+ else lives_ok($$ select vault.create_secret('http://127.0.0.1:9/api/jobs/', 'genesis_jobs_url'), vault.create_secret('genesis-test-secret', 'genesis_jobs_secret'), private.genesis_jobs_call('digests') $$, 'a call queues a request') end;
123
+ select case when not (select ready from call_ready)
124
+ then skip('Vault, pg_net or pgcrypto is not installed', 1)
125
+ else ok(pg_temp.last_call_signed(), 'posts to <url>/<job>, signed, with no secret in the queue') end;
126
+
127
+ insert into private.genesis_jobs select * from saved_jobs;
128
+ select * from finish();
129
+ rollback;