-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSCHEMA.sql
More file actions
3306 lines (2230 loc) · 102 KB
/
Copy pathSCHEMA.sql
File metadata and controls
3306 lines (2230 loc) · 102 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--
-- PostgreSQL database dump
--
\restrict ITpRAwCBQB1Eujdumlb2lXtfTcabr3ewsacbHizs9jNwJBpKN4XyV8oROCASLy3
-- Dumped from database version 16.14 (Debian 16.14-1.pgdg12+1)
-- Dumped by pg_dump version 16.15 (Ubuntu 16.15-0ubuntu0.24.04.1)
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
--
-- Name: public; Type: SCHEMA; Schema: -; Owner: -
--
CREATE SCHEMA public;
--
-- Name: SCHEMA public; Type: COMMENT; Schema: -; Owner: -
--
COMMENT ON SCHEMA public IS 'standard public schema';
--
-- Name: events_notify(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.events_notify() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
PERFORM pg_notify('overslash_events', NEW.id::text);
RETURN NULL;
END;
$$;
SET default_tablespace = '';
SET default_table_access_method = heap;
--
-- Name: api_keys; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.api_keys (
id uuid DEFAULT gen_random_uuid() NOT NULL,
org_id uuid NOT NULL,
identity_id uuid NOT NULL,
name text NOT NULL,
key_hash text NOT NULL,
key_prefix text NOT NULL,
scopes text[] DEFAULT '{}'::text[] NOT NULL,
expires_at timestamp with time zone,
last_used_at timestamp with time zone,
revoked_at timestamp with time zone,
created_at timestamp with time zone DEFAULT now() NOT NULL,
revoked_reason text
);
--
-- Name: approvals; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.approvals (
id uuid DEFAULT gen_random_uuid() NOT NULL,
org_id uuid NOT NULL,
identity_id uuid NOT NULL,
action_summary text NOT NULL,
action_detail jsonb,
permission_keys text[] DEFAULT '{}'::text[] NOT NULL,
status text DEFAULT 'pending'::text NOT NULL,
resolved_at timestamp with time zone,
resolved_by text,
remember boolean DEFAULT false NOT NULL,
token text NOT NULL,
expires_at timestamp with time zone NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
current_resolver_identity_id uuid NOT NULL,
resolver_assigned_at timestamp with time zone DEFAULT now() NOT NULL,
disclosed_fields jsonb,
replay_payload jsonb,
tags text[] DEFAULT '{}'::text[] NOT NULL,
execution_mode text DEFAULT 'sync'::text NOT NULL,
CONSTRAINT approvals_execution_mode_check CHECK ((execution_mode = ANY (ARRAY['sync'::text, 'async'::text, 'hybrid'::text]))),
CONSTRAINT approvals_status_check CHECK ((status = ANY (ARRAY['pending'::text, 'allowed'::text, 'denied'::text, 'expired'::text])))
);
--
-- Name: COLUMN approvals.tags; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN public.approvals.tags IS 'System-derived `namespace:value` metadata tags describing the gated call (sql:*, table:*, service:*, host:*, risk:*). Never caller-supplied.';
--
-- Name: audit_log; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.audit_log (
id uuid DEFAULT gen_random_uuid() NOT NULL,
org_id uuid NOT NULL,
identity_id uuid,
action text NOT NULL,
resource_type text,
resource_id uuid,
detail jsonb DEFAULT '{}'::jsonb NOT NULL,
ip_address text,
created_at timestamp with time zone DEFAULT now() NOT NULL,
description text,
impersonated_by_identity_id uuid,
tags text[] DEFAULT '{}'::text[] NOT NULL,
actor_name text,
owner_user_name text
);
--
-- Name: COLUMN audit_log.tags; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN public.audit_log.tags IS 'System-derived `namespace:value` metadata tags. Searchable via GET /v1/audit?tag=. Populated on action/approval events; other events carry an empty array.';
--
-- Name: COLUMN audit_log.actor_name; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN public.audit_log.actor_name IS 'Name of identity_id as of write time. Historical by design (D59) — the row records the name the actor had when they acted, not their current one.';
--
-- Name: COLUMN audit_log.owner_user_name; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN public.audit_log.owner_user_name IS 'Name of the owning user (identities.owner_id, a flattened pointer to the root user) as of write time, or the actor''s own name when the actor is a user. Historical by design (D59).';
--
-- Name: billing_email_log; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.billing_email_log (
id uuid DEFAULT gen_random_uuid() NOT NULL,
stripe_event_id text NOT NULL,
kind text NOT NULL,
user_id uuid NOT NULL,
attempted_at timestamp with time zone DEFAULT now() NOT NULL,
sent_at timestamp with time zone,
CONSTRAINT billing_email_log_kind_check CHECK ((kind = ANY (ARRAY['invoice_paid'::text, 'invoice_payment_failed'::text, 'subscription_canceled'::text])))
);
--
-- Name: byoc_credentials; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.byoc_credentials (
id uuid DEFAULT gen_random_uuid() NOT NULL,
org_id uuid NOT NULL,
identity_id uuid NOT NULL,
provider_key text NOT NULL,
encrypted_client_id bytea NOT NULL,
encrypted_client_secret bytea NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
metadata jsonb DEFAULT '{}'::jsonb NOT NULL
);
--
-- Name: COLUMN byoc_credentials.metadata; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN public.byoc_credentials.metadata IS 'Opaque caller-supplied key/value claim (provenance tag). Echoed verbatim; cleared/rewritten whenever the encrypted client pair is replaced.';
--
-- Name: call_results; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.call_results (
id uuid DEFAULT gen_random_uuid() NOT NULL,
org_id uuid NOT NULL,
identity_id uuid NOT NULL,
service_key text,
action_key text,
body_ciphertext bytea NOT NULL,
status_code integer NOT NULL,
content_type text,
body_bytes bigint NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
expires_at timestamp with time zone NOT NULL
);
--
-- Name: TABLE call_results; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON TABLE public.call_results IS 'Full ActionResult of a call whose compact rendering was truncated, stored so the same bytes can be delivered again without re-running upstream. Written by POST /v1/actions/call when verbose=false truncated; read via GET /v1/downloads/{token}.';
--
-- Name: COLUMN call_results.body_ciphertext; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN public.call_results.body_ciphertext IS 'AES-256-GCM [version|nonce|ct+tag] over the serialized ActionResult. Response headers live inside the blob, which is why the whole thing is encrypted.';
--
-- Name: COLUMN call_results.body_bytes; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN public.call_results.body_bytes IS 'Plaintext size, for the download Descriptor. Bounded by call_result_max_bytes.';
--
-- Name: connections; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.connections (
id uuid DEFAULT gen_random_uuid() NOT NULL,
org_id uuid NOT NULL,
identity_id uuid NOT NULL,
provider_key text NOT NULL,
encrypted_access_token bytea NOT NULL,
encrypted_refresh_token bytea,
token_expires_at timestamp with time zone,
scopes text[],
account_email text,
byoc_credential_id uuid,
is_default boolean DEFAULT true NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
keep boolean DEFAULT false NOT NULL,
reauth_required boolean DEFAULT false NOT NULL,
account_picture text
);
--
-- Name: COLUMN connections.keep; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN public.connections.keep IS 'When true, this connection is never auto-deleted by service deletion, even when no service references it.';
--
-- Name: COLUMN connections.reauth_required; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN public.connections.reauth_required IS 'When true, the connection must be re-authorized before use (e.g. its pinned BYOC client was replaced). Cleared when fresh tokens are written.';
--
-- Name: download_tokens; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.download_tokens (
id uuid DEFAULT gen_random_uuid() NOT NULL,
token_hash bytea NOT NULL,
org_id uuid NOT NULL,
identity_id uuid NOT NULL,
service_instance_id uuid,
service_key text,
action_key text,
request jsonb,
credential_ref jsonb DEFAULT '{}'::jsonb NOT NULL,
mime text,
size_bytes bigint,
filename text,
created_at timestamp with time zone DEFAULT now() NOT NULL,
expires_at timestamp with time zone NOT NULL,
last_used_at timestamp with time zone,
use_count integer DEFAULT 0 NOT NULL,
call_result_id uuid,
CONSTRAINT download_tokens_one_byte_source CHECK ((num_nonnulls(request, call_result_id) = 1))
);
--
-- Name: TABLE download_tokens; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON TABLE public.download_tokens IS 'Capability tokens for deferred (out-of-band) byte delivery. Minted by POST /v1/actions/call with deliver:"url", redeemed by GET /v1/downloads/{token}.';
--
-- Name: COLUMN download_tokens.token_hash; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN public.download_tokens.token_hash IS 'sha256 of the raw token; the raw value exists only in the minted URL.';
--
-- Name: COLUMN download_tokens.request; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN public.download_tokens.request IS 'Replayable upstream request {method,url,headers,body}. Names secrets rather than carrying them; inline credential headers are rejected at mint time.';
--
-- Name: COLUMN download_tokens.credential_ref; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN public.download_tokens.credential_ref IS 'How to re-resolve the upstream credential at fetch time. Never the credential itself.';
--
-- Name: COLUMN download_tokens.call_result_id; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN public.download_tokens.call_result_id IS 'When set, redemption serves these stored bytes instead of replaying `request`. Mutually exclusive with `request` (download_tokens_one_byte_source).';
--
-- Name: email_unsubscribe_tokens; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.email_unsubscribe_tokens (
token uuid DEFAULT gen_random_uuid() NOT NULL,
user_id uuid NOT NULL,
org_id uuid NOT NULL,
purpose text DEFAULT 'welcome'::text NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
redeemed_at timestamp with time zone,
CONSTRAINT email_unsubscribe_tokens_purpose_check CHECK ((purpose = ANY (ARRAY['welcome'::text, 'webhook_digest'::text])))
);
--
-- Name: enabled_global_templates; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.enabled_global_templates (
org_id uuid NOT NULL,
template_key text NOT NULL,
enabled_by uuid,
created_at timestamp with time zone DEFAULT now() NOT NULL
);
--
-- Name: events; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.events (
id bigint NOT NULL,
event_id uuid DEFAULT gen_random_uuid() NOT NULL,
org_id uuid NOT NULL,
type text NOT NULL,
topic text NOT NULL,
payload jsonb NOT NULL,
audience uuid[] DEFAULT '{}'::uuid[] NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL
);
--
-- Name: TABLE events; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON TABLE public.events IS 'Durable event log for the SSE stream (GET /v1/events/stream). Also the fan-out substrate: inserts pg_notify the row id on the overslash_events channel and each replica fetches + forwards to its subscribers.';
--
-- Name: COLUMN events.id; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN public.events.id IS 'Resume cursor. Sent to clients as the SSE id: field and returned by them as Last-Event-ID.';
--
-- Name: COLUMN events.event_id; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN public.events.event_id IS 'Stable uuid for the wire envelope, mirroring the webhook envelope id.';
--
-- Name: COLUMN events.audience; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN public.events.audience IS 'Identity ids permitted to receive this event, frozen at emit time. Org admins bypass this check. Empty means admins-only.';
--
-- Name: events_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE public.events_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: events_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE public.events_id_seq OWNED BY public.events.id;
--
-- Name: executions; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.executions (
id uuid DEFAULT gen_random_uuid() NOT NULL,
approval_id uuid,
org_id uuid NOT NULL,
status text NOT NULL,
remember boolean DEFAULT false NOT NULL,
remember_keys text[],
remember_rule_ttl timestamp with time zone,
result jsonb,
error text,
triggered_by text,
started_at timestamp with time zone,
completed_at timestamp with time zone,
expires_at timestamp with time zone NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
result_viewed_at timestamp with time zone,
tags text[] DEFAULT '{}'::text[] NOT NULL,
identity_id uuid NOT NULL,
request jsonb,
service_key text,
service_instance_id uuid,
lease_expires_at timestamp with time zone,
worker_id text,
attempts integer DEFAULT 0 NOT NULL,
cancel_requested boolean DEFAULT false NOT NULL,
render_verbose boolean,
template_key text,
description text,
client_ip text,
CONSTRAINT executions_attempts_nonneg CHECK ((attempts >= 0)),
CONSTRAINT executions_has_origin CHECK (((approval_id IS NOT NULL) OR (request IS NOT NULL))),
CONSTRAINT executions_status_check CHECK ((status = ANY (ARRAY['pending'::text, 'executing'::text, 'executed'::text, 'failed'::text, 'cancelled'::text, 'expired'::text])))
);
--
-- Name: COLUMN executions.tags; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN public.executions.tags IS 'Copied verbatim from the originating approval at insert time — an execution can never disagree with what its approver saw.';
--
-- Name: COLUMN executions.request; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN public.executions.request IS 'Credential-free stored call payload, same shape as approvals.replay_payload. NOT NULL marks this row as worker-run (async).';
--
-- Name: COLUMN executions.lease_expires_at; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN public.executions.lease_expires_at IS 'While status=executing, the instant after which the claiming worker is presumed dead and the row may be reclaimed. Renewed by heartbeat.';
--
-- Name: COLUMN executions.attempts; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN public.executions.attempts IS 'Attempts that ended by losing their lease. Incremented only by the reclaim sweep, never by the claim.';
--
-- Name: COLUMN executions.cancel_requested; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN public.executions.cancel_requested IS 'Cooperative cancel. Stops Overslash waiting on the upstream; does not cancel the upstream operation itself.';
--
-- Name: group_grants; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.group_grants (
id uuid DEFAULT gen_random_uuid() NOT NULL,
group_id uuid NOT NULL,
service_instance_id uuid NOT NULL,
access_level text NOT NULL,
auto_approve_reads boolean DEFAULT false NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
auto_approve_level text DEFAULT 'none'::text NOT NULL,
CONSTRAINT group_grants_access_level_check CHECK ((access_level = ANY (ARRAY['read'::text, 'write'::text, 'admin'::text]))),
CONSTRAINT group_grants_auto_approve_level_valid CHECK ((auto_approve_level = ANY (ARRAY['none'::text, 'read'::text, 'write'::text, 'admin'::text]))),
CONSTRAINT group_grants_auto_approve_within_ceiling CHECK (((auto_approve_level = 'none'::text) OR (access_level = 'admin'::text) OR ((access_level = 'write'::text) AND (auto_approve_level = ANY (ARRAY['read'::text, 'write'::text]))) OR ((access_level = 'read'::text) AND (auto_approve_level = 'read'::text))))
);
--
-- Name: COLUMN group_grants.auto_approve_reads; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN public.group_grants.auto_approve_reads IS 'DEPRECATED — derived mirror of (auto_approve_level <> ''none''). Read auto_approve_level instead; this column is dropped once the API alias is removed.';
--
-- Name: COLUMN group_grants.auto_approve_level; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN public.group_grants.auto_approve_level IS 'How far up the read<write<admin ladder actions skip Layer 2 (no permission rule, no approval). ''none'' = always require approval. Bounded by access_level.';
--
-- Name: groups; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.groups (
id uuid DEFAULT gen_random_uuid() NOT NULL,
org_id uuid NOT NULL,
name text NOT NULL,
description text DEFAULT ''::text NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
is_system boolean DEFAULT false NOT NULL,
system_kind text,
owner_identity_id uuid,
CONSTRAINT groups_owner_only_for_self CHECK (((system_kind = 'self'::text) = (owner_identity_id IS NOT NULL))),
CONSTRAINT groups_system_kind_check CHECK ((system_kind = ANY (ARRAY['everyone'::text, 'admins'::text, 'self'::text])))
);
--
-- Name: identities; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.identities (
id uuid DEFAULT gen_random_uuid() NOT NULL,
org_id uuid NOT NULL,
name text NOT NULL,
kind text NOT NULL,
external_id text,
metadata jsonb DEFAULT '{}'::jsonb NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
email text,
parent_id uuid,
depth integer DEFAULT 0 NOT NULL,
owner_id uuid,
inherit_permissions boolean DEFAULT false NOT NULL,
last_active_at timestamp with time zone DEFAULT now() NOT NULL,
archived_at timestamp with time zone,
archived_reason text,
preferences jsonb DEFAULT '{}'::jsonb NOT NULL,
is_org_admin boolean DEFAULT false NOT NULL,
user_id uuid,
auto_call_on_approve boolean DEFAULT true NOT NULL,
CONSTRAINT identities_is_org_admin_only_user CHECK (((kind = 'user'::text) OR (is_org_admin = false))),
CONSTRAINT identities_kind_check CHECK ((kind = ANY (ARRAY['user'::text, 'agent'::text, 'sub_agent'::text])))
);
--
-- Name: identity_groups; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.identity_groups (
identity_id uuid NOT NULL,
group_id uuid NOT NULL,
assigned_at timestamp with time zone DEFAULT now() NOT NULL
);
--
-- Name: magic_link_tokens; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.magic_link_tokens (
id uuid DEFAULT gen_random_uuid() NOT NULL,
token_hash bytea NOT NULL,
email text NOT NULL,
next_path text,
created_at timestamp with time zone DEFAULT now() NOT NULL,
expires_at timestamp with time zone NOT NULL,
redeemed_at timestamp with time zone
);
--
-- Name: mcp_client_agent_bindings; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.mcp_client_agent_bindings (
id uuid DEFAULT gen_random_uuid() NOT NULL,
org_id uuid NOT NULL,
user_identity_id uuid NOT NULL,
client_id text NOT NULL,
agent_identity_id uuid NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
elicitation_enabled boolean DEFAULT false NOT NULL,
self_approve_enabled boolean DEFAULT false NOT NULL
);
--
-- Name: mcp_refresh_tokens; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.mcp_refresh_tokens (
id uuid DEFAULT gen_random_uuid() NOT NULL,
client_id text NOT NULL,
identity_id uuid NOT NULL,
org_id uuid NOT NULL,
hash bytea NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
expires_at timestamp with time zone NOT NULL,
revoked_at timestamp with time zone,
replaced_by_id uuid
);
--
-- Name: mcp_upstream_connections; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.mcp_upstream_connections (
id uuid DEFAULT gen_random_uuid() NOT NULL,
identity_id uuid NOT NULL,
org_id uuid NOT NULL,
upstream_resource text NOT NULL,
upstream_client_id text NOT NULL,
status text DEFAULT 'pending_auth'::text NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
last_refreshed_at timestamp with time zone,
CONSTRAINT mcp_upstream_connections_status_check CHECK ((status = ANY (ARRAY['pending_auth'::text, 'ready'::text, 'revoked'::text, 'error'::text])))
);
--
-- Name: mcp_upstream_flows; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.mcp_upstream_flows (
id text NOT NULL,
identity_id uuid NOT NULL,
org_id uuid NOT NULL,
upstream_resource text NOT NULL,
upstream_client_id text NOT NULL,
upstream_as_issuer text NOT NULL,
upstream_token_endpoint text NOT NULL,
upstream_authorize_url text NOT NULL,
pkce_code_verifier text NOT NULL,
expires_at timestamp with time zone NOT NULL,
consumed_at timestamp with time zone,
created_at timestamp with time zone DEFAULT now() NOT NULL,
created_ip text,
created_user_agent text
);
--
-- Name: mcp_upstream_tokens; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.mcp_upstream_tokens (
id uuid DEFAULT gen_random_uuid() NOT NULL,
connection_id uuid NOT NULL,
access_token_ciphertext bytea NOT NULL,
refresh_token_ciphertext bytea,
access_token_expires_at timestamp with time zone,
scope text,
created_at timestamp with time zone DEFAULT now() NOT NULL,
superseded_at timestamp with time zone
);
--
-- Name: media_descriptors; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.media_descriptors (
id uuid DEFAULT gen_random_uuid() NOT NULL,
org_id uuid NOT NULL,
service_instance_id uuid,
service_key text,
media_path text NOT NULL,
sha256 text,
mime text,
size_bytes bigint,
filename text,
source text NOT NULL,
first_seen_at timestamp with time zone DEFAULT now() NOT NULL,
last_seen_at timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT media_descriptors_source_check CHECK ((source = ANY (ARRAY['download'::text, 'upload'::text])))
);
--
-- Name: TABLE media_descriptors; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON TABLE public.media_descriptors IS 'What the gateway recorded about bytes it moved, so an approval that references them can describe them instead of showing a bare content hash. Best-effort: bytes that never passed through the gateway are simply absent.';
--
-- Name: oauth_connection_flows; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.oauth_connection_flows (
id text NOT NULL,
org_id uuid NOT NULL,
identity_id uuid NOT NULL,
actor_identity_id uuid NOT NULL,
provider_key text NOT NULL,
byoc_credential_id uuid,
scopes text[] DEFAULT '{}'::text[] NOT NULL,
pkce_code_verifier text,
upstream_authorize_url text NOT NULL,
expires_at timestamp with time zone NOT NULL,
consumed_at timestamp with time zone,
created_at timestamp with time zone DEFAULT now() NOT NULL,
created_ip text,
created_user_agent text,
return_url text,
upgrade_connection_id uuid,
service_instance_id uuid,
pin_service_instance_ids uuid[] DEFAULT '{}'::uuid[] NOT NULL
);
--
-- Name: oauth_handoff_codes; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.oauth_handoff_codes (
code text NOT NULL,
jwt text NOT NULL,
origin text NOT NULL,
next_path text,
created_at timestamp with time zone DEFAULT now() NOT NULL,
expires_at timestamp with time zone NOT NULL,
consumed_at timestamp with time zone
);
--
-- Name: oauth_mcp_clients; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.oauth_mcp_clients (
id uuid DEFAULT gen_random_uuid() NOT NULL,
client_id text NOT NULL,
client_name text,
redirect_uris text[] NOT NULL,
software_id text,
software_version text,
created_at timestamp with time zone DEFAULT now() NOT NULL,
last_seen_at timestamp with time zone,
created_ip text,
created_user_agent text,
is_revoked boolean DEFAULT false NOT NULL,
capabilities jsonb,
client_info jsonb,
protocol_version text,
last_session_id uuid,
org_id uuid
);
--
-- Name: COLUMN oauth_mcp_clients.org_id; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN public.oauth_mcp_clients.org_id IS 'Org this DCR client is locked to (stamped from the subdomain at registration). NULL = root/multi-org: usable on any subdomain, absent from any org''s admin MCP-Clients list.';
--
-- Name: oauth_preview_origins; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.oauth_preview_origins (
preview_id uuid NOT NULL,
origin text NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
expires_at timestamp with time zone NOT NULL,
nonce text DEFAULT ''::text NOT NULL,
pkce_verifier text,
org_slug text,
next_path text
);
--
-- Name: oauth_providers; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.oauth_providers (
key text NOT NULL,
display_name text NOT NULL,
authorization_endpoint text NOT NULL,
token_endpoint text NOT NULL,
revocation_endpoint text,
userinfo_endpoint text,
client_id_pattern text,
supports_pkce boolean DEFAULT false NOT NULL,
supports_refresh boolean DEFAULT true NOT NULL,
extra_auth_params jsonb DEFAULT '{}'::jsonb NOT NULL,
is_builtin boolean DEFAULT true NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
token_auth_method text DEFAULT 'client_secret_post'::text NOT NULL,
issuer_url text,
jwks_uri text,
default_identity_scopes text[] DEFAULT '{}'::text[] NOT NULL,
login_hint_param text
);
--
-- Name: org_idp_configs; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.org_idp_configs (
id uuid DEFAULT gen_random_uuid() NOT NULL,
org_id uuid NOT NULL,
provider_key text NOT NULL,
encrypted_client_id bytea,
encrypted_client_secret bytea,
enabled boolean DEFAULT true NOT NULL,
allowed_email_domains text[] DEFAULT '{}'::text[] NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
is_default boolean DEFAULT false NOT NULL,
CONSTRAINT org_idp_configs_creds_both_or_neither CHECK ((((encrypted_client_id IS NULL) AND (encrypted_client_secret IS NULL)) OR ((encrypted_client_id IS NOT NULL) AND (encrypted_client_secret IS NOT NULL))))
);
--
-- Name: org_subscriptions; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.org_subscriptions (
org_id uuid NOT NULL,
stripe_subscription_id text NOT NULL,
stripe_customer_id text NOT NULL,
plan text DEFAULT 'team'::text NOT NULL,
seats integer DEFAULT 2 NOT NULL,
status text NOT NULL,
currency text NOT NULL,
current_period_start timestamp with time zone,
current_period_end timestamp with time zone,
cancel_at_period_end boolean DEFAULT false NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL
);
--
-- Name: orgs; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.orgs (
id uuid DEFAULT gen_random_uuid() NOT NULL,
name text NOT NULL,
slug text NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
subagent_idle_timeout_secs integer DEFAULT 14400 NOT NULL,
subagent_archive_retention_days integer DEFAULT 30 NOT NULL,
approval_auto_bubble_secs integer DEFAULT 300 NOT NULL,
global_templates_enabled boolean DEFAULT true NOT NULL,
allow_unsigned_secret_provide boolean DEFAULT true NOT NULL,
is_personal boolean DEFAULT false NOT NULL,
plan text DEFAULT 'standard'::text NOT NULL,
default_deferred_execution boolean DEFAULT false NOT NULL,
allow_overslash_managed_signin boolean DEFAULT false NOT NULL,
creator_user_id uuid,
audit_response_body_mode text DEFAULT 'off'::text NOT NULL,
headless boolean DEFAULT false NOT NULL,
require_invite_admission boolean DEFAULT true NOT NULL,
managed_signin_allowed_domains text[] DEFAULT '{}'::text[] NOT NULL,
trial_ends_at timestamp with time zone,
allow_services_outside_catalog boolean DEFAULT false NOT NULL,
user_template_policy text DEFAULT 'none'::text NOT NULL,
call_timeout_ms integer,
max_call_timeout_ms integer,
CONSTRAINT orgs_approval_auto_bubble_secs_check CHECK ((approval_auto_bubble_secs >= 0)),
CONSTRAINT orgs_audit_response_body_mode_check CHECK ((audit_response_body_mode = ANY (ARRAY['off'::text, 'errors_only'::text, 'all'::text]))),
CONSTRAINT orgs_call_timeout_bounds CHECK ((((call_timeout_ms IS NULL) OR ((call_timeout_ms >= 1000) AND (call_timeout_ms <= 600000))) AND ((max_call_timeout_ms IS NULL) OR ((max_call_timeout_ms >= 1000) AND (max_call_timeout_ms <= 600000))) AND ((call_timeout_ms IS NULL) OR (max_call_timeout_ms IS NULL) OR (call_timeout_ms <= max_call_timeout_ms)))),
CONSTRAINT orgs_plan_check CHECK ((plan = ANY (ARRAY['standard'::text, 'free_unlimited'::text, 'trial'::text]))),
CONSTRAINT orgs_user_template_policy_check CHECK ((user_template_policy = ANY (ARRAY['none'::text, 'restrictive'::text, 'full'::text])))
);
--
-- Name: COLUMN orgs.allow_services_outside_catalog; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN public.orgs.allow_services_outside_catalog IS 'When false (default), non-admins cannot instantiate global templates outside the curated catalog.';
--
-- Name: COLUMN orgs.user_template_policy; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN public.orgs.user_template_policy IS 'Whether org members may create user-namespace layers: none | restrictive (reserved) | full.';
--
-- Name: COLUMN orgs.call_timeout_ms; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN public.orgs.call_timeout_ms IS 'Default upstream timeout in ms for action calls in this org. NULL inherits the deployment default (CALL_TIMEOUT_MS). Overridden per template action and per call.';
--
-- Name: COLUMN orgs.max_call_timeout_ms; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN public.orgs.max_call_timeout_ms IS 'Ceiling on any resolved call timeout in this org, in ms. NULL inherits CALL_TIMEOUT_MAX_MS. A caller asking for more is rejected; a template or org default above it is clamped.';
--
-- Name: pending_checkouts; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.pending_checkouts (
id text NOT NULL,
user_id uuid NOT NULL,
org_name text NOT NULL,
org_slug text NOT NULL,
seats integer NOT NULL,
currency text NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
expires_at timestamp with time zone DEFAULT (now() + '02:00:00'::interval) NOT NULL,
fulfilled_org_id uuid
);
--
-- Name: pending_mcp_elicitations; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.pending_mcp_elicitations (
elicit_id text NOT NULL,
session_id uuid NOT NULL,
agent_identity_id uuid NOT NULL,
approval_id uuid NOT NULL,
status text DEFAULT 'pending'::text NOT NULL,
final_response jsonb,
created_at timestamp with time zone DEFAULT now() NOT NULL,
completed_at timestamp with time zone
);
--
-- Name: permission_rules; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.permission_rules (
id uuid DEFAULT gen_random_uuid() NOT NULL,
org_id uuid NOT NULL,
identity_id uuid NOT NULL,
action_pattern text NOT NULL,
effect text DEFAULT 'allow'::text NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
expires_at timestamp with time zone,
CONSTRAINT permission_rules_effect_check CHECK ((effect = ANY (ARRAY['allow'::text, 'deny'::text])))
);
--
-- Name: rate_limits; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.rate_limits (
id uuid DEFAULT gen_random_uuid() NOT NULL,
org_id uuid NOT NULL,
identity_id uuid,
group_id uuid,
scope text NOT NULL,
max_requests integer DEFAULT 1000 NOT NULL,
window_seconds integer DEFAULT 60 NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT rate_limits_scope_check CHECK ((scope = ANY (ARRAY['org'::text, 'group'::text, 'user'::text, 'identity_cap'::text])))
);