@marktiderman/genesis-supabase 0.15.1 → 0.16.1
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.
- package/README.md +61 -4
- package/dist/blocks.d.ts +17 -9
- package/dist/blocks.js +1 -1
- package/dist/{chunk-XESMEWXI.js → chunk-HZYI5FJR.js} +18 -4
- package/dist/chunk-HZYI5FJR.js.map +1 -0
- package/dist/{index-CKloeGaz.d.ts → index-C2xP-igM.d.ts} +128 -1
- package/dist/index.d.ts +4 -3
- package/dist/index.js +56 -2
- package/dist/index.js.map +1 -1
- package/dist/native.d.ts +4 -3
- package/functions/genesis-support-cleanup/handler.ts +30 -1
- package/functions/genesis-support-cleanup/index.ts +13 -2
- package/functions/genesis-support-intake/contract.ts +47 -6
- package/functions/genesis-support-notify/handler.ts +42 -18
- package/functions/genesis-support-notify/index.ts +2 -1
- package/migrations/0020_genesis_settings.sql +470 -0
- package/migrations/0021_genesis_settings_integer.sql +101 -0
- package/migrations/tests/0020_genesis_settings.test.sql +388 -0
- package/migrations/tests/0021_genesis_settings_integer.test.sql +21 -0
- package/package.json +41 -2
- package/dist/chunk-XESMEWXI.js.map +0 -1
|
@@ -0,0 +1,470 @@
|
|
|
1
|
+
-- Genesis settings: public.genesis_settings, its history, the app manifest, and the RPCs
|
|
2
|
+
-- genesis_settings_layers() and set_genesis_setting()
|
|
3
|
+
--
|
|
4
|
+
-- The database layer of Genesis settings (@marktiderman/genesis-config, `resolveSettings`): the
|
|
5
|
+
-- runtime settings a platform admin changes live, on top of the package defaults, the preset and
|
|
6
|
+
-- genesis.config.json `settings`. Every change is kept (who, when, the old and new value, and from
|
|
7
|
+
-- where) in public.genesis_setting_changes. A key with no row keeps the value from the layers
|
|
8
|
+
-- below it.
|
|
9
|
+
--
|
|
10
|
+
-- The resolver is the TypeScript one, so the server, the web and phones agree: this file stores
|
|
11
|
+
-- the inputs and returns them (genesis_settings_layers), it doesn't re-implement the layers.
|
|
12
|
+
--
|
|
13
|
+
-- genesis_app_manifest one row: what this app runs. Its packages and versions, the preset, the
|
|
14
|
+
-- file's settings, and the settings catalogue (each key's type, bounds,
|
|
15
|
+
-- scope and whether it applies at runtime), written by the service role on
|
|
16
|
+
-- deploy and at app start (`writeAppManifest` in genesis-supabase).
|
|
17
|
+
-- genesis_settings the database layer: a value per key, optionally locked so tenant and
|
|
18
|
+
-- person values can't change it.
|
|
19
|
+
--
|
|
20
|
+
-- The app hook: `private.genesis_platform_admin(person uuid) returns boolean` decides who may
|
|
21
|
+
-- change settings: the app's platform admins (ADR 0003's `platform` zone). Without it nobody can
|
|
22
|
+
-- (set_genesis_setting raises 42501 with a hint), so a fresh install fails closed, as
|
|
23
|
+
-- tenant_features (0004) does. It runs with this file's (the owner's) rights:
|
|
24
|
+
--
|
|
25
|
+
-- create function private.genesis_platform_admin(person uuid) returns boolean
|
|
26
|
+
-- language sql stable set search_path = '' as $$
|
|
27
|
+
-- select exists (select 1 from public.platform_admins a where a.user_id = person)
|
|
28
|
+
-- $$;
|
|
29
|
+
-- revoke execute on function private.genesis_platform_admin(uuid) from public, anon, authenticated;
|
|
30
|
+
--
|
|
31
|
+
-- What set_genesis_setting accepts is checked against the catalogue in the manifest: the key must
|
|
32
|
+
-- be declared and apply at runtime, not be locked by genesis.config.json, and the value must fit
|
|
33
|
+
-- its type and bounds (JSON null only for a key whose default is null). Without a synced catalogue
|
|
34
|
+
-- it refuses every write (22023, with a hint), so nothing unchecked gets in.
|
|
35
|
+
--
|
|
36
|
+
-- Secrets are never settings: a key names the secret it needs, the value lives in its own store
|
|
37
|
+
-- (Vault, Edge Function secrets, the server's environment, CI). Nothing here holds one.
|
|
38
|
+
--
|
|
39
|
+
-- Access: everything goes through the functions below (security definer). The tables have row
|
|
40
|
+
-- level security on, no policies and no grants to anon or authenticated. The service role (the
|
|
41
|
+
-- deploy step, a server, the CLI) may write the settings and the manifest directly; its setting
|
|
42
|
+
-- writes are recorded too, with no person and the source it names in `genesis.setting_source`
|
|
43
|
+
-- (`set_config('genesis.setting_source', 'cli', true)`), else `server`. Its direct writes are not
|
|
44
|
+
-- checked against the catalogue: the resolver reports and skips a bad one. The history is never
|
|
45
|
+
-- edited; the service role prunes rows older than a year with genesis_prune_setting_changes.
|
|
46
|
+
--
|
|
47
|
+
-- Use: copy this file into your app's `supabase/migrations/` under a new timestamped name (or let
|
|
48
|
+
-- `genesis add settings` do it). Never edit it once applied; change it with a new migration. It
|
|
49
|
+
-- stands alone.
|
|
50
|
+
--
|
|
51
|
+
-- Verify: the pgTAP test beside this file (`tests/0020_genesis_settings.test.sql`).
|
|
52
|
+
|
|
53
|
+
create schema if not exists private;
|
|
54
|
+
|
|
55
|
+
-- The manifest: one row, written by the service role.
|
|
56
|
+
create table public.genesis_app_manifest (
|
|
57
|
+
app_id text primary key
|
|
58
|
+
constraint genesis_app_manifest_app_id_format check (app_id ~ '^[a-z0-9][a-z0-9._-]{0,99}$'),
|
|
59
|
+
-- Exactly one row: every row has `one = true`, and it is unique.
|
|
60
|
+
one boolean not null default true unique constraint genesis_app_manifest_one_row check (one),
|
|
61
|
+
name text constraint genesis_app_manifest_name_length check (name is null or char_length(name) <= 200),
|
|
62
|
+
-- { "@marktiderman/genesis-support": "0.11.0", … } from genesis.lock.json
|
|
63
|
+
packages jsonb not null default '{}'::jsonb constraint genesis_app_manifest_packages_object check (jsonb_typeof(packages) = 'object'),
|
|
64
|
+
preset text not null default 'max' constraint genesis_app_manifest_preset check (preset in ('max', 'standard', 'min')),
|
|
65
|
+
config_hash text,
|
|
66
|
+
-- genesis.config.json `settings`, as written (a value, or { "value": …, "locked": true }).
|
|
67
|
+
file_settings jsonb not null default '{}'::jsonb constraint genesis_app_manifest_file_settings_object check (jsonb_typeof(file_settings) = 'object'),
|
|
68
|
+
-- { "support.dumpRetentionDays": { "type": "number", "applies": "runtime", "scope": "app", "min": 7, "max": 365 }, … }
|
|
69
|
+
catalogue jsonb not null default '{}'::jsonb constraint genesis_app_manifest_catalogue_object check (jsonb_typeof(catalogue) = 'object'),
|
|
70
|
+
reported_at timestamptz not null default now()
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
comment on table public.genesis_app_manifest is
|
|
74
|
+
'One row: the Genesis packages this app runs, its preset, its file settings and the settings catalogue. Written by the service role on deploy and at app start. Genesis settings.';
|
|
75
|
+
|
|
76
|
+
create table public.genesis_settings (
|
|
77
|
+
key text primary key
|
|
78
|
+
constraint genesis_settings_key_format check (key ~ '^[a-z][a-zA-Z0-9]*(-[a-z0-9]+)*\.[a-z][a-zA-Z0-9]*$' and char_length(key) <= 200),
|
|
79
|
+
value jsonb not null,
|
|
80
|
+
locked boolean not null default false,
|
|
81
|
+
updated_by uuid references auth.users (id) on delete set null,
|
|
82
|
+
updated_at timestamptz not null default now()
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
comment on table public.genesis_settings is
|
|
86
|
+
'The database layer of Genesis settings: a runtime value per key, locked or not. Written through set_genesis_setting or by the service role. Genesis settings.';
|
|
87
|
+
|
|
88
|
+
-- Every change, kept. A row is never edited.
|
|
89
|
+
create table public.genesis_setting_changes (
|
|
90
|
+
id bigint generated always as identity primary key,
|
|
91
|
+
key text not null,
|
|
92
|
+
-- null on the old side when the row was created, on the new side when it was deleted.
|
|
93
|
+
old_value jsonb,
|
|
94
|
+
new_value jsonb,
|
|
95
|
+
old_locked boolean,
|
|
96
|
+
new_locked boolean,
|
|
97
|
+
changed_by uuid references auth.users (id) on delete set null,
|
|
98
|
+
changed_role text not null,
|
|
99
|
+
source text not null
|
|
100
|
+
constraint genesis_setting_changes_source check (source in ('admin', 'wizard', 'cli', 'central', 'server', 'database')),
|
|
101
|
+
changed_at timestamptz not null default now()
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
create index genesis_setting_changes_key on public.genesis_setting_changes (key, changed_at desc);
|
|
105
|
+
|
|
106
|
+
comment on table public.genesis_setting_changes is
|
|
107
|
+
'History of public.genesis_settings: who changed which key, when, from where, and the old and new value. Written only by its trigger. Genesis settings.';
|
|
108
|
+
|
|
109
|
+
-- The hook, asked with the signed-in person. Missing hook, nobody signed in, or a null answer: false.
|
|
110
|
+
create or replace function private.genesis_is_platform_admin()
|
|
111
|
+
returns boolean
|
|
112
|
+
language plpgsql
|
|
113
|
+
stable
|
|
114
|
+
security definer
|
|
115
|
+
set search_path = ''
|
|
116
|
+
as $$
|
|
117
|
+
declare
|
|
118
|
+
me uuid := auth.uid();
|
|
119
|
+
begin
|
|
120
|
+
if me is null or to_regprocedure('private.genesis_platform_admin(uuid)') is null then
|
|
121
|
+
return false;
|
|
122
|
+
end if;
|
|
123
|
+
return coalesce(private.genesis_platform_admin(me), false);
|
|
124
|
+
end;
|
|
125
|
+
$$;
|
|
126
|
+
|
|
127
|
+
-- Server code, told apart from a person by the request's role (set per request by PostgREST), never
|
|
128
|
+
-- by the JWT's claims: no role switch (a direct connection, a migration) or a role that bypasses
|
|
129
|
+
-- row security (service_role, postgres). authenticated and anon never bypass it.
|
|
130
|
+
create or replace function private.genesis_settings_caller_is_server()
|
|
131
|
+
returns boolean
|
|
132
|
+
language plpgsql
|
|
133
|
+
stable
|
|
134
|
+
security definer
|
|
135
|
+
set search_path = ''
|
|
136
|
+
as $$
|
|
137
|
+
declare
|
|
138
|
+
request_role text := coalesce(nullif(current_setting('role', true), ''), 'none');
|
|
139
|
+
begin
|
|
140
|
+
return request_role = 'none'
|
|
141
|
+
or coalesce((select r.rolbypassrls from pg_catalog.pg_roles r where r.rolname = request_role), false);
|
|
142
|
+
end;
|
|
143
|
+
$$;
|
|
144
|
+
|
|
145
|
+
-- Why a value can't be stored for a key, by the manifest's catalogue; null when it can. A catalogue
|
|
146
|
+
-- entry that isn't the shape catalogueManifest writes refuses every value for its key.
|
|
147
|
+
create or replace function private.genesis_setting_problem(setting_key text, setting_value jsonb)
|
|
148
|
+
returns text
|
|
149
|
+
language plpgsql
|
|
150
|
+
stable
|
|
151
|
+
security definer
|
|
152
|
+
set search_path = ''
|
|
153
|
+
as $$
|
|
154
|
+
declare
|
|
155
|
+
cat jsonb;
|
|
156
|
+
def jsonb;
|
|
157
|
+
kind text;
|
|
158
|
+
given text := coalesce(jsonb_typeof(setting_value), 'null');
|
|
159
|
+
n numeric;
|
|
160
|
+
file_entry jsonb;
|
|
161
|
+
begin
|
|
162
|
+
if setting_key is null or setting_key !~ '^[a-z][a-zA-Z0-9]*(-[a-z0-9]+)*\.[a-z][a-zA-Z0-9]*$' then
|
|
163
|
+
return format('%s isn''t a setting key (namespace.key)', coalesce(setting_key, 'null'));
|
|
164
|
+
end if;
|
|
165
|
+
select m.catalogue, m.file_settings -> setting_key into cat, file_entry from public.genesis_app_manifest m limit 1;
|
|
166
|
+
if cat is null or cat = '{}'::jsonb then
|
|
167
|
+
return 'no settings catalogue has been synced yet (write the manifest on deploy: writeAppManifest)';
|
|
168
|
+
end if;
|
|
169
|
+
def := cat -> setting_key;
|
|
170
|
+
if def is null or jsonb_typeof(def) <> 'object' then
|
|
171
|
+
return format('%s isn''t a setting any installed package declares', setting_key);
|
|
172
|
+
end if;
|
|
173
|
+
kind := def ->> 'type';
|
|
174
|
+
if kind is null or kind not in ('boolean', 'number', 'string', 'list', 'object')
|
|
175
|
+
or (def ? 'min' and jsonb_typeof(def -> 'min') <> 'number')
|
|
176
|
+
or (def ? 'max' and jsonb_typeof(def -> 'max') <> 'number')
|
|
177
|
+
or (def ? 'options' and jsonb_typeof(def -> 'options') <> 'array')
|
|
178
|
+
or coalesce(def ->> 'applies', 'runtime') not in ('build', 'runtime') then
|
|
179
|
+
return format('the synced catalogue''s entry for %s is malformed (write the manifest again: writeAppManifest)', setting_key);
|
|
180
|
+
end if;
|
|
181
|
+
if def ->> 'applies' = 'build' then
|
|
182
|
+
return format('%s applies at build time: change it in genesis.config.json', setting_key);
|
|
183
|
+
end if;
|
|
184
|
+
-- A value genesis.config.json locks: no later layer may change it, so don't store one.
|
|
185
|
+
if jsonb_typeof(file_entry) = 'object' and (select count(*) from jsonb_object_keys(file_entry)) = 2
|
|
186
|
+
and file_entry ? 'value' and file_entry -> 'locked' = 'true'::jsonb then
|
|
187
|
+
return format('%s is locked by genesis.config.json: change it there', setting_key);
|
|
188
|
+
end if;
|
|
189
|
+
-- JSON null is "not set": only for a key whose default is null.
|
|
190
|
+
if given = 'null' then
|
|
191
|
+
if def -> 'nullable' = 'true'::jsonb then
|
|
192
|
+
return null;
|
|
193
|
+
end if;
|
|
194
|
+
return format('%s can''t be null: it has a default, so set a value', setting_key);
|
|
195
|
+
end if;
|
|
196
|
+
if kind = 'boolean' and given <> 'boolean'
|
|
197
|
+
or kind = 'number' and given <> 'number'
|
|
198
|
+
or kind = 'string' and given <> 'string'
|
|
199
|
+
or kind = 'list' and given <> 'array'
|
|
200
|
+
or kind = 'object' and given <> 'object' then
|
|
201
|
+
return format('%s must be a %s', setting_key, kind);
|
|
202
|
+
end if;
|
|
203
|
+
if kind = 'list' and exists (select 1 from jsonb_array_elements(setting_value) e where jsonb_typeof(e) not in ('string', 'number')) then
|
|
204
|
+
return format('%s must be a list of strings or numbers', setting_key);
|
|
205
|
+
end if;
|
|
206
|
+
if kind in ('number', 'list') then
|
|
207
|
+
n := case when kind = 'number' then setting_value::text::numeric else jsonb_array_length(setting_value) end;
|
|
208
|
+
if def ? 'min' and n < (def ->> 'min')::numeric then
|
|
209
|
+
return format('%s must be at least %s%s', setting_key, def ->> 'min', case when kind = 'list' then ' items' else '' end);
|
|
210
|
+
end if;
|
|
211
|
+
if def ? 'max' and n > (def ->> 'max')::numeric then
|
|
212
|
+
return format('%s must be at most %s%s', setting_key, def ->> 'max', case when kind = 'list' then ' items' else '' end);
|
|
213
|
+
end if;
|
|
214
|
+
end if;
|
|
215
|
+
if def ? 'options' and kind in ('string', 'number') and not (def -> 'options') @> jsonb_build_array(setting_value) then
|
|
216
|
+
return format('%s must be one of %s', setting_key, def -> 'options');
|
|
217
|
+
end if;
|
|
218
|
+
if def ? 'options' and kind = 'list' and exists (
|
|
219
|
+
select 1 from jsonb_array_elements(setting_value) e where not (def -> 'options') @> jsonb_build_array(e)
|
|
220
|
+
) then
|
|
221
|
+
return format('%s may only hold %s', setting_key, def -> 'options');
|
|
222
|
+
end if;
|
|
223
|
+
return null;
|
|
224
|
+
end;
|
|
225
|
+
$$;
|
|
226
|
+
|
|
227
|
+
-- `value` is JSON: JSON null ("not set") arrives from PostgREST as SQL NULL, so a missing value is
|
|
228
|
+
-- read as JSON null, and checked like any other.
|
|
229
|
+
create or replace function public.set_genesis_setting(key text, value jsonb, locked boolean default false)
|
|
230
|
+
returns void
|
|
231
|
+
language plpgsql
|
|
232
|
+
security definer
|
|
233
|
+
set search_path = ''
|
|
234
|
+
as $$
|
|
235
|
+
declare
|
|
236
|
+
me uuid := auth.uid();
|
|
237
|
+
stored jsonb := coalesce(set_genesis_setting.value, 'null'::jsonb);
|
|
238
|
+
problem text;
|
|
239
|
+
begin
|
|
240
|
+
if me is null then
|
|
241
|
+
raise exception 'set_genesis_setting: not signed in' using errcode = '42501';
|
|
242
|
+
end if;
|
|
243
|
+
if to_regprocedure('private.genesis_platform_admin(uuid)') is null then
|
|
244
|
+
raise exception 'set_genesis_setting: nobody may change settings yet'
|
|
245
|
+
using errcode = '42501', hint = 'Create private.genesis_platform_admin(uuid) (see migration 0020_genesis_settings).';
|
|
246
|
+
end if;
|
|
247
|
+
if not private.genesis_is_platform_admin() then
|
|
248
|
+
raise exception 'set_genesis_setting: only platform admins can change settings' using errcode = '42501';
|
|
249
|
+
end if;
|
|
250
|
+
if key is null or locked is null then
|
|
251
|
+
raise exception 'set_genesis_setting: key and locked are required' using errcode = '22004';
|
|
252
|
+
end if;
|
|
253
|
+
problem := private.genesis_setting_problem(key, stored);
|
|
254
|
+
if problem is not null then
|
|
255
|
+
raise exception 'set_genesis_setting: %', problem using errcode = '22023';
|
|
256
|
+
end if;
|
|
257
|
+
perform set_config('genesis.setting_source', 'admin', true);
|
|
258
|
+
insert into public.genesis_settings as s (key, value, locked, updated_by, updated_at)
|
|
259
|
+
values (set_genesis_setting.key, stored, set_genesis_setting.locked, me, now())
|
|
260
|
+
on conflict on constraint genesis_settings_pkey do update
|
|
261
|
+
set value = excluded.value, locked = excluded.locked, updated_by = excluded.updated_by, updated_at = excluded.updated_at
|
|
262
|
+
-- Saving the same value and lock again changes nothing, not even who and when.
|
|
263
|
+
where (s.value, s.locked) is distinct from (excluded.value, excluded.locked);
|
|
264
|
+
-- The source names this write only, not later ones in the same transaction.
|
|
265
|
+
perform set_config('genesis.setting_source', '', true);
|
|
266
|
+
end;
|
|
267
|
+
$$;
|
|
268
|
+
|
|
269
|
+
comment on function public.set_genesis_setting(text, jsonb, boolean) is
|
|
270
|
+
'Sets a runtime setting in the database layer, optionally locked. Only platform admins (private.genesis_platform_admin); checked against the manifest''s catalogue, and refused for a key genesis.config.json locks. Genesis settings.';
|
|
271
|
+
|
|
272
|
+
-- Back to the layers below: the database row goes.
|
|
273
|
+
create or replace function public.clear_genesis_setting(key text)
|
|
274
|
+
returns void
|
|
275
|
+
language plpgsql
|
|
276
|
+
security definer
|
|
277
|
+
set search_path = ''
|
|
278
|
+
as $$
|
|
279
|
+
begin
|
|
280
|
+
if auth.uid() is null then
|
|
281
|
+
raise exception 'clear_genesis_setting: not signed in' using errcode = '42501';
|
|
282
|
+
end if;
|
|
283
|
+
if not private.genesis_is_platform_admin() then
|
|
284
|
+
raise exception 'clear_genesis_setting: only platform admins can change settings'
|
|
285
|
+
using errcode = '42501', hint = 'Create private.genesis_platform_admin(uuid) (see migration 0020_genesis_settings).';
|
|
286
|
+
end if;
|
|
287
|
+
perform set_config('genesis.setting_source', 'admin', true);
|
|
288
|
+
delete from public.genesis_settings s where s.key = clear_genesis_setting.key;
|
|
289
|
+
perform set_config('genesis.setting_source', '', true);
|
|
290
|
+
end;
|
|
291
|
+
$$;
|
|
292
|
+
|
|
293
|
+
comment on function public.clear_genesis_setting(text) is
|
|
294
|
+
'Removes a setting from the database layer, so the file, preset or default applies again. Only platform admins. Genesis settings.';
|
|
295
|
+
|
|
296
|
+
-- The layers a client resolves with resolveSettings (it is not a resolved snapshot: the one resolver
|
|
297
|
+
-- is the TypeScript one): the preset, the file's settings for every key the catalogue declares
|
|
298
|
+
-- (build-time keys included: they are committed config, never secrets), and the database layer for
|
|
299
|
+
-- runtime keys. For any signed-in person, and for server code.
|
|
300
|
+
create or replace function public.genesis_settings_layers()
|
|
301
|
+
returns jsonb
|
|
302
|
+
language plpgsql
|
|
303
|
+
stable
|
|
304
|
+
security definer
|
|
305
|
+
set search_path = ''
|
|
306
|
+
as $$
|
|
307
|
+
declare
|
|
308
|
+
m public.genesis_app_manifest;
|
|
309
|
+
declared text[];
|
|
310
|
+
runtime text[];
|
|
311
|
+
begin
|
|
312
|
+
if auth.uid() is null and not private.genesis_settings_caller_is_server() then
|
|
313
|
+
raise exception 'genesis_settings_layers: not signed in' using errcode = '42501';
|
|
314
|
+
end if;
|
|
315
|
+
select * into m from public.genesis_app_manifest limit 1;
|
|
316
|
+
select coalesce(array_agg(c.key), '{}'),
|
|
317
|
+
coalesce(array_agg(c.key) filter (where coalesce(c.value ->> 'applies', 'runtime') = 'runtime'), '{}')
|
|
318
|
+
into declared, runtime
|
|
319
|
+
from jsonb_each(coalesce(m.catalogue, '{}'::jsonb)) c
|
|
320
|
+
where jsonb_typeof(c.value) = 'object';
|
|
321
|
+
return jsonb_build_object(
|
|
322
|
+
'preset', coalesce(m.preset, 'max'),
|
|
323
|
+
'file', coalesce((select jsonb_object_agg(f.key, f.value) from jsonb_each(coalesce(m.file_settings, '{}'::jsonb)) f where f.key = any (declared)), '{}'::jsonb),
|
|
324
|
+
'database', coalesce((
|
|
325
|
+
select jsonb_object_agg(s.key, jsonb_build_object('value', s.value, 'locked', s.locked))
|
|
326
|
+
from public.genesis_settings s where s.key = any (runtime)
|
|
327
|
+
), '{}'::jsonb),
|
|
328
|
+
'reportedAt', m.reported_at
|
|
329
|
+
);
|
|
330
|
+
end;
|
|
331
|
+
$$;
|
|
332
|
+
|
|
333
|
+
comment on function public.genesis_settings_layers() is
|
|
334
|
+
'The preset, the file settings and the database layer, for any signed-in person or server code to resolve with resolveSettings. Genesis settings.';
|
|
335
|
+
|
|
336
|
+
-- The database layer's history, newest first, at most `max_rows` (1 to 1000). For platform admins.
|
|
337
|
+
create or replace function public.genesis_setting_history(max_rows integer default 100)
|
|
338
|
+
returns table (key text, old_value jsonb, new_value jsonb, old_locked boolean, new_locked boolean, changed_by uuid, changed_role text, source text, changed_at timestamptz)
|
|
339
|
+
language plpgsql
|
|
340
|
+
stable
|
|
341
|
+
security definer
|
|
342
|
+
set search_path = ''
|
|
343
|
+
as $$
|
|
344
|
+
begin
|
|
345
|
+
if not private.genesis_is_platform_admin() then
|
|
346
|
+
raise exception 'genesis_setting_history: only platform admins can read the history' using errcode = '42501';
|
|
347
|
+
end if;
|
|
348
|
+
return query
|
|
349
|
+
select c.key, c.old_value, c.new_value, c.old_locked, c.new_locked, c.changed_by, c.changed_role, c.source, c.changed_at
|
|
350
|
+
from public.genesis_setting_changes c
|
|
351
|
+
order by c.changed_at desc, c.id desc
|
|
352
|
+
limit greatest(1, least(coalesce(max_rows, 100), 1000));
|
|
353
|
+
end;
|
|
354
|
+
$$;
|
|
355
|
+
|
|
356
|
+
comment on function public.genesis_setting_history(integer) is
|
|
357
|
+
'Who changed which setting, when, from where, and from what to what, newest first. For platform admins. Genesis settings.';
|
|
358
|
+
|
|
359
|
+
-- The history is evidence: nobody edits it, and the only way to remove rows is this, for the
|
|
360
|
+
-- service role, and never the last year (365 days at least, the default).
|
|
361
|
+
create or replace function public.genesis_prune_setting_changes(older_than interval default interval '365 days')
|
|
362
|
+
returns integer
|
|
363
|
+
language plpgsql
|
|
364
|
+
security definer
|
|
365
|
+
set search_path = ''
|
|
366
|
+
as $$
|
|
367
|
+
declare
|
|
368
|
+
removed integer;
|
|
369
|
+
begin
|
|
370
|
+
if older_than is null or older_than < interval '365 days' then
|
|
371
|
+
raise exception 'genesis_prune_setting_changes: keep at least 365 days of history' using errcode = '22023';
|
|
372
|
+
end if;
|
|
373
|
+
delete from public.genesis_setting_changes c where c.changed_at < now() - older_than;
|
|
374
|
+
get diagnostics removed = row_count;
|
|
375
|
+
return removed;
|
|
376
|
+
end;
|
|
377
|
+
$$;
|
|
378
|
+
|
|
379
|
+
comment on function public.genesis_prune_setting_changes(interval) is
|
|
380
|
+
'Deletes setting history older than `older_than` (365 days at least). For the service role. Genesis settings.';
|
|
381
|
+
|
|
382
|
+
create or replace function private.genesis_record_setting_change()
|
|
383
|
+
returns trigger
|
|
384
|
+
language plpgsql
|
|
385
|
+
security definer
|
|
386
|
+
set search_path = ''
|
|
387
|
+
as $$
|
|
388
|
+
declare
|
|
389
|
+
claims jsonb := coalesce(nullif(current_setting('request.jwt.claims', true), '')::jsonb, '{}'::jsonb);
|
|
390
|
+
who_role text := coalesce(nullif(claims ->> 'role', ''), 'database');
|
|
391
|
+
named text := nullif(current_setting('genesis.setting_source', true), '');
|
|
392
|
+
src text;
|
|
393
|
+
begin
|
|
394
|
+
-- Only a change of the value or the lock is history: this skips the `on delete set null` of
|
|
395
|
+
-- updated_by when a person is deleted, which would otherwise record them and refuse their deletion.
|
|
396
|
+
if tg_op = 'UPDATE' and new.key = old.key and new.value is not distinct from old.value and new.locked is not distinct from old.locked then
|
|
397
|
+
return null;
|
|
398
|
+
end if;
|
|
399
|
+
src := case
|
|
400
|
+
when named in ('admin', 'wizard', 'cli', 'central', 'server', 'database') then named
|
|
401
|
+
when who_role = 'service_role' then 'server'
|
|
402
|
+
else 'database'
|
|
403
|
+
end;
|
|
404
|
+
-- A renamed key is two changes: the old key goes, the new one comes.
|
|
405
|
+
if tg_op = 'UPDATE' and new.key <> old.key then
|
|
406
|
+
insert into public.genesis_setting_changes (key, old_value, new_value, old_locked, new_locked, changed_by, changed_role, source)
|
|
407
|
+
values (old.key, old.value, null, old.locked, null, (select u.id from auth.users u where u.id = auth.uid()), who_role, src);
|
|
408
|
+
insert into public.genesis_setting_changes (key, old_value, new_value, old_locked, new_locked, changed_by, changed_role, source)
|
|
409
|
+
values (new.key, null, new.value, null, new.locked, (select u.id from auth.users u where u.id = auth.uid()), who_role, src);
|
|
410
|
+
return null;
|
|
411
|
+
end if;
|
|
412
|
+
insert into public.genesis_setting_changes (key, old_value, new_value, old_locked, new_locked, changed_by, changed_role, source)
|
|
413
|
+
values (
|
|
414
|
+
coalesce(new.key, old.key),
|
|
415
|
+
case when tg_op = 'INSERT' then null else old.value end,
|
|
416
|
+
case when tg_op = 'DELETE' then null else new.value end,
|
|
417
|
+
case when tg_op = 'INSERT' then null else old.locked end,
|
|
418
|
+
case when tg_op = 'DELETE' then null else new.locked end,
|
|
419
|
+
-- Only a login that still exists: one being deleted in this statement is no one.
|
|
420
|
+
(select u.id from auth.users u where u.id = auth.uid()),
|
|
421
|
+
who_role,
|
|
422
|
+
src
|
|
423
|
+
);
|
|
424
|
+
return null;
|
|
425
|
+
end;
|
|
426
|
+
$$;
|
|
427
|
+
|
|
428
|
+
create trigger genesis_record_change
|
|
429
|
+
after insert or update or delete on public.genesis_settings
|
|
430
|
+
for each row execute function private.genesis_record_setting_change();
|
|
431
|
+
|
|
432
|
+
-- Row level security on, with no policies: anon and authenticated reach the tables only through
|
|
433
|
+
-- the functions above, even if a later grant lets them try.
|
|
434
|
+
alter table public.genesis_app_manifest enable row level security;
|
|
435
|
+
alter table public.genesis_settings enable row level security;
|
|
436
|
+
alter table public.genesis_setting_changes enable row level security;
|
|
437
|
+
|
|
438
|
+
-- Privileges by name. Supabase's default privileges give anon and authenticated everything on a
|
|
439
|
+
-- new public table and function, and Postgres gives every function to public: take them back,
|
|
440
|
+
-- then grant what's needed.
|
|
441
|
+
revoke all on table public.genesis_app_manifest from public, anon, authenticated;
|
|
442
|
+
revoke all on table public.genesis_settings from public, anon, authenticated;
|
|
443
|
+
revoke all on table public.genesis_setting_changes from public, anon, authenticated;
|
|
444
|
+
revoke all on sequence public.genesis_setting_changes_id_seq from public, anon, authenticated;
|
|
445
|
+
-- The service role writes the manifest and the settings (no truncate: it would skip the history).
|
|
446
|
+
revoke all on table public.genesis_app_manifest from service_role;
|
|
447
|
+
grant select, insert, update, delete on table public.genesis_app_manifest to service_role;
|
|
448
|
+
revoke all on table public.genesis_settings from service_role;
|
|
449
|
+
grant select, insert, update, delete on table public.genesis_settings to service_role;
|
|
450
|
+
-- The history is the trigger's: the service role reads it, and prunes only old rows, through
|
|
451
|
+
-- genesis_prune_setting_changes.
|
|
452
|
+
revoke all on table public.genesis_setting_changes from service_role;
|
|
453
|
+
grant select on table public.genesis_setting_changes to service_role;
|
|
454
|
+
|
|
455
|
+
revoke execute on function public.set_genesis_setting(text, jsonb, boolean) from public, anon;
|
|
456
|
+
grant execute on function public.set_genesis_setting(text, jsonb, boolean) to authenticated;
|
|
457
|
+
revoke execute on function public.clear_genesis_setting(text) from public, anon;
|
|
458
|
+
grant execute on function public.clear_genesis_setting(text) to authenticated;
|
|
459
|
+
revoke execute on function public.genesis_settings_layers() from public, anon;
|
|
460
|
+
grant execute on function public.genesis_settings_layers() to authenticated, service_role;
|
|
461
|
+
revoke execute on function public.genesis_setting_history(integer) from public, anon;
|
|
462
|
+
grant execute on function public.genesis_setting_history(integer) to authenticated;
|
|
463
|
+
revoke execute on function public.genesis_prune_setting_changes(interval) from public, anon, authenticated;
|
|
464
|
+
grant execute on function public.genesis_prune_setting_changes(interval) to service_role;
|
|
465
|
+
|
|
466
|
+
-- Only the functions above call these.
|
|
467
|
+
revoke execute on function private.genesis_is_platform_admin() from public, anon, authenticated;
|
|
468
|
+
revoke execute on function private.genesis_settings_caller_is_server() from public, anon, authenticated;
|
|
469
|
+
revoke execute on function private.genesis_setting_problem(text, jsonb) from public, anon, authenticated;
|
|
470
|
+
revoke execute on function private.genesis_record_setting_change() from public, anon, authenticated;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
-- Genesis settings 0021: whole numbers (after 0020_genesis_settings.sql).
|
|
2
|
+
--
|
|
3
|
+
-- genesis-config's defineSettings takes `integer: true` on a number (a count, days, milliseconds),
|
|
4
|
+
-- and catalogueManifest writes it into the synced catalogue. private.genesis_setting_problem now
|
|
5
|
+
-- refuses a fraction for such a key, as resolveSettings does, so set_genesis_setting can't store a
|
|
6
|
+
-- value every reader would skip. A catalogue entry whose `integer` isn't `true` is malformed, like
|
|
7
|
+
-- any other bad entry. Nothing else changes.
|
|
8
|
+
--
|
|
9
|
+
-- Use: copy this file into your app's `supabase/migrations/` under a new timestamped name
|
|
10
|
+
-- (`genesis migrate` does it). Never edit it once applied; change it with a new migration.
|
|
11
|
+
--
|
|
12
|
+
-- Verify: the pgTAP test beside this file (`tests/0021_genesis_settings_integer.test.sql`).
|
|
13
|
+
|
|
14
|
+
create or replace function private.genesis_setting_problem(setting_key text, setting_value jsonb)
|
|
15
|
+
returns text
|
|
16
|
+
language plpgsql
|
|
17
|
+
stable
|
|
18
|
+
security definer
|
|
19
|
+
set search_path = ''
|
|
20
|
+
as $$
|
|
21
|
+
declare
|
|
22
|
+
cat jsonb;
|
|
23
|
+
def jsonb;
|
|
24
|
+
kind text;
|
|
25
|
+
given text := coalesce(jsonb_typeof(setting_value), 'null');
|
|
26
|
+
n numeric;
|
|
27
|
+
file_entry jsonb;
|
|
28
|
+
begin
|
|
29
|
+
if setting_key is null or setting_key !~ '^[a-z][a-zA-Z0-9]*(-[a-z0-9]+)*\.[a-z][a-zA-Z0-9]*$' then
|
|
30
|
+
return format('%s isn''t a setting key (namespace.key)', coalesce(setting_key, 'null'));
|
|
31
|
+
end if;
|
|
32
|
+
select m.catalogue, m.file_settings -> setting_key into cat, file_entry from public.genesis_app_manifest m limit 1;
|
|
33
|
+
if cat is null or cat = '{}'::jsonb then
|
|
34
|
+
return 'no settings catalogue has been synced yet (write the manifest on deploy: writeAppManifest)';
|
|
35
|
+
end if;
|
|
36
|
+
def := cat -> setting_key;
|
|
37
|
+
if def is null or jsonb_typeof(def) <> 'object' then
|
|
38
|
+
return format('%s isn''t a setting any installed package declares', setting_key);
|
|
39
|
+
end if;
|
|
40
|
+
kind := def ->> 'type';
|
|
41
|
+
if kind is null or kind not in ('boolean', 'number', 'string', 'list', 'object')
|
|
42
|
+
or (def ? 'min' and jsonb_typeof(def -> 'min') <> 'number')
|
|
43
|
+
or (def ? 'max' and jsonb_typeof(def -> 'max') <> 'number')
|
|
44
|
+
or (def ? 'integer' and def -> 'integer' <> 'true'::jsonb)
|
|
45
|
+
or (def ? 'options' and jsonb_typeof(def -> 'options') <> 'array')
|
|
46
|
+
or coalesce(def ->> 'applies', 'runtime') not in ('build', 'runtime') then
|
|
47
|
+
return format('the synced catalogue''s entry for %s is malformed (write the manifest again: writeAppManifest)', setting_key);
|
|
48
|
+
end if;
|
|
49
|
+
if def ->> 'applies' = 'build' then
|
|
50
|
+
return format('%s applies at build time: change it in genesis.config.json', setting_key);
|
|
51
|
+
end if;
|
|
52
|
+
-- A value genesis.config.json locks: no later layer may change it, so don't store one.
|
|
53
|
+
if jsonb_typeof(file_entry) = 'object' and (select count(*) from jsonb_object_keys(file_entry)) = 2
|
|
54
|
+
and file_entry ? 'value' and file_entry -> 'locked' = 'true'::jsonb then
|
|
55
|
+
return format('%s is locked by genesis.config.json: change it there', setting_key);
|
|
56
|
+
end if;
|
|
57
|
+
-- JSON null is "not set": only for a key whose default is null.
|
|
58
|
+
if given = 'null' then
|
|
59
|
+
if def -> 'nullable' = 'true'::jsonb then
|
|
60
|
+
return null;
|
|
61
|
+
end if;
|
|
62
|
+
return format('%s can''t be null: it has a default, so set a value', setting_key);
|
|
63
|
+
end if;
|
|
64
|
+
if kind = 'boolean' and given <> 'boolean'
|
|
65
|
+
or kind = 'number' and given <> 'number'
|
|
66
|
+
or kind = 'string' and given <> 'string'
|
|
67
|
+
or kind = 'list' and given <> 'array'
|
|
68
|
+
or kind = 'object' and given <> 'object' then
|
|
69
|
+
return format('%s must be a %s', setting_key, kind);
|
|
70
|
+
end if;
|
|
71
|
+
if kind = 'list' and exists (select 1 from jsonb_array_elements(setting_value) e where jsonb_typeof(e) not in ('string', 'number')) then
|
|
72
|
+
return format('%s must be a list of strings or numbers', setting_key);
|
|
73
|
+
end if;
|
|
74
|
+
-- A number declared whole (catalogueManifest's `integer: true`): no fraction.
|
|
75
|
+
if kind = 'number' and def -> 'integer' = 'true'::jsonb
|
|
76
|
+
and setting_value::text::numeric <> trunc(setting_value::text::numeric) then
|
|
77
|
+
return format('%s must be a whole number', setting_key);
|
|
78
|
+
end if;
|
|
79
|
+
if kind in ('number', 'list') then
|
|
80
|
+
n := case when kind = 'number' then setting_value::text::numeric else jsonb_array_length(setting_value) end;
|
|
81
|
+
if def ? 'min' and n < (def ->> 'min')::numeric then
|
|
82
|
+
return format('%s must be at least %s%s', setting_key, def ->> 'min', case when kind = 'list' then ' items' else '' end);
|
|
83
|
+
end if;
|
|
84
|
+
if def ? 'max' and n > (def ->> 'max')::numeric then
|
|
85
|
+
return format('%s must be at most %s%s', setting_key, def ->> 'max', case when kind = 'list' then ' items' else '' end);
|
|
86
|
+
end if;
|
|
87
|
+
end if;
|
|
88
|
+
if def ? 'options' and kind in ('string', 'number') and not (def -> 'options') @> jsonb_build_array(setting_value) then
|
|
89
|
+
return format('%s must be one of %s', setting_key, def -> 'options');
|
|
90
|
+
end if;
|
|
91
|
+
if def ? 'options' and kind = 'list' and exists (
|
|
92
|
+
select 1 from jsonb_array_elements(setting_value) e where not (def -> 'options') @> jsonb_build_array(e)
|
|
93
|
+
) then
|
|
94
|
+
return format('%s may only hold %s', setting_key, def -> 'options');
|
|
95
|
+
end if;
|
|
96
|
+
return null;
|
|
97
|
+
end;
|
|
98
|
+
$$;
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
revoke execute on function private.genesis_setting_problem(text, jsonb) from public, anon, authenticated;
|