-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.sql
3230 lines (2275 loc) · 94.9 KB
/
backup.sql
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
--
-- Dumped from database version 12.5 (Ubuntu 12.5-1.pgdg18.04+1)
-- Dumped by pg_dump version 14.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: auth; Type: SCHEMA; Schema: -; Owner: supabase_admin
--
CREATE SCHEMA auth;
ALTER SCHEMA auth OWNER TO supabase_admin;
--
-- Name: extensions; Type: SCHEMA; Schema: -; Owner: postgres
--
CREATE SCHEMA extensions;
ALTER SCHEMA extensions OWNER TO postgres;
--
-- Name: realtime; Type: SCHEMA; Schema: -; Owner: supabase_admin
--
CREATE SCHEMA realtime;
ALTER SCHEMA realtime OWNER TO supabase_admin;
--
-- Name: storage; Type: SCHEMA; Schema: -; Owner: supabase_admin
--
CREATE SCHEMA storage;
ALTER SCHEMA storage OWNER TO supabase_admin;
--
-- Name: moddatetime; Type: EXTENSION; Schema: -; Owner: -
--
CREATE EXTENSION IF NOT EXISTS moddatetime WITH SCHEMA extensions;
--
-- Name: EXTENSION moddatetime; Type: COMMENT; Schema: -; Owner:
--
COMMENT ON EXTENSION moddatetime IS 'functions for tracking last modification time';
--
-- Name: pg_stat_statements; Type: EXTENSION; Schema: -; Owner: -
--
CREATE EXTENSION IF NOT EXISTS pg_stat_statements WITH SCHEMA extensions;
--
-- Name: EXTENSION pg_stat_statements; Type: COMMENT; Schema: -; Owner:
--
COMMENT ON EXTENSION pg_stat_statements IS 'track execution statistics of all SQL statements executed';
--
-- Name: pg_trgm; Type: EXTENSION; Schema: -; Owner: -
--
CREATE EXTENSION IF NOT EXISTS pg_trgm WITH SCHEMA extensions;
--
-- Name: EXTENSION pg_trgm; Type: COMMENT; Schema: -; Owner:
--
COMMENT ON EXTENSION pg_trgm IS 'text similarity measurement and index searching based on trigrams';
--
-- Name: pgcrypto; Type: EXTENSION; Schema: -; Owner: -
--
CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA extensions;
--
-- Name: EXTENSION pgcrypto; Type: COMMENT; Schema: -; Owner:
--
COMMENT ON EXTENSION pgcrypto IS 'cryptographic functions';
--
-- Name: pgjwt; Type: EXTENSION; Schema: -; Owner: -
--
CREATE EXTENSION IF NOT EXISTS pgjwt WITH SCHEMA extensions;
--
-- Name: EXTENSION pgjwt; Type: COMMENT; Schema: -; Owner:
--
COMMENT ON EXTENSION pgjwt IS 'JSON Web Token API for Postgresql';
--
-- 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: uuid-ossp; Type: EXTENSION; Schema: -; Owner: -
--
CREATE EXTENSION IF NOT EXISTS "uuid-ossp" WITH SCHEMA extensions;
--
-- Name: EXTENSION "uuid-ossp"; Type: COMMENT; Schema: -; Owner:
--
COMMENT ON EXTENSION "uuid-ossp" IS 'generate universally unique identifiers (UUIDs)';
--
-- Name: post_type__; Type: TYPE; Schema: public; Owner: supabase_admin
--
CREATE TYPE public.post_type__ AS ENUM (
'presential',
'online',
'presencial'
);
ALTER TYPE public.post_type__ OWNER TO supabase_admin;
--
-- Name: action; Type: TYPE; Schema: realtime; Owner: supabase_admin
--
CREATE TYPE realtime.action AS ENUM (
'INSERT',
'UPDATE',
'DELETE',
'TRUNCATE',
'ERROR'
);
ALTER TYPE realtime.action OWNER TO supabase_admin;
--
-- Name: equality_op; Type: TYPE; Schema: realtime; Owner: supabase_admin
--
CREATE TYPE realtime.equality_op AS ENUM (
'eq',
'neq',
'lt',
'lte',
'gt',
'gte'
);
ALTER TYPE realtime.equality_op OWNER TO supabase_admin;
--
-- Name: user_defined_filter; Type: TYPE; Schema: realtime; Owner: supabase_admin
--
CREATE TYPE realtime.user_defined_filter AS (
column_name text,
op realtime.equality_op,
value text
);
ALTER TYPE realtime.user_defined_filter OWNER TO supabase_admin;
--
-- Name: wal_column; Type: TYPE; Schema: realtime; Owner: supabase_admin
--
CREATE TYPE realtime.wal_column AS (
name text,
type text,
value jsonb,
is_pkey boolean,
is_selectable boolean
);
ALTER TYPE realtime.wal_column OWNER TO supabase_admin;
--
-- Name: wal_rls; Type: TYPE; Schema: realtime; Owner: supabase_admin
--
CREATE TYPE realtime.wal_rls AS (
wal jsonb,
is_rls_enabled boolean,
users uuid[],
errors text[]
);
ALTER TYPE realtime.wal_rls OWNER TO supabase_admin;
--
-- Name: email(); Type: FUNCTION; Schema: auth; Owner: supabase_auth_admin
--
CREATE FUNCTION auth.email() RETURNS text
LANGUAGE sql STABLE
AS $$
select
coalesce(
current_setting('request.jwt.claim.email', true),
(current_setting('request.jwt.claims', true)::jsonb ->> 'email')
)::text
$$;
ALTER FUNCTION auth.email() OWNER TO supabase_auth_admin;
--
-- Name: role(); Type: FUNCTION; Schema: auth; Owner: supabase_auth_admin
--
CREATE FUNCTION auth.role() RETURNS text
LANGUAGE sql STABLE
AS $$
select
coalesce(
current_setting('request.jwt.claim.role', true),
(current_setting('request.jwt.claims', true)::jsonb ->> 'role')
)::text
$$;
ALTER FUNCTION auth.role() OWNER TO supabase_auth_admin;
--
-- Name: uid(); Type: FUNCTION; Schema: auth; Owner: supabase_auth_admin
--
CREATE FUNCTION auth.uid() RETURNS uuid
LANGUAGE sql STABLE
AS $$
select
nullif(
coalesce(
current_setting('request.jwt.claim.sub', true),
(current_setting('request.jwt.claims', true)::jsonb ->> 'sub')
),
''
)::uuid
$$;
ALTER FUNCTION auth.uid() OWNER TO supabase_auth_admin;
--
-- Name: notify_api_restart(); Type: FUNCTION; Schema: extensions; Owner: postgres
--
CREATE FUNCTION extensions.notify_api_restart() RETURNS event_trigger
LANGUAGE plpgsql
AS $$
BEGIN
NOTIFY ddl_command_end;
END;
$$;
ALTER FUNCTION extensions.notify_api_restart() OWNER TO postgres;
--
-- Name: FUNCTION notify_api_restart(); Type: COMMENT; Schema: extensions; Owner: postgres
--
COMMENT ON FUNCTION extensions.notify_api_restart() IS 'Sends a notification to the API to restart. If your database schema has changed, this is required so that Supabase can rebuild the relationships.';
--
-- Name: edit_role(uuid, text); Type: FUNCTION; Schema: public; Owner: supabase_admin
--
CREATE FUNCTION public.edit_role(uuid uuid, new_role text) RETURNS text
LANGUAGE plpgsql
AS $$
DECLARE
updated_role text;
BEGIN
UPDATE auth.users
SET role = new_role
WHERE id = uuid;
SELECT role INTO updated_role
FROM auth.users WHERE id = uuid;
RETURN updated_role;
END;
$$;
ALTER FUNCTION public.edit_role(uuid uuid, new_role text) OWNER TO supabase_admin;
--
-- Name: get_landing_aggs(); Type: FUNCTION; Schema: public; Owner: supabase_admin
--
CREATE FUNCTION public.get_landing_aggs() RETURNS jsonb
LANGUAGE plpgsql
AS $$
DECLARE
n_games int;
n_posts int;
n_dm_users int;
BEGIN
SELECT count(id)
INTO n_games
FROM games;
SELECT count(id)
INTO n_posts
FROM posts;
SELECT count(distinct narrator_id)
INTO n_dm_users
FROM posts;
return jsonb_build_object(
'games', n_games,
'posts', n_posts,
'dms', n_dm_users
);
END;
$$;
ALTER FUNCTION public.get_landing_aggs() OWNER TO supabase_admin;
--
-- Name: handle_delete_user(); Type: FUNCTION; Schema: public; Owner: supabase_admin
--
CREATE FUNCTION public.handle_delete_user() RETURNS trigger
LANGUAGE plpgsql SECURITY DEFINER
AS $$
BEGIN
DELETE FROM public.users WHERE OLD.id = id;
END;
$$;
ALTER FUNCTION public.handle_delete_user() OWNER TO supabase_admin;
--
-- Name: handle_new_user(); Type: FUNCTION; Schema: public; Owner: supabase_admin
--
CREATE FUNCTION public.handle_new_user() RETURNS trigger
LANGUAGE plpgsql SECURITY DEFINER
AS $$
begin
insert into public.users (id, email, name, role)
values (
NEW.id,
md5(NEW.email),
(regexp_split_to_array(NEW.email, '@'))[1],
NEW.role
)
on CONFLICT do NOTHING;
return NEW;
end;
$$;
ALTER FUNCTION public.handle_new_user() OWNER TO supabase_admin;
--
-- Name: handle_user_update(); Type: FUNCTION; Schema: public; Owner: supabase_admin
--
CREATE FUNCTION public.handle_user_update() RETURNS trigger
LANGUAGE plpgsql SECURITY DEFINER
AS $$
BEGIN
update public.users
set last_sign_in_at = NEW.last_sign_in_at,
email = md5(NEW.email),
role = NEW.role
where id = NEW.id;
return NEW;
END;
$$;
ALTER FUNCTION public.handle_user_update() OWNER TO supabase_admin;
--
-- Name: slugify(text); Type: FUNCTION; Schema: public; Owner: supabase_admin
--
CREATE FUNCTION public.slugify(value text) RETURNS text
LANGUAGE sql IMMUTABLE STRICT
AS $_$
-- removes accents (diacritic signs) from a given string --
WITH "unaccented" AS (
SELECT unaccent("value") AS "value"
),
-- lowercases the string
"lowercase" AS (
SELECT lower("value") AS "value"
FROM "unaccented"
),
-- remove single and double quotes
"removed_quotes" AS (
SELECT regexp_replace("value", '[''"]+', '', 'gi') AS "value"
FROM "lowercase"
),
-- transform '&' to 'n'
"transform_and_symbol" AS (
SELECT regexp_replace("value", '&+', 'n', 'gi') AS "value"
FROM "removed_quotes"
),
-- replaces anything that's not a letter, number, hyphen('-'), or underscore('_') with a hyphen('-')
"hyphenated" AS (
SELECT regexp_replace("value", '[^a-z0-9\\-_]+', '-', 'gi') AS "value"
FROM "transform_and_symbol"
),
-- trims hyphens('-') if they exist on the head or tail of the string
"trimmed" AS (
SELECT regexp_replace(regexp_replace("value", '\-+$', ''), '^\-', '') AS "value"
FROM "hyphenated"
)
SELECT "value" FROM "trimmed";
$_$;
ALTER FUNCTION public.slugify(value text) OWNER TO supabase_admin;
SET default_tablespace = '';
SET default_table_access_method = heap;
--
-- Name: posts; Type: TABLE; Schema: public; Owner: supabase_admin
--
CREATE TABLE public.posts (
id bigint NOT NULL,
inserted_at timestamp with time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
updated_at timestamp with time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
name text NOT NULL,
slug text GENERATED ALWAYS AS (public.slugify(name)) STORED,
description text,
tags text[] NOT NULL,
seats integer,
section integer,
date date NOT NULL,
place text,
place_link text,
image character varying,
game integer,
narrator_id uuid,
"time" character varying,
image_position character varying,
is_draft boolean DEFAULT false NOT NULL,
dates_with_labels jsonb[] DEFAULT '{}'::jsonb[] NOT NULL
);
ALTER TABLE public.posts OWNER TO supabase_admin;
--
-- Name: has_free_seats(public.posts); Type: FUNCTION; Schema: public; Owner: supabase_admin
--
CREATE FUNCTION public.has_free_seats(public.posts) RETURNS boolean
LANGUAGE sql
AS $_$
SELECT seats > coalesce(array_length(players(p.*), 1), 0) FROM posts p where id = $1.id
$_$;
ALTER FUNCTION public.has_free_seats(public.posts) OWNER TO supabase_admin;
--
-- Name: users; Type: TABLE; Schema: public; Owner: supabase_admin
--
CREATE TABLE public.users (
id uuid NOT NULL,
email character varying NOT NULL,
name character varying NOT NULL,
challengeable boolean DEFAULT false NOT NULL,
bio text,
last_sign_in_at timestamp without time zone,
role character varying DEFAULT 'authenticated'::character varying NOT NULL,
avatar character varying
);
ALTER TABLE public.users OWNER TO supabase_admin;
--
-- Name: TABLE users; Type: COMMENT; Schema: public; Owner: supabase_admin
--
COMMENT ON TABLE public.users IS 'User profile table';
--
-- Name: COLUMN users.challengeable; Type: COMMENT; Schema: public; Owner: supabase_admin
--
COMMENT ON COLUMN public.users.challengeable IS 'Available for challange';
--
-- Name: is_narrator(public.users); Type: FUNCTION; Schema: public; Owner: supabase_admin
--
CREATE FUNCTION public.is_narrator(public.users) RETURNS boolean
LANGUAGE sql
AS $_$
SELECT exists(
SELECT id FROM posts where narrator_id = $1.id
)
$_$;
ALTER FUNCTION public.is_narrator(public.users) OWNER TO supabase_admin;
--
-- Name: narrator(public.posts); Type: FUNCTION; Schema: public; Owner: supabase_admin
--
CREATE FUNCTION public.narrator(public.posts) RETURNS jsonb
LANGUAGE sql
AS $_$
select jsonb_build_object(
'id', u.id,
'name', u.name,
'email', u.email,
'avatar', u.avatar
)
from posts p
left join users u on u.id = p.narrator_id
where p.id = $1.id
$_$;
ALTER FUNCTION public.narrator(public.posts) OWNER TO supabase_admin;
--
-- Name: players(public.posts); Type: FUNCTION; Schema: public; Owner: supabase_admin
--
CREATE FUNCTION public.players(public.posts) RETURNS jsonb[]
LANGUAGE sql
AS $_$
select COALESCE(array_agg(q.jsonb_build_object), ARRAY[]::jsonb[])
from (
select jsonb_build_object(
'id', u.id,
'name', u.name,
'email', u.email,
'avatar', u.avatar
)
from posts p
join players up on up.post_id = p.id
join users u on u.id = up.user_id
where p.id = $1.id
) q
$_$;
ALTER FUNCTION public.players(public.posts) OWNER TO supabase_admin;
--
-- Name: games; Type: TABLE; Schema: public; Owner: supabase_admin
--
CREATE TABLE public.games (
id bigint NOT NULL,
inserted_at timestamp with time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
updated_at timestamp with time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
name text NOT NULL,
slug text GENERATED ALWAYS AS (public.slugify(name)) STORED,
description text,
tags text[] DEFAULT '{}'::text[],
image_position real,
image character varying
);
ALTER TABLE public.games OWNER TO supabase_admin;
--
-- Name: post_count(public.games); Type: FUNCTION; Schema: public; Owner: supabase_admin
--
CREATE FUNCTION public.post_count(public.games) RETURNS bigint
LANGUAGE sql
AS $_$
SELECT COUNT(*) FROM posts p WHERE p.game = $1.id
$_$;
ALTER FUNCTION public.post_count(public.games) OWNER TO supabase_admin;
--
-- Name: posts_from_user(uuid); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION public.posts_from_user(userid uuid) RETURNS jsonb[]
LANGUAGE sql
AS $$
SELECT COALESCE(array_agg(q.jsonb_build_object), ARRAY[]::jsonb[])
FROM (
SELECT jsonb_build_object(
'id', p.id,
'name', name,
'slug', slug,
'tags', tags,
'seats', seats,
'date', date,
'time', time,
'image', image,
'place', place,
'place_link', place_link,
'game', (SELECT jsonb_build_object('id', id, 'name', name, 'slug', slug) FROM games g WHERE g.id = p.game),
'section', (SELECT jsonb_build_object('id', id, 'name', name) FROM sections s WHERE s.id = p.section),
'narrator', narrator(p.*),
'players', players(p.*)
)
FROM posts p
WHERE p.narrator_id = userid
OR p.id = ANY(select post_id from players where user_id = userid)
ORDER BY date desc, time desc, narrator_id desc, slug desc
) q
$$;
ALTER FUNCTION public.posts_from_user(userid uuid) OWNER TO postgres;
--
-- Name: update_players(bigint, uuid[]); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION public.update_players(postid bigint, players uuid[]) RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
player uuid;
BEGIN
DELETE FROM players WHERE post_id = postid;
FOREACH player IN ARRAY players
LOOP
INSERT INTO players (user_id, post_id) VALUES (player, postid);
END LOOP;
END
$$;
ALTER FUNCTION public.update_players(postid bigint, players uuid[]) OWNER TO postgres;
--
-- Name: apply_rls(jsonb, integer); Type: FUNCTION; Schema: realtime; Owner: supabase_admin
--
CREATE FUNCTION realtime.apply_rls(wal jsonb, max_record_bytes integer DEFAULT (1024 * 1024)) RETURNS realtime.wal_rls
LANGUAGE plpgsql
AS $$
declare
-- Regclass of the table e.g. public.notes
entity_ regclass = (quote_ident(wal ->> 'schema') || '.' || quote_ident(wal ->> 'table'))::regclass;
-- I, U, D, T: insert, update ...
action realtime.action = (
case wal ->> 'action'
when 'I' then 'INSERT'
when 'U' then 'UPDATE'
when 'D' then 'DELETE'
when 'T' then 'TRUNCATE'
else 'ERROR'
end
);
-- Is row level security enabled for the table
is_rls_enabled bool = relrowsecurity from pg_class where oid = entity_;
-- Subscription vars
user_id uuid;
email varchar(255);
user_has_access bool;
is_visible_to_user boolean;
visible_to_user_ids uuid[] = '{}';
-- user subscriptions to the wal record's table
subscriptions realtime.subscription[] =
array_agg(sub)
from
realtime.subscription sub
where
sub.entity = entity_;
-- structured info for wal's columns
columns realtime.wal_column[] =
array_agg(
(
x->>'name',
x->>'type',
realtime.cast((x->'value') #>> '{}', (x->>'type')::regtype),
(pks ->> 'name') is not null,
pg_catalog.has_column_privilege('authenticated', entity_, x->>'name', 'SELECT')
)::realtime.wal_column
)
from
jsonb_array_elements(wal -> 'columns') x
left join jsonb_array_elements(wal -> 'pk') pks
on (x ->> 'name') = (pks ->> 'name');
-- previous identity values for update/delete
old_columns realtime.wal_column[] =
array_agg(
(
x->>'name',
x->>'type',
realtime.cast((x->'value') #>> '{}', (x->>'type')::regtype),
(pks ->> 'name') is not null,
pg_catalog.has_column_privilege('authenticated', entity_, x->>'name', 'SELECT')
)::realtime.wal_column
)
from
jsonb_array_elements(wal -> 'identity') x
left join jsonb_array_elements(wal -> 'pk') pks
on (x ->> 'name') = (pks ->> 'name');
output jsonb;
-- Error states
error_record_exceeds_max_size boolean = octet_length(wal::text) > max_record_bytes;
error_unauthorized boolean = not pg_catalog.has_any_column_privilege('authenticated', entity_, 'SELECT');
errors text[] = case
when error_record_exceeds_max_size then array['Error 413: Payload Too Large']
else '{}'::text[]
end;
begin
-- The 'authenticated' user does not have SELECT permission on any of the columns for the entity_
if error_unauthorized is true then
return (
null,
null,
visible_to_user_ids,
array['Error 401: Unauthorized']
)::realtime.wal_rls;
end if;
-------------------------------
-- Build Output JSONB Object --
-------------------------------
output = jsonb_build_object(
'schema', wal ->> 'schema',
'table', wal ->> 'table',
'type', action,
'commit_timestamp', (wal ->> 'timestamp')::text::timestamp with time zone,
'columns', (
select
jsonb_agg(
jsonb_build_object(
'name', pa.attname,
'type', pt.typname
)
order by pa.attnum asc
)
from
pg_attribute pa
join pg_type pt
on pa.atttypid = pt.oid
where
attrelid = entity_
and attnum > 0
and pg_catalog.has_column_privilege('authenticated', entity_, pa.attname, 'SELECT')
)
)
-- Add "record" key for insert and update
|| case
when error_record_exceeds_max_size then jsonb_build_object('record', '{}'::jsonb)
when action in ('INSERT', 'UPDATE') then
jsonb_build_object(
'record',
(select jsonb_object_agg((c).name, (c).value) from unnest(columns) c where (c).is_selectable)
)
else '{}'::jsonb
end
-- Add "old_record" key for update and delete
|| case
when error_record_exceeds_max_size then jsonb_build_object('old_record', '{}'::jsonb)
when action in ('UPDATE', 'DELETE') then
jsonb_build_object(
'old_record',
(select jsonb_object_agg((c).name, (c).value) from unnest(old_columns) c where (c).is_selectable)
)
else '{}'::jsonb
end;
if action in ('TRUNCATE', 'DELETE') then
visible_to_user_ids = array_agg(s.user_id) from unnest(subscriptions) s;
else
-- If RLS is on and someone is subscribed to the table prep
if is_rls_enabled and array_length(subscriptions, 1) > 0 then
perform set_config('role', 'authenticated', true);
if (select 1 from pg_prepared_statements where name = 'walrus_rls_stmt' limit 1) > 0 then
deallocate walrus_rls_stmt;
end if;
execute realtime.build_prepared_statement_sql('walrus_rls_stmt', entity_, columns);
end if;
-- For each subscribed user
for user_id, email, is_visible_to_user in (
select
subs.user_id,
subs.email,
realtime.is_visible_through_filters(columns, subs.filters)
from
unnest(subscriptions) subs
)
loop
if is_visible_to_user then
-- If RLS is off, add to visible users
if not is_rls_enabled then
visible_to_user_ids = visible_to_user_ids || user_id;
else
-- Check if RLS allows the user to see the record
perform
set_config(
'request.jwt.claims',
jsonb_build_object(
'sub', user_id::text,
'email', email::text,
'role', 'authenticated'
)::text,
true
);
execute 'execute walrus_rls_stmt' into user_has_access;
if user_has_access then
visible_to_user_ids = visible_to_user_ids || user_id;
end if;
end if;
end if;
end loop;
perform (
set_config('role', null, true)
);
end if;
return (
output,
is_rls_enabled,
visible_to_user_ids,
errors
)::realtime.wal_rls;
end;
$$;
ALTER FUNCTION realtime.apply_rls(wal jsonb, max_record_bytes integer) OWNER TO supabase_admin;
--
-- Name: build_prepared_statement_sql(text, regclass, realtime.wal_column[]); Type: FUNCTION; Schema: realtime; Owner: supabase_admin
--
CREATE FUNCTION realtime.build_prepared_statement_sql(prepared_statement_name text, entity regclass, columns realtime.wal_column[]) RETURNS text
LANGUAGE sql
AS $$
/*
Builds a sql string that, if executed, creates a prepared statement to
tests retrive a row from *entity* by its primary key columns.
Example
select realtime.build_prepared_statment_sql('public.notes', '{"id"}'::text[], '{"bigint"}'::text[])
*/
select
'prepare ' || prepared_statement_name || ' as
select
exists(
select
1
from
' || entity || '
where
' || string_agg(quote_ident(pkc.name) || '=' || quote_nullable(pkc.value #>> '{}') , ' and ') || '
)'
from
unnest(columns) pkc
where
pkc.is_pkey
group by
entity
$$;
ALTER FUNCTION realtime.build_prepared_statement_sql(prepared_statement_name text, entity regclass, columns realtime.wal_column[]) OWNER TO supabase_admin;
--
-- Name: cast(text, regtype); Type: FUNCTION; Schema: realtime; Owner: supabase_admin
--
CREATE FUNCTION realtime."cast"(val text, type_ regtype) RETURNS jsonb
LANGUAGE plpgsql IMMUTABLE
AS $$
declare
res jsonb;
begin
execute format('select to_jsonb(%L::'|| type_::text || ')', val) into res;
return res;
end
$$;
ALTER FUNCTION realtime."cast"(val text, type_ regtype) OWNER TO supabase_admin;
--
-- Name: check_equality_op(realtime.equality_op, regtype, text, text); Type: FUNCTION; Schema: realtime; Owner: supabase_admin
--
CREATE FUNCTION realtime.check_equality_op(op realtime.equality_op, type_ regtype, val_1 text, val_2 text) RETURNS boolean
LANGUAGE plpgsql IMMUTABLE
AS $$
/*
Casts *val_1* and *val_2* as type *type_* and check the *op* condition for truthiness
*/
declare
op_symbol text = (
case
when op = 'eq' then '='
when op = 'neq' then '!='
when op = 'lt' then '<'
when op = 'lte' then '<='
when op = 'gt' then '>'
when op = 'gte' then '>='
else 'UNKNOWN OP'
end
);
res boolean;
begin
execute format('select %L::'|| type_::text || ' ' || op_symbol || ' %L::'|| type_::text, val_1, val_2) into res;
return res;
end;
$$;
ALTER FUNCTION realtime.check_equality_op(op realtime.equality_op, type_ regtype, val_1 text, val_2 text) OWNER TO supabase_admin;
--
-- Name: is_visible_through_filters(realtime.wal_column[], realtime.user_defined_filter[]); Type: FUNCTION; Schema: realtime; Owner: supabase_admin
--