返回 CodeWhale
20261117000114_registry_channel_publication.sql
根目录 / web / supabase / migrations / 20261117000114_registry_channel_publication.sql
1 alter table public.facts_release
2 drop constraint if exists facts_release_payload_b64_check;
3
4 alter table public.facts_release
5 add constraint facts_release_payload_b64_check check (
6 octet_length(payload_b64) between 4 and 11184812
7 and octet_length(payload_b64) % 4 = 0
8 and payload_b64 ~ '^[A-Za-z0-9+/]+={0,2}$'
9 and (
10 case
11 when payload ->> 'schema' = 'codewhale.registry.v1' then octet_length(decode(payload_b64, 'base64')) <= 8388608
12 else octet_length(payload_b64) <= 699052 and octet_length(decode(payload_b64, 'base64')) <= 524288
13 end
14 )
15 );
16
17 alter table public.facts_release
18 drop constraint if exists facts_release_payload_meta_check;
19
20 alter table public.facts_release
21 add constraint facts_release_payload_meta_check check (
22 case
23 when payload ->> 'schema' = 'codewhale.registry.v1' then (
24 schema_version = 1
25 and payload -> 'revision' = to_jsonb(facts_version)
26 and (payload -> 'applies_to' is null or jsonb_typeof(payload -> 'applies_to') = 'string')
27 and coalesce(payload ->> 'applies_to', '*') = applies_to
28 and payload ->> 'generated_at' ~ '^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,3})?Z$'
29 and (payload ->> 'generated_at')::timestamptz = published_at
30 and jsonb_typeof(payload -> 'providers') = 'array'
31 and jsonb_typeof(payload -> 'models') = 'array'
32 and jsonb_typeof(payload -> 'offerings') = 'array'
33 and jsonb_typeof(payload -> 'dialects') = 'object'
34 and case
35 when not_after is null then payload ->> 'not_after' is null
36 else payload ->> 'not_after' ~ '^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,3})?Z$'
37 and (payload ->> 'not_after')::timestamptz = not_after
38 end
39 ) is true
40 else (
41 payload -> 'facts_version' = to_jsonb(facts_version)
42 and payload -> 'schema_version' = to_jsonb(schema_version)
43 and payload -> 'applies_to' = to_jsonb(applies_to)
44 and payload ->> 'published_at' ~ '^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,3})?Z$'
45 and (payload ->> 'published_at')::timestamptz = published_at
46 and case
47 when not_after is null then payload ->> 'not_after' is null
48 else payload ->> 'not_after' ~ '^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,3})?Z$'
49 and (payload ->> 'not_after')::timestamptz = not_after
50 end
51 ) is true
52 end
53 );
54
55 create or replace function public.facts_release_guard()
56 returns trigger
57 language plpgsql
58 security invoker
59 set search_path = ''
60 as $facts_release_guard$
61 declare
62 channel_row public.facts_channel%rowtype;
63 extra_sig jsonb;
64 begin
65 if tg_op = 'UPDATE' then
66 if new.id is distinct from old.id
67 or new.channel_id is distinct from old.channel_id
68 or new.facts_version is distinct from old.facts_version
69 or new.schema_version is distinct from old.schema_version
70 or new.envelope_version is distinct from old.envelope_version
71 or new.applies_to is distinct from old.applies_to
72 or new.key_id is distinct from old.key_id
73 or new.payload_b64 is distinct from old.payload_b64
74 or new.sig_b64 is distinct from old.sig_b64
75 or new.sigs is distinct from old.sigs
76 or new.payload is distinct from old.payload
77 or new.published_at is distinct from old.published_at
78 or new.not_after is distinct from old.not_after then
79 raise exception 'signed columns of facts_release % are immutable; publish a higher facts_version instead', old.id
80 using errcode = 'restrict_violation';
81 end if;
82 if old.status = 'revoked' and new.status is distinct from 'revoked' then
83 raise exception 'facts_release % cannot be un-revoked; publish a higher facts_version instead', old.id
84 using errcode = 'restrict_violation';
85 end if;
86 return new;
87 end if;
88
89 select * into channel_row from public.facts_channel where id = new.channel_id for update;
90 if not found then
91 raise exception 'facts_channel % does not exist', new.channel_id
92 using errcode = 'foreign_key_violation';
93 end if;
94
95 if channel_row.slug = 'registry' then
96 if new.payload ->> 'schema' is distinct from 'codewhale.registry.v1' then
97 raise exception 'registry channel requires schema codewhale.registry.v1, got %',
98 coalesce(new.payload ->> 'schema', '<missing>')
99 using errcode = 'check_violation';
100 end if;
101 else
102 if new.payload ->> 'schema' is not null or new.payload -> 'channel' is distinct from to_jsonb(channel_row.slug) then
103 raise exception 'signed payload channel % does not match channel %',
104 coalesce(new.payload ->> 'channel', '<missing>'), channel_row.slug
105 using errcode = 'check_violation';
106 end if;
107 end if;
108
109 if new.facts_version <= channel_row.max_facts_version then
110 raise exception 'facts_version % is not above the published high-water mark % for channel %',
111 new.facts_version, channel_row.max_facts_version, channel_row.slug
112 using errcode = 'unique_violation';
113 end if;
114
115 for extra_sig in select value from jsonb_array_elements(new.sigs) loop
116 if jsonb_typeof(extra_sig) <> 'object'
117 or coalesce(extra_sig ->> 'key_id', '') !~ '^cwf-[a-z0-9-]{1,32}$'
118 or coalesce(extra_sig ->> 'sig_b64', '') !~ '^[A-Za-z0-9+/]{86}==$' then
119 raise exception 'extra signature % is not a well-formed {key_id, sig_b64} pair', extra_sig
120 using errcode = 'check_violation';
121 end if;
122 end loop;
123
124 update public.facts_channel set max_facts_version = new.facts_version where id = new.channel_id;
125 return new;
126 end;
127 $facts_release_guard$;
128
129 notify pgrst, 'reload schema';
130
130 lines SQL