Repository navigation
Expand file tree
/
Copy pathschema_dump.sql
More file actions
6922 lines (4850 loc) · 223 KB
/
Copy pathschema_dump.sql
File metadata and controls
6922 lines (4850 loc) · 223 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 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
-- Dumped from database version 17.8 (Debian 17.8-1.pgdg12+1)
-- Dumped by pg_dump version 17.8 (Debian 17.8-1.pgdg12+1)
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET transaction_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: pg_trgm; Type: EXTENSION; Schema: -; Owner: -
--
CREATE EXTENSION IF NOT EXISTS pg_trgm WITH SCHEMA public;
--
-- Name: EXTENSION pg_trgm; Type: COMMENT; Schema: -; Owner: -
--
COMMENT ON EXTENSION pg_trgm IS 'text similarity measurement and index searching based on trigrams';
--
-- Name: unaccent; Type: EXTENSION; Schema: -; Owner: -
--
CREATE EXTENSION IF NOT EXISTS unaccent WITH SCHEMA public;
--
-- Name: EXTENSION unaccent; Type: COMMENT; Schema: -; Owner: -
--
COMMENT ON EXTENSION unaccent IS 'text search dictionary that removes accents';
--
-- Name: vector; Type: EXTENSION; Schema: -; Owner: -
--
CREATE EXTENSION IF NOT EXISTS vector WITH SCHEMA public;
--
-- Name: EXTENSION vector; Type: COMMENT; Schema: -; Owner: -
--
COMMENT ON EXTENSION vector IS 'vector data type and ivfflat and hnsw access methods';
--
-- Name: bump_discord_sync_generation(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.bump_discord_sync_generation() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
NEW.generation := OLD.generation + 1;
RETURN NEW;
END;
$$;
--
-- Name: bump_permission_epoch(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.bump_permission_epoch() RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
new_version bigint;
BEGIN
-- Statement-level, so a bulk grant bumps once rather than per row.
UPDATE public.permission_epoch
SET version = version + 1, updated_at = now()
WHERE id = 1
RETURNING version INTO new_version;
-- Same contract as squid_domain_events: NOTIFY arrives only after commit and
-- is a latency hint. Watchers poll permission_epoch as the durable signal, so
-- a dropped notification costs latency rather than correctness.
--
-- The singleton row is created by the migration that installs this trigger,
-- so a missing one means someone deleted it. Skip the notify rather than
-- error: a permission write failing outright would be far worse than a stale
-- cache, and the watchers' wall-clock backstop still bounds the staleness.
IF new_version IS NOT NULL THEN
PERFORM pg_notify('squid_permissions', new_version::text);
END IF;
RETURN NULL;
END;
$$;
--
-- Name: delete_orphaned_build_vote_sessions_after_builds_delete(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.delete_orphaned_build_vote_sessions_after_builds_delete() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
DELETE FROM vote_sessions
WHERE id IN (
SELECT vote_sessions.id
FROM vote_sessions vs
LEFT JOIN build_vote_sessions bvs ON vs.id = bvs.vote_session_id
LEFT JOIN delete_log_vote_sessions dvs ON vs.id = dvs.vote_session_id
WHERE bvs.vote_session_id IS NULL AND dvs.vote_session_id IS NULL
);
RETURN NULL; -- Statement-level triggers do not use OLD or NEW
END;
$$;
--
-- Name: emit_domain_event(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.emit_domain_event() RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
target_type text;
target_kind text;
target_id bigint;
target_payload jsonb;
target_schema_version integer := 1;
BEGIN
-- Unlike discord_sync_queue, this log records transitions, so an UPDATE that
-- rewrites a column to the value it already held must not produce an event.
IF TG_TABLE_NAME = 'builds' THEN
IF TG_OP = 'INSERT' THEN
target_kind := 'build';
target_id := NEW.id;
target_type := 'build.submitted';
target_schema_version := 2;
target_payload := jsonb_build_object(
'status', NEW.submission_status,
'submitter_account_id', NEW.submitter_account_id,
'category', NEW.category
);
PERFORM public.publish_domain_event(
target_type, target_schema_version, target_kind, target_id, target_payload
);
RETURN NULL;
END IF;
IF OLD.submission_status IS NOT DISTINCT FROM NEW.submission_status THEN RETURN NULL; END IF;
target_kind := 'build';
target_id := NEW.id;
target_schema_version := 3;
IF NEW.submission_status = 1 THEN
target_type := 'build.confirmed';
ELSIF NEW.submission_status = 2 THEN
target_type := 'build.denied';
ELSE
RETURN NULL;
END IF;
target_payload := jsonb_build_object(
'previous_status', OLD.submission_status,
'status', NEW.submission_status,
'submitter_account_id', NEW.submitter_account_id,
'category', NEW.category,
'first_confirmation', NEW.submission_status = 1 AND NOT EXISTS (
SELECT 1
FROM public.domain_events
WHERE aggregate_kind = 'build'
AND aggregate_id = NEW.id
AND event_type = 'build.confirmed'
)
);
ELSE
IF OLD.status IS NOT DISTINCT FROM NEW.status OR NEW.status <> 'closed' THEN RETURN NULL; END IF;
target_kind := 'vote_session';
target_id := NEW.id;
target_type := 'vote_session.closed';
target_payload := jsonb_build_object('kind', NEW.kind, 'result', NEW.result);
END IF;
PERFORM public.publish_domain_event(
target_type, target_schema_version, target_kind, target_id, target_payload
);
RETURN NULL;
END;
$$;
--
-- Name: enqueue_build_search_projection(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.enqueue_build_search_projection() RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
target_build_id bigint;
target_action text := 'upsert';
target_kind text;
BEGIN
IF TG_TABLE_NAME = 'builds' THEN
target_build_id := CASE WHEN TG_OP = 'DELETE' THEN OLD.id ELSE NEW.id END;
target_kind := lower(CASE WHEN TG_OP = 'DELETE' THEN OLD.category ELSE NEW.category END);
IF TG_OP = 'DELETE' THEN
target_action := 'delete';
END IF;
ELSE
target_build_id := CASE WHEN TG_OP = 'DELETE' THEN OLD.build_id ELSE NEW.build_id END;
SELECT lower(category) INTO target_kind FROM public.builds WHERE id = target_build_id;
END IF;
INSERT INTO public.search_projection_queue (resource_kind, source_key, action, enqueued_at)
VALUES ('build', target_build_id::text, target_action, now())
ON CONFLICT (resource_kind, source_key) DO UPDATE
SET action = EXCLUDED.action,
enqueued_at = EXCLUDED.enqueued_at,
attempts = 0,
locked_at = NULL,
dead_at = NULL,
last_error = NULL;
IF target_kind IN ('door', 'extender') THEN
INSERT INTO public.record_recompute_queue
(scope_key, build_kind, build_id, reasons, enqueued_at)
VALUES (
target_kind,
target_kind,
CASE WHEN TG_TABLE_NAME = 'builds' AND TG_OP = 'DELETE' THEN NULL ELSE target_build_id END,
'["source_change"]'::jsonb,
now()
)
ON CONFLICT (scope_key) DO UPDATE
SET build_id = EXCLUDED.build_id,
reasons = (
SELECT jsonb_agg(DISTINCT reason)
FROM jsonb_array_elements_text(
record_recompute_queue.reasons || EXCLUDED.reasons
) AS reason
),
enqueued_at = EXCLUDED.enqueued_at,
attempts = 0,
locked_at = NULL,
last_error = NULL;
END IF;
RETURN NULL;
END;
$$;
--
-- Name: enqueue_computed_record_search_projection(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.enqueue_computed_record_search_projection() RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
target_result_id bigint;
BEGIN
IF TG_TABLE_NAME = 'record_results' THEN
target_result_id := CASE WHEN TG_OP = 'DELETE' THEN OLD.id ELSE NEW.id END;
INSERT INTO public.search_projection_queue (resource_kind, source_key, action, enqueued_at)
VALUES (
'record',
'result:' || target_result_id::text,
CASE WHEN TG_OP = 'DELETE' THEN 'delete' ELSE 'upsert' END,
now()
)
ON CONFLICT (resource_kind, source_key) DO UPDATE
SET action = EXCLUDED.action, enqueued_at = EXCLUDED.enqueued_at,
attempts = 0, locked_at = NULL, dead_at = NULL, last_error = NULL;
ELSIF TG_TABLE_NAME = 'record_result_holders' THEN
target_result_id := CASE WHEN TG_OP = 'DELETE' THEN OLD.result_id ELSE NEW.result_id END;
INSERT INTO public.search_projection_queue (resource_kind, source_key, action, enqueued_at)
VALUES ('record', 'result:' || target_result_id::text, 'upsert', now())
ON CONFLICT (resource_kind, source_key) DO UPDATE
SET action = 'upsert', enqueued_at = EXCLUDED.enqueued_at,
attempts = 0, locked_at = NULL, dead_at = NULL, last_error = NULL;
ELSE
INSERT INTO public.search_projection_queue (resource_kind, source_key, action, enqueued_at)
SELECT 'record', 'result:' || rr.id::text, 'upsert', now()
FROM public.record_results rr
WHERE rr.run_id = CASE WHEN TG_OP = 'DELETE' THEN OLD.id ELSE NEW.id END
ON CONFLICT (resource_kind, source_key) DO UPDATE
SET action = 'upsert', enqueued_at = EXCLUDED.enqueued_at,
attempts = 0, locked_at = NULL, dead_at = NULL, last_error = NULL;
END IF;
RETURN NULL;
END;
$$;
--
-- Name: enqueue_discord_sync(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.enqueue_discord_sync() RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
target_kind text;
target_key bigint;
target_action text := 'refresh';
BEGIN
IF TG_TABLE_NAME = 'vote_sessions' THEN
target_kind := 'vote_session';
target_key := CASE WHEN TG_OP = 'DELETE' THEN OLD.id ELSE NEW.id END;
IF TG_OP = 'DELETE' THEN target_action := 'delete'; END IF;
ELSIF TG_TABLE_NAME = 'votes' THEN
target_kind := 'vote_session';
target_key := CASE WHEN TG_OP = 'DELETE' THEN OLD.vote_session_id ELSE NEW.vote_session_id END;
IF NOT EXISTS (SELECT 1 FROM public.vote_sessions WHERE id = target_key) THEN RETURN NULL; END IF;
ELSIF TG_TABLE_NAME = 'builds' THEN
target_kind := 'build';
target_key := CASE WHEN TG_OP = 'DELETE' THEN OLD.id ELSE NEW.id END;
IF TG_OP = 'DELETE' THEN target_action := 'delete'; END IF;
ELSE
target_kind := 'build';
target_key := CASE WHEN TG_OP = 'DELETE' THEN OLD.build_id ELSE NEW.build_id END;
IF NOT EXISTS (SELECT 1 FROM public.builds WHERE id = target_key) THEN RETURN NULL; END IF;
END IF;
INSERT INTO public.discord_sync_queue
(resource_kind, source_key, action, enqueued_at, claimed_at, dead_at, attempts, last_error)
VALUES (target_kind, target_key::text, target_action, now(), NULL, NULL, 0, NULL)
ON CONFLICT (resource_kind, source_key) DO UPDATE
SET action = EXCLUDED.action,
enqueued_at = EXCLUDED.enqueued_at,
claimed_at = NULL,
dead_at = NULL,
attempts = 0,
last_error = NULL;
RETURN NULL;
END;
$$;
--
-- Name: enqueue_metadata_search_projection(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.enqueue_metadata_search_projection() RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
target_id bigint;
target_kind text;
target_action text := 'upsert';
BEGIN
target_kind := CASE TG_TABLE_NAME
WHEN 'tag_definitions' THEN 'tag'
WHEN 'tag_aliases' THEN 'tag'
WHEN 'creator_aliases' THEN 'creator'
WHEN 'versions' THEN 'version'
END;
IF TG_TABLE_NAME = 'tag_aliases' THEN
target_id := CASE WHEN TG_OP = 'DELETE' THEN OLD.tag_id ELSE NEW.tag_id END;
ELSE
target_id := CASE WHEN TG_OP = 'DELETE' THEN OLD.id ELSE NEW.id END;
END IF;
IF TG_OP = 'DELETE' AND TG_TABLE_NAME <> 'tag_aliases' THEN
target_action := 'delete';
END IF;
INSERT INTO public.search_projection_queue (resource_kind, source_key, action, enqueued_at)
VALUES ('metadata', target_kind || ':' || target_id::text, target_action, now())
ON CONFLICT (resource_kind, source_key) DO UPDATE
SET action = EXCLUDED.action,
enqueued_at = EXCLUDED.enqueued_at,
attempts = 0,
locked_at = NULL,
dead_at = NULL,
last_error = NULL;
IF TG_TABLE_NAME IN ('tag_definitions', 'tag_aliases') THEN
INSERT INTO public.search_projection_queue (resource_kind, source_key, action, enqueued_at)
SELECT 'build', assignment.build_id::text, 'upsert', now()
FROM public.build_tag_assignments assignment
WHERE assignment.tag_id = target_id
ON CONFLICT (resource_kind, source_key) DO UPDATE
SET action = 'upsert',
enqueued_at = EXCLUDED.enqueued_at,
attempts = 0,
locked_at = NULL,
dead_at = NULL,
last_error = NULL;
INSERT INTO public.discord_sync_queue
(resource_kind, source_key, action, enqueued_at, claimed_at, dead_at, attempts, last_error)
SELECT 'build', assignment.build_id::text, 'refresh', now(), NULL, NULL, 0, NULL
FROM public.build_tag_assignments assignment
WHERE assignment.tag_id = target_id
ON CONFLICT (resource_kind, source_key) DO UPDATE
SET action = EXCLUDED.action,
enqueued_at = EXCLUDED.enqueued_at,
claimed_at = NULL,
dead_at = NULL,
attempts = 0,
last_error = NULL;
ELSIF TG_TABLE_NAME = 'creator_aliases' THEN
INSERT INTO public.search_projection_queue (resource_kind, source_key, action, enqueued_at)
SELECT 'build', bc.build_id::text, 'upsert', now()
FROM public.build_creators bc
WHERE bc.alias_id = target_id
ON CONFLICT (resource_kind, source_key) DO UPDATE
SET action = 'upsert', enqueued_at = EXCLUDED.enqueued_at,
attempts = 0, locked_at = NULL, dead_at = NULL, last_error = NULL;
ELSIF TG_TABLE_NAME = 'versions' THEN
INSERT INTO public.search_projection_queue (resource_kind, source_key, action, enqueued_at)
SELECT 'build', bv.build_id::text, 'upsert', now()
FROM public.build_versions bv
WHERE bv.version_id = target_id
ON CONFLICT (resource_kind, source_key) DO UPDATE
SET action = 'upsert', enqueued_at = EXCLUDED.enqueued_at,
attempts = 0, locked_at = NULL, dead_at = NULL, last_error = NULL;
END IF;
RETURN NULL;
END;
$$;
--
-- Name: get_quantified_version_names(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.get_quantified_version_names() RETURNS TABLE(id smallint, quantified_name text)
LANGUAGE plpgsql
AS $$
BEGIN
RETURN QUERY
SELECT
v.id,
v.edition || ' ' ||
v.major_version || '.' ||
v.minor_version || '.' ||
v.patch_number AS quantified_name
FROM
versions v;
END;
$$;
SET default_tablespace = '';
SET default_table_access_method = heap;
--
-- Name: builds; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.builds (
id bigint NOT NULL,
submission_status smallint NOT NULL,
edited_time timestamp with time zone DEFAULT now(),
record_category text,
extra_info jsonb DEFAULT '{}'::jsonb NOT NULL,
width integer,
height integer,
depth integer,
completion_time text,
submission_time timestamp with time zone DEFAULT CURRENT_TIMESTAMP,
category text,
ai_generated boolean NOT NULL,
original_message_id bigint,
version_spec text,
embedding public.vector(1536),
is_locked boolean DEFAULT false NOT NULL,
locked_at timestamp with time zone,
completion_at timestamp with time zone,
completion_evidence text,
description text,
submitter_account_id integer NOT NULL,
lock_token uuid,
lock_expires_at timestamp with time zone,
revision bigint DEFAULT 1 NOT NULL,
display_name text,
source_submission_draft_id uuid,
sponsor_installation_id uuid,
sponsor_display_name text,
sponsor_address text,
sponsor_description text,
sponsor_website_url text,
CONSTRAINT builds_display_name_valid CHECK (((display_name IS NULL) OR ((display_name = btrim(display_name)) AND (display_name <> ''::text) AND (char_length(display_name) <= 120)))),
CONSTRAINT builds_lock_lease_complete CHECK (((is_locked AND (lock_token IS NOT NULL) AND (lock_expires_at IS NOT NULL)) OR ((NOT is_locked) AND (lock_token IS NULL) AND (lock_expires_at IS NULL)))),
CONSTRAINT builds_revision_positive CHECK ((revision > 0)),
CONSTRAINT builds_sponsor_address_length CHECK (((sponsor_address IS NULL) OR ((char_length(sponsor_address) >= 1) AND (char_length(sponsor_address) <= 255)))),
CONSTRAINT builds_sponsor_description_length CHECK (((sponsor_description IS NULL) OR ((char_length(sponsor_description) >= 1) AND (char_length(sponsor_description) <= 500)))),
CONSTRAINT builds_sponsor_display_name_length CHECK (((sponsor_display_name IS NULL) OR ((char_length(sponsor_display_name) >= 1) AND (char_length(sponsor_display_name) <= 80)))),
CONSTRAINT builds_sponsor_projection_complete CHECK (((sponsor_installation_id IS NOT NULL) OR ((sponsor_display_name IS NULL) AND (sponsor_address IS NULL) AND (sponsor_description IS NULL) AND (sponsor_website_url IS NULL)))),
CONSTRAINT builds_sponsor_website_valid CHECK (((sponsor_website_url IS NULL) OR (((char_length(sponsor_website_url) >= 1) AND (char_length(sponsor_website_url) <= 2048)) AND (sponsor_website_url ~ '^https?://'::text)))),
CONSTRAINT check_record_category CHECK ((record_category = ANY (ARRAY['Smallest'::text, 'Fastest'::text, 'First'::text, 'Smallest Fastest'::text, 'Fastest Smallest'::text, NULL::text]))),
CONSTRAINT check_status CHECK ((submission_status = ANY (ARRAY[0, 1, 2]))),
CONSTRAINT submissions_build_depth_check CHECK ((depth > 0)),
CONSTRAINT submissions_build_height_check CHECK ((height > 0)),
CONSTRAINT submissions_build_width_check CHECK ((width > 0))
);
--
-- Name: TABLE builds; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON TABLE public.builds IS 'A build submitted by a user.';
--
-- Name: COLUMN builds.embedding; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON COLUMN public.builds.embedding IS 'Application-owned semantic vector stored in the authoritative build row.';
--
-- Name: get_unsent_builds(bigint); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.get_unsent_builds(server_id_input bigint) RETURNS SETOF public.builds
LANGUAGE plpgsql
AS $$
begin
return query select *
from builds
where id not in (
select build_id
from messages
where server_id = server_id_input
)
and submission_status = 1; -- accepted
end;
$$;
--
-- Name: initialize_discord_message_projection(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.initialize_discord_message_projection() RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
current_generation bigint;
BEGIN
IF NEW.projection_resource_kind IS NULL OR NEW.projection_source_key IS NULL THEN
RETURN NEW;
END IF;
SELECT generation INTO current_generation
FROM public.discord_sync_queue
WHERE resource_kind = NEW.projection_resource_kind
AND source_key = NEW.projection_source_key;
NEW.desired_action := 'refresh';
NEW.desired_revision := COALESCE(current_generation, 1);
NEW.applied_revision := NEW.desired_revision;
RETURN NEW;
END;
$$;
--
-- Name: power_set_max(text[], integer); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.power_set_max(txt text[], max_k integer DEFAULT 8) RETURNS SETOF text[]
LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE
AS $$
DECLARE
n int := array_length(txt, 1);
mask int;
BEGIN
IF n IS NULL OR n = 0 THEN
RETURN NEXT ARRAY[]::text[];
RETURN;
END IF;
FOR mask IN 0 .. (1 << n) - 1 LOOP
-- skip masks with more than max_k bits set
IF (
SELECT COUNT(*)
FROM generate_series(0, n - 1) g
WHERE ((mask >> g) & 1) = 1
) > max_k THEN
CONTINUE;
END IF;
RETURN NEXT coalesce( -- fall back to empty array
(SELECT array_agg(txt[i] ORDER BY i)
FROM generate_subscripts(txt, 1) AS i
WHERE (mask >> (i - 1)) & 1 = 1),
ARRAY[]::text[]
);
END LOOP;
END;
$$;
--
-- Name: project_discord_message_desired_state(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.project_discord_message_desired_state() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
UPDATE public.messages
SET desired_action = NEW.action,
desired_revision = NEW.generation
WHERE projection_resource_kind = NEW.resource_kind
AND projection_source_key = NEW.source_key;
RETURN NULL;
END;
$$;
--
-- Name: publish_domain_event(text, integer, text, bigint, jsonb); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.publish_domain_event(event_type_input text, schema_version_input integer, aggregate_kind_input text, aggregate_id_input bigint, payload_input jsonb DEFAULT '{}'::jsonb) RETURNS bigint
LANGUAGE plpgsql
AS $$
DECLARE
new_event_id bigint;
BEGIN
IF event_type_input = '' OR aggregate_kind_input = '' OR schema_version_input < 1 THEN
RAISE EXCEPTION 'invalid domain event envelope';
END IF;
INSERT INTO public.domain_events
(event_type, schema_version, aggregate_kind, aggregate_id, payload, occurred_at)
VALUES (
event_type_input,
schema_version_input,
aggregate_kind_input,
aggregate_id_input,
coalesce(payload_input, '{}'::jsonb),
now()
)
RETURNING id INTO new_event_id;
INSERT INTO public.domain_event_deliveries
(event_id, consumer, available_at, claimed_at, attempts, last_error)
SELECT new_event_id, consumers.name, now(), NULL, 0, NULL
FROM public.domain_event_consumers AS consumers;
-- PostgreSQL delivers NOTIFY only after this transaction commits. It is a
-- latency hint; durable consumers still poll domain_event_deliveries.
PERFORM pg_notify('squid_domain_events', new_event_id::text);
RETURN new_event_id;
END;
$$;
--
-- Name: set_locked_at(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.set_locked_at() RETURNS trigger
LANGUAGE plpgsql
AS $$
begin
if new.is_locked then
new.locked_at := now();
else
new.locked_at := null;
end if;
return new;
end;
$$;
--
-- Name: update_updated_at_column(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.update_updated_at_column() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
NEW.updated_at = now();
RETURN NEW;
END;
$$;
--
-- Name: account_identities; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.account_identities (
id bigint NOT NULL,
account_id integer NOT NULL,
provider text NOT NULL,
subject text NOT NULL,
display_name text,
verified_at timestamp with time zone DEFAULT now() NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT account_identities_provider_check CHECK ((provider = ANY (ARRAY['discord'::text, 'java'::text, 'bedrock'::text]))),
CONSTRAINT account_identities_subject_check CHECK (((subject = btrim(subject)) AND (subject <> ''::text))),
CONSTRAINT account_identities_subject_format_check CHECK ((((provider <> 'discord'::text) OR (subject ~ '^[1-9][0-9]*$'::text)) AND ((provider <> 'bedrock'::text) OR (subject ~ '^[1-9][0-9]*$'::text)) AND ((provider <> 'java'::text) OR (subject ~ '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'::text))))
);
--
-- Name: TABLE account_identities; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON TABLE public.account_identities IS 'A verified provider subject attached to exactly one account.';
--
-- Name: account_identities_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
ALTER TABLE public.account_identities ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY (
SEQUENCE NAME public.account_identities_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
);
--
-- Name: accounts; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.accounts (
id integer NOT NULL,
created_at timestamp with time zone DEFAULT now(),
consent_version text,
consented_at timestamp with time zone,
public_creator_id uuid DEFAULT gen_random_uuid() NOT NULL,
CONSTRAINT accounts_consent_receipt_complete CHECK (((consent_version IS NULL) = (consented_at IS NULL)))
);
--
-- Name: TABLE accounts; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON TABLE public.accounts IS 'An internal principal independent of every external identity provider.';
--
-- Name: accounts_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE public.accounts_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: accounts_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE public.accounts_id_seq OWNED BY public.accounts.id;
--
-- Name: api_keys; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.api_keys (
id bigint NOT NULL,
key_id text NOT NULL,
secret_hash bytea NOT NULL,
label text NOT NULL,
scopes text[] DEFAULT '{}'::text[] NOT NULL,
owner_account_id integer,
created_by_account_id integer,
created_at timestamp with time zone DEFAULT now() NOT NULL,
expires_at timestamp with time zone,
revoked_at timestamp with time zone,
last_used_at timestamp with time zone,
last_used_ip inet
);
--
-- Name: TABLE api_keys; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON TABLE public.api_keys IS 'A revocable high-entropy credential used by an API service client.';
--
-- Name: api_keys_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
ALTER TABLE public.api_keys ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY (
SEQUENCE NAME public.api_keys_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
);
--
-- Name: build_creators; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.build_creators (
build_id bigint NOT NULL,
alias_id integer NOT NULL
);
--
-- Name: TABLE build_creators; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON TABLE public.build_creators IS 'Association table between builds and the creator names credited on them.';
--
-- Name: build_edit_history; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.build_edit_history (
build_id bigint NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
version smallint NOT NULL
);
--
-- Name: TABLE build_edit_history; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON TABLE public.build_edit_history IS 'A version marker recorded when a build is edited.';
--
-- Name: build_edit_history_build_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
ALTER TABLE public.build_edit_history ALTER COLUMN build_id ADD GENERATED BY DEFAULT AS IDENTITY (
SEQUENCE NAME public.build_edit_history_build_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
);
--
-- Name: build_links; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.build_links (
build_id bigint NOT NULL,
url text NOT NULL,
media_type text
);
--
-- Name: TABLE build_links; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON TABLE public.build_links IS 'A link associated with a build (image, video, world download).';
--
-- Name: build_schematics; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.build_schematics (
id bigint NOT NULL,
build_id bigint NOT NULL,
file_sha256 text NOT NULL,
is_primary boolean NOT NULL,
original_filename text,
width integer NOT NULL,
height integer NOT NULL,
length integer NOT NULL,
allocated_width integer NOT NULL,
allocated_height integer NOT NULL,
allocated_length integer NOT NULL,
block_count integer NOT NULL,
bounding_volume bigint NOT NULL,
entity_count integer NOT NULL,
palette_size integer NOT NULL,
region_names text[] NOT NULL,
source_data_version integer,
declared_name text,
declared_author text,
signs jsonb NOT NULL,
fingerprint_structural text,
fingerprint_shape text,
fingerprint_exact text,
signature_structural text,
analyzer_version text NOT NULL,
analysis_schema_version smallint NOT NULL,
lattice jsonb,
uploaded_by_discord_id bigint,
analyzed_at timestamp with time zone DEFAULT now() NOT NULL,
simulation_evidence jsonb,
visibility text DEFAULT 'legacy_unverified'::text NOT NULL,
license_code text,
rights_attested_at timestamp with time zone,
rights_attested_by_account_id integer,
published_at timestamp with time zone,
withdrawn_at timestamp with time zone,
CONSTRAINT build_schematics_license_check CHECK (((license_code IS NULL) OR (license_code = ANY (ARRAY['cc0_1_0'::text, 'cc_by_4_0'::text, 'cc_by_sa_4_0'::text, 'cc_by_nd_4_0'::text, 'cc_by_nc_4_0'::text, 'cc_by_nc_sa_4_0'::text, 'cc_by_nc_nd_4_0'::text])))),
CONSTRAINT build_schematics_publication_complete CHECK (((visibility <> 'public_download'::text) OR ((license_code IS NOT NULL) AND (rights_attested_at IS NOT NULL) AND (rights_attested_by_account_id IS NOT NULL)))),
CONSTRAINT build_schematics_visibility_check CHECK ((visibility = ANY (ARRAY['legacy_unverified'::text, 'reviewer_only'::text, 'public_download'::text])))
);
--
-- Name: TABLE build_schematics; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON TABLE public.build_schematics IS 'One analyzed schematic attached to a build.
Metrics and fingerprints are denormalised onto this row so duplicate shortlisting is a
plain indexed query. Fingerprints are only comparable within the `analyzer_version` that
produced them, so every identity index carries that column and every lookup filters on it;
an engine upgrade therefore becomes a visible backfill rather than a silent regression.';
--
-- Name: build_schematics_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE public.build_schematics_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: build_schematics_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE public.build_schematics_id_seq OWNED BY public.build_schematics.id;
--
-- Name: build_tag_assignments; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.build_tag_assignments (
build_id bigint NOT NULL,
tag_id bigint NOT NULL,
value_type text NOT NULL,
numeric_value numeric,
text_value text,
boolean_value boolean,
display_unit_key text,
display_order smallint,
evidence text,
provenance text NOT NULL,
created_by_discord_id bigint,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT build_tag_assignments_finite_numeric_check CHECK (((numeric_value IS NULL) OR ((numeric_value)::text <> ALL (ARRAY['NaN'::text, 'Infinity'::text, '-Infinity'::text])))),
CONSTRAINT build_tag_assignments_order_check CHECK (((display_order IS NULL) OR (display_order >= 0))),
CONSTRAINT build_tag_assignments_provenance_check CHECK ((provenance = ANY (ARRAY['submitted'::text, 'inferred'::text, 'moderated'::text, 'legacy_import'::text]))),
CONSTRAINT build_tag_assignments_typed_value_check CHECK ((((value_type = 'none'::text) AND (num_nonnulls(numeric_value, text_value, boolean_value) = 0)) OR ((value_type = 'numeric'::text) AND (numeric_value IS NOT NULL) AND (num_nonnulls(text_value, boolean_value) = 0)) OR ((value_type = 'text'::text) AND (text_value IS NOT NULL) AND (num_nonnulls(numeric_value, boolean_value) = 0)) OR ((value_type = 'boolean'::text) AND (boolean_value IS NOT NULL) AND (num_nonnulls(numeric_value, text_value) = 0))))
);
--
-- Name: TABLE build_tag_assignments; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON TABLE public.build_tag_assignments IS 'A typed tag value attached to one build.';
--
-- Name: build_versions; Type: TABLE; Schema: public; Owner: -
--