@marktiderman/genesis-supabase 0.15.0 → 0.16.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,388 @@
1
+ -- @feat:settings
2
+ -- pgTAP test for 0020_genesis_settings.sql.
3
+ -- Copy into your app's `supabase/tests/database/` and run `supabase test db`.
4
+ -- It writes only its own users, manifest, settings and hook and rolls everything back. If your app
5
+ -- already has the hook (`private.genesis_platform_admin`) or a manifest, they are replaced inside
6
+ -- this transaction only.
7
+ --
8
+ -- Signing in: auth.uid() reads request.jwt.claims (and the legacy request.jwt.claim.sub on older
9
+ -- images), so pg_temp.sign_in sets both.
10
+ begin;
11
+ create extension if not exists pgtap with schema extensions;
12
+ select plan(77);
13
+
14
+ -- People: a1 a platform admin, b2 signed in but not an admin.
15
+ insert into auth.users (id, aud, role, email, instance_id)
16
+ select ('00000000-0000-4000-8000-0000000000' || n)::uuid, 'authenticated', 'authenticated', 'genesis-settings-' || n || '@example.test', '00000000-0000-0000-0000-000000000000'
17
+ from unnest(array['a1', 'b2']) n;
18
+
19
+ create function pg_temp.person(n text) returns uuid language sql immutable as $$ select ('00000000-0000-4000-8000-0000000000' || n)::uuid $$;
20
+ create function pg_temp.sign_in(n text) returns void language plpgsql as $$
21
+ begin
22
+ perform set_config('request.jwt.claims', json_build_object('sub', pg_temp.person(n), 'role', 'authenticated')::text, true);
23
+ perform set_config('request.jwt.claim.sub', pg_temp.person(n)::text, true);
24
+ end $$;
25
+ create function pg_temp.sign_out() returns void language plpgsql as $$
26
+ begin
27
+ perform set_config('request.jwt.claims', '{"role":"authenticated"}', true);
28
+ perform set_config('request.jwt.claim.sub', '', true);
29
+ end $$;
30
+ grant execute on function pg_temp.person(text), pg_temp.sign_in(text), pg_temp.sign_out() to anon, authenticated, service_role;
31
+
32
+ drop function if exists private.genesis_platform_admin(uuid);
33
+ delete from public.genesis_settings;
34
+ delete from public.genesis_app_manifest;
35
+
36
+ -- Shape ---------------------------------------------------------------------------------
37
+ select has_table('public', 'genesis_settings', 'genesis_settings exists');
38
+ select col_is_pk('public', 'genesis_settings', array['key'], 'one row per key');
39
+ select has_table('public', 'genesis_setting_changes', 'genesis_setting_changes exists');
40
+ select has_table('public', 'genesis_app_manifest', 'genesis_app_manifest exists');
41
+ select ok(
42
+ (select bool_and(relrowsecurity) from pg_class where oid in ('public.genesis_settings'::regclass, 'public.genesis_setting_changes'::regclass, 'public.genesis_app_manifest'::regclass)),
43
+ 'RLS is on for all three tables'
44
+ );
45
+ select is(
46
+ (select count(*)::int from pg_policy where polrelid in ('public.genesis_settings'::regclass, 'public.genesis_setting_changes'::regclass, 'public.genesis_app_manifest'::regclass)),
47
+ 0,
48
+ 'no policies: the tables are reached only through the functions'
49
+ );
50
+
51
+ -- Privileges ---------------------------------------------------------------------------------
52
+ select ok(
53
+ not has_table_privilege('anon', 'public.genesis_settings', 'select,insert,update,delete,truncate')
54
+ and not has_table_privilege('authenticated', 'public.genesis_settings', 'select,insert,update,delete,truncate'),
55
+ 'anon and authenticated have no privilege on the settings'
56
+ );
57
+ select ok(
58
+ not has_table_privilege('anon', 'public.genesis_setting_changes', 'select,insert,update,delete,truncate')
59
+ and not has_table_privilege('authenticated', 'public.genesis_setting_changes', 'select,insert,update,delete,truncate'),
60
+ '... nor on the history'
61
+ );
62
+ select ok(
63
+ not has_table_privilege('anon', 'public.genesis_app_manifest', 'select,insert,update,delete,truncate')
64
+ and not has_table_privilege('authenticated', 'public.genesis_app_manifest', 'select,insert,update,delete,truncate'),
65
+ '... nor on the manifest'
66
+ );
67
+ select ok(
68
+ not exists (
69
+ select 1 from pg_class c, aclexplode(coalesce(c.relacl, acldefault('r', c.relowner))) a
70
+ where c.oid in ('public.genesis_settings'::regclass, 'public.genesis_setting_changes'::regclass, 'public.genesis_app_manifest'::regclass) and a.grantee = 0
71
+ ),
72
+ 'public has no grant on any of them'
73
+ );
74
+ select ok(
75
+ has_table_privilege('service_role', 'public.genesis_settings', 'select,insert,update,delete')
76
+ and has_table_privilege('service_role', 'public.genesis_app_manifest', 'select,insert,update,delete')
77
+ and not has_table_privilege('service_role', 'public.genesis_settings', 'truncate'),
78
+ 'the service role writes the settings and the manifest, but cannot truncate the settings (which would skip the history)'
79
+ );
80
+ select ok(
81
+ not has_table_privilege('service_role', 'public.genesis_setting_changes', 'insert')
82
+ and not has_table_privilege('service_role', 'public.genesis_setting_changes', 'update')
83
+ and not has_table_privilege('service_role', 'public.genesis_setting_changes', 'delete')
84
+ and has_table_privilege('service_role', 'public.genesis_setting_changes', 'select'),
85
+ 'the service role reads the history but never writes or deletes it directly'
86
+ );
87
+ select ok(
88
+ (select bool_and(prosecdef and proconfig @> array['search_path=""'] and proowner::regrole::text = 'postgres') from pg_proc
89
+ where oid in (
90
+ 'public.set_genesis_setting(text, jsonb, boolean)'::regprocedure,
91
+ 'public.clear_genesis_setting(text)'::regprocedure,
92
+ 'public.genesis_settings_layers()'::regprocedure,
93
+ 'public.genesis_setting_history(integer)'::regprocedure,
94
+ 'public.genesis_prune_setting_changes(interval)'::regprocedure,
95
+ 'private.genesis_settings_caller_is_server()'::regprocedure,
96
+ 'private.genesis_is_platform_admin()'::regprocedure,
97
+ 'private.genesis_setting_problem(text, jsonb)'::regprocedure,
98
+ 'private.genesis_record_setting_change()'::regprocedure
99
+ )),
100
+ 'every function is security definer, owned by postgres, with an empty search_path'
101
+ );
102
+ select ok(
103
+ not has_function_privilege('anon', 'public.set_genesis_setting(text, jsonb, boolean)', 'execute')
104
+ and not has_function_privilege('anon', 'public.clear_genesis_setting(text)', 'execute')
105
+ and not has_function_privilege('anon', 'public.genesis_settings_layers()', 'execute')
106
+ and not has_function_privilege('anon', 'public.genesis_setting_history(integer)', 'execute'),
107
+ 'anon executes none of the public functions'
108
+ );
109
+ select ok(
110
+ has_function_privilege('authenticated', 'public.set_genesis_setting(text, jsonb, boolean)', 'execute')
111
+ and has_function_privilege('authenticated', 'public.clear_genesis_setting(text)', 'execute')
112
+ and has_function_privilege('authenticated', 'public.genesis_settings_layers()', 'execute')
113
+ and has_function_privilege('authenticated', 'public.genesis_setting_history(integer)', 'execute'),
114
+ 'authenticated executes them (each checks who is asking)'
115
+ );
116
+ select ok(
117
+ not exists (
118
+ select 1 from pg_proc p, aclexplode(coalesce(p.proacl, acldefault('f', p.proowner))) a
119
+ where p.oid in (
120
+ 'public.set_genesis_setting(text, jsonb, boolean)'::regprocedure,
121
+ 'public.clear_genesis_setting(text)'::regprocedure,
122
+ 'public.genesis_settings_layers()'::regprocedure,
123
+ 'public.genesis_setting_history(integer)'::regprocedure,
124
+ 'private.genesis_is_platform_admin()'::regprocedure,
125
+ 'private.genesis_setting_problem(text, jsonb)'::regprocedure,
126
+ 'private.genesis_record_setting_change()'::regprocedure
127
+ ) and a.grantee = 0 and a.privilege_type = 'EXECUTE'
128
+ ),
129
+ 'public executes none of them'
130
+ );
131
+ select ok(
132
+ not has_function_privilege('authenticated', 'private.genesis_is_platform_admin()', 'execute')
133
+ and not has_function_privilege('authenticated', 'private.genesis_setting_problem(text, jsonb)', 'execute')
134
+ and not has_function_privilege('authenticated', 'private.genesis_record_setting_change()', 'execute'),
135
+ 'the hook check, the validator and the trigger are not executable by authenticated'
136
+ );
137
+ select ok(
138
+ not has_sequence_privilege('anon', 'public.genesis_setting_changes_id_seq', 'usage,select,update')
139
+ and not has_sequence_privilege('authenticated', 'public.genesis_setting_changes_id_seq', 'usage,select,update'),
140
+ 'the history''s id sequence is not anon''s or authenticated''s'
141
+ );
142
+
143
+ -- The manifest is one row -----------------------------------------------------------------
144
+ insert into public.genesis_app_manifest (app_id, name, packages, preset, file_settings, catalogue) values (
145
+ 'sample-app', 'Sample app',
146
+ '{"@marktiderman/genesis-config": "0.10.1"}',
147
+ 'standard',
148
+ '{"support.dumpRetentionDays": 30, "support.diagnostics": {"value": true, "locked": true}, "support.sentryDsn": "https://dsn.example"}',
149
+ '{
150
+ "support.inbox": {"type": "boolean", "applies": "runtime", "scope": "app"},
151
+ "support.diagnostics": {"type": "boolean", "applies": "runtime", "scope": "app"},
152
+ "support.dumpRetentionDays": {"type": "number", "applies": "runtime", "scope": "app", "min": 7, "max": 365},
153
+ "support.channel": {"type": "string", "applies": "runtime", "scope": "app", "options": ["chat", "email"]},
154
+ "support.languages": {"type": "list", "applies": "runtime", "scope": "app", "options": ["en", "nl"], "min": 1, "max": 2},
155
+ "support.shake": {"type": "object", "applies": "runtime", "scope": "app"},
156
+ "support.githubRepo": {"type": "string", "applies": "runtime", "scope": "app", "nullable": true},
157
+ "support.badMin": {"type": "number", "applies": "runtime", "scope": "app", "min": "abc"},
158
+ "support.weird": {"type": "wat", "applies": "runtime", "scope": "app"},
159
+ "support.badOptions": {"type": "string", "applies": "runtime", "scope": "app", "options": "a"},
160
+ "support.sentryDsn": {"type": "string", "applies": "build", "scope": "app"}
161
+ }'
162
+ );
163
+ select throws_ok(
164
+ $$ insert into public.genesis_app_manifest (app_id) values ('other-app') $$,
165
+ '23505', null, 'a second manifest row is refused'
166
+ );
167
+ select throws_ok(
168
+ $$ update public.genesis_app_manifest set preset = 'huge' $$,
169
+ '23514', null, 'the preset is max, standard or min'
170
+ );
171
+
172
+ -- Without the hook, nobody may change anything ------------------------------------------------
173
+ select pg_temp.sign_in('a1');
174
+ set local role authenticated;
175
+ select throws_ok(
176
+ $$ select public.set_genesis_setting('support.inbox', 'false') $$,
177
+ '42501', 'set_genesis_setting: nobody may change settings yet',
178
+ 'without private.genesis_platform_admin, set_genesis_setting refuses (fails closed)'
179
+ );
180
+ select throws_ok(
181
+ $$ select public.clear_genesis_setting('support.inbox') $$,
182
+ '42501', null, '... and clear_genesis_setting too'
183
+ );
184
+ select throws_ok(
185
+ $$ select * from public.genesis_setting_history() $$,
186
+ '42501', null, '... and nobody may read the history'
187
+ );
188
+ select is(
189
+ public.genesis_settings_layers() ->> 'preset',
190
+ 'standard',
191
+ 'a signed-in person still reads the resolved inputs without the hook'
192
+ );
193
+ reset role;
194
+
195
+ -- The app's hook, reading a table of the test's own -------------------------------------------
196
+ create table private.genesis_test_platform_admins (person uuid primary key);
197
+ insert into private.genesis_test_platform_admins values (pg_temp.person('a1'));
198
+ create function private.genesis_platform_admin(person uuid) returns boolean
199
+ language sql stable set search_path = '' as $$
200
+ select exists (select 1 from private.genesis_test_platform_admins a where a.person = $1)
201
+ $$;
202
+ revoke execute on function private.genesis_platform_admin(uuid) from public, anon, authenticated;
203
+
204
+ -- Who may change a setting --------------------------------------------------------------------
205
+ set local role anon;
206
+ select throws_ok($$ select public.set_genesis_setting('support.inbox', 'false') $$, '42501', null, 'anon cannot call set_genesis_setting');
207
+ select throws_ok($$ select public.genesis_settings_layers() $$, '42501', null, '... nor read the settings');
208
+ reset role;
209
+
210
+ select pg_temp.sign_out();
211
+ set local role authenticated;
212
+ select throws_ok($$ select public.set_genesis_setting('support.inbox', 'false') $$, '42501', 'set_genesis_setting: not signed in', 'a call with nobody signed in is refused');
213
+ select throws_ok($$ select public.genesis_settings_layers() $$, '42501', 'genesis_settings_layers: not signed in', '... and so is a read');
214
+ reset role;
215
+
216
+ select pg_temp.sign_in('b2');
217
+ set local role authenticated;
218
+ select throws_ok(
219
+ $$ select public.set_genesis_setting('support.inbox', 'false') $$,
220
+ '42501', 'set_genesis_setting: only platform admins can change settings', 'a signed-in person who isn''t a platform admin is refused'
221
+ );
222
+ select throws_ok($$ select public.clear_genesis_setting('support.inbox') $$, '42501', null, '... for clearing too');
223
+ select throws_ok($$ select * from public.genesis_setting_history() $$, '42501', null, '... and for the history');
224
+ reset role;
225
+
226
+ -- What a platform admin may store: the catalogue decides ---------------------------------------
227
+ select pg_temp.sign_in('a1');
228
+ set local role authenticated;
229
+ select throws_ok($$ select public.set_genesis_setting('support.nope', 'true') $$, '22023', 'set_genesis_setting: support.nope isn''t a setting any installed package declares', 'an unknown key is refused');
230
+ select throws_ok($$ select public.set_genesis_setting('support.sentryDsn', '"https://x"') $$, '22023', 'set_genesis_setting: support.sentryDsn applies at build time: change it in genesis.config.json', 'a build-time key is refused');
231
+ select throws_ok($$ select public.set_genesis_setting('support.inbox', '"yes"') $$, '22023', 'set_genesis_setting: support.inbox must be a boolean', 'a value of the wrong type is refused');
232
+ select throws_ok($$ select public.set_genesis_setting('support.dumpRetentionDays', '400') $$, '22023', 'set_genesis_setting: support.dumpRetentionDays must be at most 365', 'a number above its max is refused');
233
+ select throws_ok($$ select public.set_genesis_setting('support.dumpRetentionDays', '3') $$, '22023', 'set_genesis_setting: support.dumpRetentionDays must be at least 7', '... and below its min');
234
+ select throws_ok($$ select public.set_genesis_setting('support.channel', '"sms"') $$, '22023', null, 'a string outside its options is refused');
235
+ select throws_ok($$ select public.set_genesis_setting('support.languages', '["en", "fr"]') $$, '22023', null, 'a list item outside its options is refused');
236
+ select throws_ok($$ select public.set_genesis_setting('support.languages', '["en", "nl", "en"]') $$, '22023', 'set_genesis_setting: support.languages must be at most 2 items', 'a list longer than its max is refused');
237
+ select throws_ok($$ select public.set_genesis_setting('support.shake', '[1]') $$, '22023', null, 'an object key refuses an array');
238
+ select throws_ok($$ select public.set_genesis_setting('support.inbox', null) $$, '22023', 'set_genesis_setting: support.inbox can''t be null: it has a default, so set a value', 'null (as PostgREST sends JSON null) is refused for a key with a default');
239
+ select throws_ok($$ select public.set_genesis_setting('support.inbox', 'null') $$, '22023', null, '... as JSON null too');
240
+ select throws_ok($$ select public.set_genesis_setting(null, 'true') $$, '22004', null, 'a key is required');
241
+ select throws_ok($$ select public.set_genesis_setting('support.diagnostics', 'false') $$, '22023', 'set_genesis_setting: support.diagnostics is locked by genesis.config.json: change it there', 'a key genesis.config.json locks is refused');
242
+ select throws_ok($$ select public.set_genesis_setting('support.badMin', '5') $$, '22023', 'set_genesis_setting: the synced catalogue''s entry for support.badMin is malformed (write the manifest again: writeAppManifest)', 'a malformed catalogue min refuses the key, with no raw cast error');
243
+ select throws_ok($$ select public.set_genesis_setting('support.weird', '{"anything": [1]}') $$, '22023', null, '... and so does an unknown type');
244
+ select throws_ok($$ select public.set_genesis_setting('support.badOptions', '"a"') $$, '22023', null, '... and options that aren''t a list');
245
+ select throws_ok($$ select public.set_genesis_setting('__proto__', 'true') $$, '22023', null, 'a key that isn''t namespace.key is refused');
246
+ select throws_ok($$ select * from public.genesis_settings $$, '42501', null, 'authenticated cannot read the table directly');
247
+ select throws_ok($$ insert into public.genesis_settings (key, value) values ('support.inbox', 'false') $$, '42501', null, '... nor write it');
248
+ select throws_ok($$ select private.genesis_is_platform_admin() $$, '42501', null, '... nor call the hook check itself');
249
+
250
+ select lives_ok($$ select public.set_genesis_setting('support.dumpRetentionDays', '60') $$, 'the platform admin sets a number in bounds');
251
+ select lives_ok($$ select public.set_genesis_setting('support.inbox', 'false', true) $$, '... and locks a boolean');
252
+ select lives_ok($$ select public.set_genesis_setting('support.githubRepo', null) $$, '... and sets a key whose default is null to "not set" (SQL null, as PostgREST sends it, is JSON null)');
253
+ select lives_ok($$ select public.set_genesis_setting('support.dumpRetentionDays', '60') $$, '... and saves the same value again');
254
+ reset role;
255
+
256
+ select is(
257
+ (select array_agg(key || '=' || value::text || ':' || locked order by key) from public.genesis_settings),
258
+ array['support.dumpRetentionDays=60:false', 'support.githubRepo=null:false', 'support.inbox=false:true'],
259
+ 'the values and the lock are stored'
260
+ );
261
+ select is(
262
+ (select updated_by from public.genesis_settings where key = 'support.inbox'),
263
+ pg_temp.person('a1'),
264
+ '... with who set them'
265
+ );
266
+ select is(
267
+ (select count(*)::int from public.genesis_setting_changes where key = 'support.dumpRetentionDays'),
268
+ 1,
269
+ 'saving the same value again records nothing'
270
+ );
271
+ select is(
272
+ (select source || ':' || changed_role || ':' || coalesce(old_value::text, '-') || '>' || new_value::text from public.genesis_setting_changes where key = 'support.inbox'),
273
+ 'admin:authenticated:->false',
274
+ 'the history records the source, the role and the old and new value'
275
+ );
276
+
277
+ -- What a signed-in person reads --------------------------------------------------------------
278
+ select pg_temp.sign_in('b2');
279
+ set local role authenticated;
280
+ select is(
281
+ public.genesis_settings_layers() - 'reportedAt',
282
+ '{
283
+ "preset": "standard",
284
+ "file": {"support.dumpRetentionDays": 30, "support.diagnostics": {"value": true, "locked": true}, "support.sentryDsn": "https://dsn.example"},
285
+ "database": {
286
+ "support.inbox": {"value": false, "locked": true},
287
+ "support.githubRepo": {"value": null, "locked": false},
288
+ "support.dumpRetentionDays": {"value": 60, "locked": false}
289
+ }
290
+ }'::jsonb,
291
+ 'any signed-in person reads the preset, the file (build-time keys too) and the database layer (runtime keys)'
292
+ );
293
+ reset role;
294
+ set local role service_role;
295
+ select set_config('request.jwt.claims', '{"role":"service_role"}', true);
296
+ select set_config('request.jwt.claim.sub', '', true);
297
+ select is(public.genesis_settings_layers() ->> 'preset', 'standard', 'server code reads the layers too');
298
+ select throws_ok($$ delete from public.genesis_setting_changes $$, '42501', null, 'the service role cannot wipe the history');
299
+ select throws_ok($$ select public.genesis_prune_setting_changes(interval '1 day') $$, '22023', null, '... nor prune the last year of it');
300
+ select is(public.genesis_prune_setting_changes(), 0, '... and prunes only rows older than a year');
301
+ reset role;
302
+ select pg_temp.sign_in('b2');
303
+ set local role authenticated;
304
+ select throws_ok($$ select public.genesis_prune_setting_changes() $$, '42501', null, 'a signed-in person cannot prune the history');
305
+ reset role;
306
+
307
+ -- The service role, and keys the catalogue drops ---------------------------------------------
308
+ set local role service_role;
309
+ select set_config('request.jwt.claims', '{"role":"service_role"}', true);
310
+ select set_config('request.jwt.claim.sub', '', true);
311
+ insert into public.genesis_settings (key, value) values ('legacy.removed', '1');
312
+ select set_config('genesis.setting_source', 'cli', true);
313
+ update public.genesis_settings set value = '90' where key = 'support.dumpRetentionDays';
314
+ select set_config('genesis.setting_source', '', true);
315
+ update public.genesis_settings set key = 'legacy.renamed' where key = 'legacy.removed';
316
+ reset role;
317
+ select is(
318
+ (select source || ':' || changed_role from public.genesis_setting_changes where key = 'legacy.removed' order by id limit 1),
319
+ 'server:service_role',
320
+ 'a service-role write is recorded as from the server, with no person'
321
+ );
322
+ select is(
323
+ (select source from public.genesis_setting_changes where key = 'support.dumpRetentionDays' order by id desc limit 1),
324
+ 'cli',
325
+ '... or from the source it names'
326
+ );
327
+ select is(
328
+ (select array_agg(key || ':' || coalesce(old_value::text, '-') || '>' || coalesce(new_value::text, '-') order by id) from public.genesis_setting_changes where key in ('legacy.removed', 'legacy.renamed')),
329
+ array['legacy.removed:->1', 'legacy.removed:1>-', 'legacy.renamed:->1'],
330
+ 'a renamed key is recorded as the old key going and the new one coming'
331
+ );
332
+ select pg_temp.sign_in('b2');
333
+ set local role authenticated;
334
+ select ok(
335
+ not (public.genesis_settings_layers() -> 'database' ? 'legacy.removed'),
336
+ 'a stored key the catalogue no longer declares isn''t returned'
337
+ );
338
+ reset role;
339
+
340
+ -- Clearing, and the history ------------------------------------------------------------------
341
+ select pg_temp.sign_in('a1');
342
+ set local role authenticated;
343
+ select lives_ok($$ select public.clear_genesis_setting('support.inbox') $$, 'the platform admin clears a setting');
344
+ select is(
345
+ (select array_agg(key order by key) from public.genesis_setting_history(1000) where new_value is null and key like 'support.%'),
346
+ array['support.inbox'],
347
+ '... which the history records with no new value'
348
+ );
349
+ select is(
350
+ (select count(*)::int from public.genesis_setting_history(2)),
351
+ 2,
352
+ 'the history takes a row limit'
353
+ );
354
+ reset role;
355
+ select ok(
356
+ not exists (select 1 from public.genesis_settings where key = 'support.inbox'),
357
+ 'the row is gone, so the layers below apply again'
358
+ );
359
+
360
+ -- Without a synced catalogue, nothing is accepted ----------------------------------------------
361
+ update public.genesis_app_manifest set catalogue = '{}';
362
+ select pg_temp.sign_in('a1');
363
+ set local role authenticated;
364
+ select throws_ok(
365
+ $$ select public.set_genesis_setting('support.inbox', 'true') $$,
366
+ '22023', null, 'with no catalogue, set_genesis_setting refuses every write'
367
+ );
368
+ reset role;
369
+
370
+ -- Deleting a person never fails on these tables ------------------------------------------------
371
+ delete from auth.users where id = pg_temp.person('a1');
372
+ select is(
373
+ (select count(*)::int from public.genesis_settings where updated_by is not null),
374
+ 0,
375
+ 'deleting a person clears who set a setting'
376
+ );
377
+ select is(
378
+ (select count(*)::int from public.genesis_setting_changes where changed_by = pg_temp.person('a1')),
379
+ 0,
380
+ '... and who changed it, keeping the history'
381
+ );
382
+ select ok(
383
+ (select count(*) from public.genesis_setting_changes) > 0,
384
+ '... whose rows stay'
385
+ );
386
+
387
+ select * from finish();
388
+ rollback;
@@ -0,0 +1,21 @@
1
+ -- pgTAP test for 0021_genesis_settings_integer.sql: a number the catalogue declares whole refuses a
2
+ -- fraction; other numbers still take one; a malformed `integer` refuses every value. Rolls back.
3
+ begin;
4
+ create extension if not exists pgtap with schema extensions;
5
+ select plan(5);
6
+
7
+ delete from public.genesis_app_manifest;
8
+ insert into public.genesis_app_manifest (app_id, catalogue) values ('sample-app', '{
9
+ "diagnostics.perHour": {"type": "number", "applies": "runtime", "scope": "app", "min": 1, "max": 100, "integer": true},
10
+ "acme.ratio": {"type": "number", "applies": "runtime", "scope": "app"},
11
+ "acme.bad": {"type": "number", "applies": "runtime", "scope": "app", "integer": "yes"}
12
+ }');
13
+
14
+ select is(private.genesis_setting_problem('diagnostics.perHour', '2.5'), 'diagnostics.perHour must be a whole number', 'a fraction is refused for a whole number');
15
+ select is(private.genesis_setting_problem('diagnostics.perHour', '2'), null, 'a whole number is fine');
16
+ select is(private.genesis_setting_problem('diagnostics.perHour', '500'), 'diagnostics.perHour must be at most 100', 'bounds still hold');
17
+ select is(private.genesis_setting_problem('acme.ratio', '2.5'), null, 'other numbers still take a fraction');
18
+ select ok(private.genesis_setting_problem('acme.bad', '2') like '%malformed%', 'a malformed integer flag refuses every value');
19
+
20
+ select * from finish();
21
+ rollback;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marktiderman/genesis-supabase",
3
- "version": "0.15.0",
3
+ "version": "0.16.0",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -82,7 +82,7 @@
82
82
  "README.md"
83
83
  ],
84
84
  "dependencies": {
85
- "@marktiderman/genesis-features": "^0.9.0"
85
+ "@marktiderman/genesis-config": "^0.11.0"
86
86
  },
87
87
  "peerDependencies": {
88
88
  "@supabase/supabase-js": "^2.118.0",
@@ -332,6 +332,45 @@
332
332
  "For each table to check: add `version bigint not null default 1` and `create trigger <table>_version_bump before update on <table> for each row execute function public.genesis_version_bump('version')`.",
333
333
  "Give the resource a version rule in createSupabaseProvider: `resources: { tasks: { version: { field: \"version\", kind: \"integer\" } } }`. The Genesis write hooks then send the version they read, and a stale edit comes back as a conflict."
334
334
  ]
335
+ },
336
+ "settings": {
337
+ "feature": {
338
+ "name": "Settings in the app's database",
339
+ "summary": "The database layer of Genesis settings: runtime values platform admins set and lock live, every change audited, and the app manifest with the catalogue values are checked against.",
340
+ "keywords": [
341
+ "settings",
342
+ "configuration",
343
+ "platform admin",
344
+ "genesis_settings",
345
+ "preset"
346
+ ],
347
+ "audience": [
348
+ "developer",
349
+ "admin"
350
+ ]
351
+ },
352
+ "platforms": [
353
+ "web",
354
+ "native"
355
+ ],
356
+ "migrations": [
357
+ {
358
+ "file": "migrations/0020_genesis_settings.sql",
359
+ "test": "migrations/tests/0020_genesis_settings.test.sql"
360
+ },
361
+ {
362
+ "file": "migrations/0021_genesis_settings_integer.sql",
363
+ "test": "migrations/tests/0021_genesis_settings_integer.test.sql"
364
+ }
365
+ ],
366
+ "install": [
367
+ "@supabase/supabase-js@^2.118.0"
368
+ ],
369
+ "steps": [
370
+ "Say who your platform admins are in a migration of your own: create private.genesis_platform_admin(person uuid) returns boolean, and revoke execute on it from public, anon, authenticated (genesis-supabase README, Settings). Without it nobody can change a setting.",
371
+ "On every deploy, after the migrations, write the app manifest: genesis settings sync (through the linked project), or writeAppManifest(admin, { appId, packages, preset, fileSettings, catalogue }) with the service-role client from your server at start. Until it is written, set_genesis_setting refuses every value.",
372
+ "Read settings with resolveAppSettings(supabase, catalogue) from @marktiderman/genesis-supabase, and feed the gate with withSettings from @marktiderman/genesis-config."
373
+ ]
335
374
  }
336
375
  },
337
376
  "doctor": {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A,EAAa;AAAA,IACX,UAAA,EAAY;AAAA,MACV,EAAE,OAAA,EAAS,OAAA,EAAS,IAAA,EAAM,yCAAA,EAA2C,MAAM,oDAAA;AAAqD,KAClI;AAAA,IACA,OAAA,EAAS;AAAA,MACP,KAAA,EAAO;AAAA,QACL;AAAA;AACF;AACF;AAEJ,CAAC;AAUM,IAAM,cAAc,WAAA,CAAY;AAAA,EACrC,EAAA,EAAI,QAAA;AAAA,EACJ,SAAA,EAAW,CAAC,KAAA,EAAO,QAAQ,CAAA;AAAA,EAC3B,WAAA,EAAa;AAAA,IACX,UAAA,EAAY,CAAC,EAAE,OAAA,EAAS,SAAS,IAAA,EAAM,oCAAA,EAAsC,IAAA,EAAM,+CAAA,EAAiD,CAAA;AAAA,IACpI,OAAA,EAAS;AAAA,MACP,KAAA,EAAO;AAAA,QACL;AAAA;AACF;AACF;AAEJ,CAAC;AASM,IAAM,uBAAuB,WAAA,CAAY;AAAA,EAC9C,EAAA,EAAI,kBAAA;AAAA,EACJ,SAAA,EAAW,CAAC,KAAA,EAAO,QAAQ,CAAA;AAAA,EAC3B,WAAA,EAAa;AAAA,IACX,UAAA,EAAY;AAAA,MACV,EAAE,OAAA,EAAS,OAAA,EAAS,IAAA,EAAM,0CAAA,EAA4C,MAAM,qDAAA;AAAsD;AACpI;AAEJ,CAAC","file":"chunk-XESMEWXI.js","sourcesContent":["/**\n * The blocks this package carries the server half of, as `defineBlock`\n * declarations: what each needs in the database, and what it means for the\n * stores' privacy forms. The functions themselves (`deleteMyAccount`,\n * `tenantFeatures`, …) don't change.\n */\nimport { defineBlock } from \"@marktiderman/genesis-features/blocks\";\n\nconst PACKAGE = \"@marktiderman/genesis-supabase\";\n\n/**\n * The account block: sign-in, profile and in-app account deletion. Its\n * screens are the app's (or genesis-ui-native's `AuthScreen`); this names its\n * migrations and its data.\n *\n * @stability Beta\n */\nexport const accountBlock = defineBlock({\n id: \"account\",\n platforms: [\"web\", \"native\"],\n contributes: {\n migrations: [\n {\n package: PACKAGE,\n file: \"migrations/0001_genesis_account_deletion.sql\",\n test: \"migrations/tests/0001_genesis_account_deletion.test.sql\",\n },\n // With Sign in with Apple, with the genesis-apple-revoke function beside it.\n {\n package: PACKAGE,\n file: \"migrations/0003_genesis_apple_token_revocation.sql\",\n test: \"migrations/tests/0003_genesis_apple_token_revocation.test.sql\",\n },\n ],\n privacy: {\n collects: [\n {\n type: \"email address\",\n purposes: [\"app functionality\"],\n linked: true,\n tracking: false,\n },\n {\n type: \"user id\",\n purposes: [\"app functionality\"],\n linked: true,\n tracking: false,\n },\n {\n type: \"name\",\n purposes: [\"app functionality\"],\n linked: true,\n tracking: false,\n },\n ],\n notes: [\n \"People delete their account from inside the app; their login and the rows that cascade from it go with it.\",\n \"With Sign in with Apple, the person's Apple tokens are revoked when the account is deleted.\",\n ],\n },\n },\n});\n\n/**\n * The tenant features block: per-tenant on/off switches for shipped\n * features, set by the tenant's admins with a reason.\n *\n * @stability Beta\n */\nexport const tenantFeaturesBlock = defineBlock({\n id: \"tenant-features\",\n contributes: {\n migrations: [\n {\n package: PACKAGE,\n file: \"migrations/0004_genesis_tenant_features.sql\",\n test: \"migrations/tests/0004_genesis_tenant_features.test.sql\",\n },\n ],\n privacy: {\n notes: [\n \"Keeps who changed a tenant's switches; a deleted person's changes stay, without the person.\",\n ],\n },\n },\n});\n\n/**\n * The feature registry block: the registry mirrored into the app's database\n * on deploy, entitlements, per-tenant and platform flag values and SQL `feature_on()`,\n * for the feature gate. Needs the tenant features block.\n *\n * @stability Beta\n */\nexport const featureMirrorBlock = defineBlock({\n id: \"feature-mirror\",\n requires: [\"tenant-features\"],\n contributes: {\n migrations: [\n {\n package: PACKAGE,\n file: \"migrations/0008_genesis_feature_registry.sql\",\n test: \"migrations/tests/0008_genesis_feature_registry.test.sql\",\n },\n {\n package: PACKAGE,\n file: \"migrations/0014_genesis_feature_registry_hardening.sql\",\n test: \"migrations/tests/0014_genesis_feature_registry_hardening.test.sql\",\n },\n { package: PACKAGE, file: \"migrations/0016_genesis_viewer_people.sql\", test: \"migrations/tests/0016_genesis_viewer_people.test.sql\" },\n {\n package: PACKAGE,\n file: \"migrations/0017_genesis_person_entitlements.sql\",\n test: \"migrations/tests/0017_genesis_person_entitlements.test.sql\",\n },\n ],\n privacy: {\n notes: [\n \"Stores which tenants and which people (the app's person ids) hold which paid features, and flag values.\",\n ],\n },\n },\n});\n\n/**\n * The jobs block: a background-job queue and named schedules that call the\n * app's job route. Server only; the route is `createJobsHandler` from `/jobs`.\n *\n * @stability Beta\n */\nexport const jobsBlock = defineBlock({\n id: \"jobs\",\n platforms: [\"web\"],\n contributes: {\n migrations: [{ package: PACKAGE, file: \"migrations/0007_genesis_jobs.sql\", test: \"migrations/tests/0007_genesis_jobs.test.sql\" }],\n privacy: { notes: [\"Job payloads hold ids, never content or secrets; finished jobs are deleted after 14 days.\"] },\n },\n});\n\n/**\n * The web handoff block: continue on the web, already signed in. The routes are\n * `createWebHandoff` from `/web-handoff/server`, the page uses `/web-handoff`, and the native\n * app calls `openOnWeb` from genesis-native.\n *\n * @stability Beta\n */\nexport const webHandoffBlock = defineBlock({\n id: \"web-handoff\",\n platforms: [\"web\", \"native\"],\n contributes: {\n migrations: [\n { package: PACKAGE, file: \"migrations/0015_genesis_web_handoff.sql\", test: \"migrations/tests/0015_genesis_web_handoff.test.sql\" },\n ],\n privacy: {\n notes: [\n \"Logs each handoff (who, which page, when) for 30 days; keeps a SHA-256 digest of each link's token, never the token or a sign-in token.\",\n ],\n },\n },\n});\n\n\n/**\n * The product events block: the app's own events table and its tracking functions (ADR 0010).\n * The client is `createEventTracker` from `/events`; on native, `createEventsSink` plugs it into the\n * observability sinks.\n *\n * @stability Beta\n */\nexport const eventsBlock = defineBlock({\n id: \"events\",\n platforms: [\"web\", \"native\"],\n contributes: {\n migrations: [{ package: PACKAGE, file: \"migrations/0018_genesis_events.sql\", test: \"migrations/tests/0018_genesis_events.test.sql\" }],\n privacy: {\n notes: [\n \"Stores product events: the login, the person record and tenant, the feature, an event name, small properties (labels and counts, never typed content or personal data), the flags that were on, the platform and the time. Only the service role reads them; delete old ones with genesis_prune_events (default 400 days).\",\n ],\n },\n },\n});\n\n/**\n * The versioned-writes block: `genesis_version_bump`, the trigger that advances a row's version on\n * every update, so the Supabase data provider's `version` rule can refuse an edit made on a stale\n * copy as a `conflict`.\n *\n * @stability Beta\n */\nexport const versionedWritesBlock = defineBlock({\n id: \"versioned-writes\",\n platforms: [\"web\", \"native\"],\n contributes: {\n migrations: [\n { package: PACKAGE, file: \"migrations/0019_genesis_version_bump.sql\", test: \"migrations/tests/0019_genesis_version_bump.test.sql\" },\n ],\n },\n});\n"]}