-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.sql
2262 lines (2130 loc) · 67 KB
/
build.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
do
$install$
declare _t text;
begin
/* #region init */
if exists(select 1 as name from pg_namespace where nspname = 'schema') then
--drop schema schema cascade;
raise exception 'Schema "schema" already exists. Consider running "drop schema schema cascade;" to recreate schema schema.';
end if;
create schema schema;
/* #endregion init */
/* #region tables */
create view schema.tables as
select
sub.table_oid,
sub.schema_name,
sub.table_name,
coalesce(
array_agg(pk_att.attname::text order by pk_att.attnum) filter (where pk_att.attname is not null),
array[]::text[]
) as pk_column_names,
coalesce(
array_agg(pk_att.atttypid::regtype::text order by pk_att.attnum) filter (where pk_att.attname is not null),
array[]::text[]
) as pk_column_types,
coalesce(
array_agg(fk_att.attname::text order by fk_att.attnum) filter (where fk_att.attname is not null),
array[]::text[]
) as fk_column_names,
coalesce(
array_agg(fk_att.atttypid::regtype::text order by pk_att.attnum) filter (where fk_att.attname is not null),
array[]::text[]
) as fk_column_types,
coalesce(
array_agg(ref_cl.oid order by fk_att.attnum) filter (where fk_att.attname is not null),
array[]::oid[]
) as ref_table_oids,
coalesce(
array_agg(ref_cl.relnamespace::regnamespace::text order by fk_att.attnum) filter (where fk_att.attname is not null),
array[]::text[]
) as ref_schema_names,
coalesce(
array_agg(ref_cl.relname::text order by fk_att.attnum) filter (where fk_att.attname is not null),
array[]::text[]
) as ref_table_names,
coalesce(
array_agg(ref_att.attname::text order by fk_att.attnum) filter (where fk_att.attname is not null),
array[]::text[]
) as ref_column_names,
coalesce(
array_agg(ref_att.atttypid::regtype::text order by pk_att.attnum) filter (where fk_att.attname is not null),
array[]::text[]
) as ref_column_types
from (
select
cl.oid as table_oid,
cl.relnamespace::regnamespace::text as schema_name,
cl.relname::text as table_name,
con.contype,
con.confrelid as fk_id,
unnest(con.conkey) as column_attnum,
unnest(con.confkey) as fk_column_attnum
from
pg_catalog.pg_class cl
left join pg_catalog.pg_constraint con on cl.oid = con.conrelid
where
cl.relnamespace::regnamespace::text not like 'pg_%'
and (con.contype = 'p' or con.contype = 'f')
) sub
left join pg_attribute pk_att
on sub.table_oid = pk_att.attrelid and sub.column_attnum = pk_att.attnum and sub.contype = 'p'
left join pg_attribute fk_att
on sub.table_oid = fk_att.attrelid and sub.column_attnum = fk_att.attnum and sub.contype = 'f'
left join pg_class ref_cl on sub.fk_id = ref_cl.oid
left join pg_attribute ref_att
on sub.fk_id = ref_att.attrelid and sub.fk_column_attnum = ref_att.attnum and sub.contype = 'f'
group by
sub.table_oid,
sub.schema_name,
sub.table_name;
/* #endregion tables */
/* #region _prepare_params */
create function schema._prepare_params(
inout _schema text,
inout _type text,
inout _search text
)
returns record
language plpgsql
as
$$
begin
_schema = lower(trim(_schema));
_type = lower(trim(_type));
_search = lower(trim(_search));
if _schema = '' then
_schema = null;
end if;
if _type = '' then
_type = null;
end if;
if _search = '' then
_search = null;
end if;
end;
$$;
/* #endregion _prepare_params */
/* #region _get_schema_array */
create function schema._get_schema_array(text)
returns text[]
language sql
as
$$
select
array_agg(nspname::text)
from
pg_namespace
where
nspname not like 'pg_%'
and nspname <> 'information_schema'
/* and nspname <> 'schema' */
and ($1 is null or nspname similar to $1);
$$;
/* #endregion _get_schema_array */
/* #region _parse_column */
create function schema._parse_column(_c record)
returns text
language plpgsql
as
$$
declare
_identity text;
_rec record;
begin
if _c.is_identity = 'YES' then
select
seq.seqstart as start_value,
seq.seqmin as min_value,
seq.seqmax as max_value,
seq.seqincrement as increment_by,
seq.seqcycle as cycle,
seq.seqcache as cache_size
into _rec
from
pg_catalog.pg_class cl
join pg_catalog.pg_sequence seq on seq.seqrelid = cl.relfilenode
join pg_catalog.pg_depend dep on seq.seqrelid = dep.objid
join pg_catalog.pg_class depcl on dep.refobjid = depcl.relfilenode
join pg_catalog.pg_attribute attrib on attrib.attnum = dep.refobjsubid and attrib.attrelid = dep.refobjid
where
depcl.relnamespace::regnamespace::text = _c.table_schema
and depcl.relname = _c.table_name
and attrib.attname = _c.column_name
limit 1;
_identity = concat(
'GENERATED ',
_c.identity_generation,
' AS IDENTITY (INCREMENT ',
coalesce(_rec.increment_by::text, _c.identity_increment::text),
' START ',
coalesce(_rec.start_value::text, _c.identity_start::text),
' MINVALUE ',
coalesce(_rec.min_value::text, _c.identity_minimum::text),
' MAXVALUE ',
coalesce(_rec.max_value::text, _c.identity_maximum::text),
case when _rec is not null and _rec.cache_size <> 1
then ' CACHE ' || coalesce(_rec.cache_size::text, '1')
else ''
end,
case when _rec.cycle is true or _c.identity_cycle = 'YES' then ' CYCLE)' else ')' end
);
end if;
return concat(
case when _c.is_nullable = 'NO' then 'NOT NULL ' else '' end,
case
when _identity is not null then _identity
when _c.is_generated <> 'NEVER' then 'GENERATED ' || _c.is_generated || ' AS ' || _c.generation_expression
when _c.column_default is not null then 'DEFAULT ' || _c.column_default
end
);
end;
$$;
/* #endregion _parse_column */
/* #region _parse_return */
create function schema._parse_return(_oid oid, _specific_schema text, _specific_name text)
returns text
language plpgsql
as
$$
declare
_result text;
begin
_result = pg_get_function_result(_oid);
if _result = 'record' then
_result = 'TABLE(' || E'\n ' || (
select string_agg(quote_ident(p.parameter_name) || ' ' || (p.udt_schema || '.' || p.udt_name)::regtype::text, E'\n ' order by p.ordinal_position)
from information_schema.parameters p
where p.specific_name = _specific_name and p.specific_schema = _specific_schema and p.parameter_mode = 'OUT'
) || E'\n)';
end if;
return _result;
end;
$$;
/* #endregion _parse_return */
/* #region _ident */
create function schema._ident(text, text, boolean = false)
returns text
language sql
as
$$
select
case
when $3 then
case when $1 = 'public' then '' else quote_ident($1) || '.' end || quote_ident($2)
else
quote_ident($1) || '.' || quote_ident($2)
end
$$;
/* #endregion _ident */
/* #region _parse_routine_body */
create function schema._parse_routine_body(_body text, _signature text, _comment text)
returns text
language plpgsql
as
$$
declare
_line text;
_quote text = '$' || '$';
_count int = 0;
begin
foreach _line in array string_to_array(_body, E'\n') loop
if position(_quote in _line) > 0 then
_count = _count + 1;
_quote = '$' || repeat('_', _count) || '$';
end if;
end loop;
return concat(
_quote,
_body,
_quote,
';',
case when _comment is not null then
concat(
E'\n\n'
'COMMENT ON ',
_signature,
' IS ',
schema._quote(_comment),
';'
)
else
''
end
);
end;
$$;
/* #endregion _parse_routine_body */
/* #region _create_table_temp_tables */
create function schema._create_table_temp_tables(
_schemas text[],
_create_columns boolean = true,
_create_constraints boolean = true,
_create_indexes boolean = true,
_create_triggers boolean = true,
_create_policies boolean = true,
_create_sequences boolean = true
)
returns void
language plpgsql
as
$$
begin
if _create_columns then
create temp table _columns on commit drop as
select
t.type,
t.schema,
t.name,
t.table_schema,
t.table_name,
t.order_by,
t.comment,
t.short_definition,
t.definition
from schema._columns(_schemas) t;
end if;
if _create_constraints then
create temp table _constraints on commit drop as
select
t.type,
t.order_by,
t.schema,
t.name,
t.table_schema,
t.table_name,
t.comment,
t.short_definition,
t.definition
from schema._constraints(_schemas) t;
end if;
if _create_indexes then
create temp table _indexes on commit drop as
select
t.type,
t.schema,
t.name,
t.table_schema,
t.table_name,
t.comment,
t.definition
from schema._indexes(_schemas) t;
end if;
if _create_triggers then
create temp table _triggers on commit drop as
select
t.type,
t.schema,
t.name,
t.table_schema,
t.table_name,
t.comment,
t.definition
from schema._triggers(_schemas) t;
end if;
if _create_policies then
create temp table _policies on commit drop as
select
t.type,
t.schema,
t.name,
t.table_schema,
t.table_name,
t.comment,
t.definition
from schema._policies(_schemas) t;
end if;
if _create_sequences then
create temp table _sequences on commit drop as
select
t.type,
t.schema,
t.name,
t.table_schema,
t.table_name,
t.column_name,
t.comment,
t.definition
from schema._sequences(_schemas) t;
end if;
end;
$$;
/* #endregion _create_table_temp_tables */
/* #region _search_filter */
create or replace function schema._search_filter(
_record record,
_type text,
_search text
)
returns boolean
language plpgsql
as
$$
begin
return (
_type is null
or _record.type = _type
or _record.type similar to _type
)
and (
_search is null
or _record.name = _search
or _record.definition = _search
or _record.comment = _search
or lower(_record.name) similar to _search
or lower(_record.definition) similar to _search
or lower(_record.comment) similar to _search
);
end;
$$;
/* #endregion _search_filter */
/* #region _quote */
create function schema._quote(text)
returns text
language sql
immutable
as
$$
select case when position(E'\'' in $1) > 0 then 'E''' || replace($1, E'\'', '\''') || E'\'' else E'\'' || $1 || E'\'' end
$$;
/* #endregion _quote */
/* #region _aggregates */
create function schema._aggregates(_schemas text[])
returns table (
type text,
schema text,
name text,
comment text,
definition text
)
language sql
as
$$
select
sub.type,
sub.schema,
sub.name,
sub.comment,
concat(
sub.definition,
case when sub.comment is null then '' else
concat(
E'\n',
'COMMENT ON AGGREGATE ',
sub.signature,
' IS ',
schema._quote(sub.comment),
E';'
)
end
)
from (
select
'aggregate' as type,
n.nspname as schema,
p.proname as name,
pg_catalog.obj_description(p.oid, 'pg_proc') as comment,
format('%I.%I(%s)', n.nspname, p.proname, format_type(a.aggtranstype, null)) as signature,
format(
E'CREATE AGGREGATE %I.%I(%s) (\n%s\n);',
n.nspname,
p.proname,
format_type(a.aggtranstype, null),
array_to_string(
array[
format(' SFUNC = %s', a.aggtransfn::regproc),
format(' STYPE = %s', format_type(a.aggtranstype, NULL)),
case a.aggfinalfn when '-'::regproc then null else format(' FINALFUNC = %s', a.aggfinalfn::text) end,
case a.aggsortop when 0 then null else format(' SORTOP = %s', op.oprname) end,
case when a.agginitval is null then null else format(' INITCOND = %s', a.agginitval) end
], E',\n'
)
) as definition
from
pg_catalog.pg_proc p
join pg_catalog.pg_namespace n on p.pronamespace = n.oid
join pg_catalog.pg_aggregate a on p.oid = a.aggfnoid::regproc::oid
left join pg_catalog.pg_operator op on op.oid = a.aggsortop
where
n.nspname = any(_schemas) and p.prokind = 'a'
) sub
$$;
/* #endregion _aggregates */
/* #region _ranges */
create function schema._ranges(_schemas text[])
returns table (
type text,
schema text,
name text,
comment text,
definition text
)
language sql
as
$$
select
'range' as type,
sub.schema,
sub.name,
sub.comment,
concat(
'CREATE TYPE ',
schema._ident(sub.schema, sub.name),
E' AS RANGE (\n',
' SUBTYPE = ', SUBTYPE,
case when SUBTYPE_OPCLASS <> '-' and SUBTYPE_OPCLASS not like 'pg_%' then E',\n SUBTYPE_OPCLASS = ' || SUBTYPE_OPCLASS else '' end,
case when sub.COLLATION <> '-' and sub.COLLATION not like 'pg_%' then E',\n COLLATION = ' || sub.COLLATION else '' end,
case when CANONICAL <> '-' and CANONICAL not like 'pg_%' then E',\n CANONICAL = ' || CANONICAL else '' end,
case when SUBTYPE_DIFF <> '-' and SUBTYPE_DIFF not like 'pg_%' then E',\n SUBTYPE_DIFF = ' || SUBTYPE_DIFF else '' end,
case when MULTIRANGE_TYPE_NAME <> '-' and MULTIRANGE_TYPE_NAME not like 'pg_%' then E',\n MULTIRANGE_TYPE_NAME = ' || MULTIRANGE_TYPE_NAME else '' end,
E'\n);',
case when sub.comment is null then '' else
concat(
E'\n',
'COMMENT ON TYPE ',
schema._ident(sub.schema, sub.name),
' IS ',
schema._quote(sub.comment),
E';'
)
end
) as definition
from (
select
n.nspname::text as schema,
t.typname::text as name,
pg_catalog.obj_description(t.oid, 'pg_type') as comment,
r.rngsubtype::regtype::text as SUBTYPE,
r.rngsubopc::regtype::text as SUBTYPE_OPCLASS,
r.rngcollation::regtype::text as COLLATION,
r.rngcanonical::regproc::text as CANONICAL,
r.rngsubdiff::regproc::text as SUBTYPE_DIFF,
r.rngmultitypid::regtype::text as MULTIRANGE_TYPE_NAME
from
pg_catalog.pg_type t
join pg_catalog.pg_namespace n on n.oid = t.typnamespace
join pg_catalog.pg_range r on t.oid = r.rngtypid
where
t.typtype = 'r'
and n.nspname = any(_schemas)
) sub
$$;
/* #endregion _ranges */
/* #region _routines */
create function schema._routines(_schemas text[])
returns table (
type text,
schema text,
name text,
specific_name text,
comment text,
definition text
)
language sql
as
$$
select
r.type,
r.schema,
r.name,
r.specific_name,
r.comment,
concat(
'CREATE ',
r.routine_type,
' ',
r.signature, E'\n',
'RETURNS ', r.returns, E'\n',
'LANGUAGE ', lower(r.language), E'\n',
case when r.security_type <> 'INVOKER' then 'SECURITY ' || r.security_type || E'\n' else '' end,
--case when r.is_deterministic = 'YES' then E'IMMUTABLE\n' else '' end,
case when r.volatility = 'i' then E'IMMUTABLE\n'
when r.volatility = 's' then E'STABLE\n'
when r.volatility = 'v' then E''
else '' end,
case when r.parallel_option = 'u' then ''
when r.parallel_option = 's' then E'PARALLEL SAFE\n'
when r.parallel_option = 'r' then E'PARALLEL RESTRICTED\n'
else '' end,
case when r.cost_num = 100 then '' else 'COST ' || r.cost_num::text || E'\n' end,
case when r.rows_num = 1000 or r.rows_num = 0 then '' else 'ROWS ' || r.rows_num::text || E'\n' end,
case when r.is_strict then E'STRICT\n' else '' end,
E'AS', E'\n',
schema._parse_routine_body(r.routine_definition, r.type || ' ' || r.name, r.comment),
E'\n'
) as definition
from (
select
r.routine_type,
lower(r.routine_type) as type,
quote_ident(r.specific_schema::text) as schema,
concat(
quote_ident(r.routine_name),
'(',
array_to_string(array_agg((p.udt_schema || '.' || p.udt_name)::regtype::text order by p.ordinal_position), ', '),
')'
) as name,
r.specific_name,
concat(
schema._ident(r.specific_schema, r.routine_name),
'(',
string_agg(
E'\n ' ||
case when p.parameter_name is null then '' else quote_ident(p.parameter_name) end ||
' ' ||
(p.udt_schema || '.' || p.udt_name)::regtype::text ||
case when p.parameter_default is not null then ' = ' || p.parameter_default else '' end,
', ' order by p.ordinal_position
),
E'\n)'
) as signature,
pgdesc.description as comment,
lower(r.external_language) as language,
r.security_type,
r.is_deterministic,
schema._parse_return(proc.oid, r.specific_schema, r.specific_name) as returns,
r.routine_definition,
proc.proisstrict as is_strict,
proc.provolatile as volatility,
procost as cost_num,
prorows as rows_num,
proparallel as parallel_option
from
information_schema.routines r
join pg_catalog.pg_proc proc
on r.specific_name = proc.proname || '_' || proc.oid
left join information_schema.parameters p
on r.specific_name = p.specific_name and r.specific_schema = p.specific_schema and (p.parameter_mode = 'IN' or p.parameter_mode = 'INOUT')
left join pg_catalog.pg_description pgdesc
on proc.oid = pgdesc.objoid
where
r.specific_schema = any(_schemas)
and proc.prokind in ('f', 'p')
and not lower(r.external_language) = any(array['c', 'internal'])
group by
r.specific_schema, r.specific_name, r.routine_type, r.external_language, r.routine_name,
r.data_type, r.type_udt_catalog, r.type_udt_schema, r.type_udt_name,
pgdesc.description, proc.proretset, r.routine_definition, proc.oid, r.security_type, r.is_deterministic
) r
order by
type desc, name
$$;
/* #endregion _routines */
/* #region _routines_order */
create function schema._routines_order(_schemas text[])
returns table (
specific_schema text,
specific_name text,
order_by numeric
)
language sql
as
$$
with recursive cte as (
select
r.specific_schema,
r.specific_name,
r.external_language,
u.routine_schema,
u.routine_name,
1::numeric - (case when r.external_language = 'SQL' then 0 else 0.5 end) as order_by
from
information_schema.routines r
left join information_schema.routine_routine_usage u using (specific_schema, specific_name)
where
u.routine_schema is null and r.specific_schema = any(_schemas)
union all
select
r.specific_schema,
r.specific_name,
r.external_language,
u.routine_schema,
u.routine_name,
cte.order_by + 1 - (case when r.external_language = 'SQL' then 0 else 0.5 end) as order_by
from
information_schema.routines r
join information_schema.routine_routine_usage u
on r.specific_schema = u.specific_schema and r.specific_name = u.specific_name
join cte
on u.routine_schema = cte.specific_schema and u.routine_name = cte.specific_name
where
r.specific_schema = any(_schemas)
)
select
cte.specific_schema,
cte.specific_name,
cte.order_by
from cte
$$;
/* #endregion _routines_order */
/* #region _constraints */
create function schema._constraints(_schemas text[])
returns table (
type text,
order_by int,
schema text,
name text,
table_schema text,
table_name text,
comment text,
short_definition text,
definition text
)
language sql
as
$$
select
sub.type,
sub.order_by,
sub.schema,
sub.name,
sub.schema as table_schema,
sub.table_name,
null::text as comment,
concat(
'CONSTRAINT ',
sub.name,
' ',
sub.definition
) as short_definition,
concat(
'ALTER TABLE ONLY ',
quote_ident(sub.schema),
'.',
quote_ident(sub.table_name),
' ADD CONSTRAINT ',
sub.name,
' ',
sub.definition,
';'
) as definition
from (
select
case
when contype = 'p' then 'primary key constraint'
when contype = 'u' then 'unique constraint'
when contype = 'c' then 'check constraint'
when contype = 'f' then 'foreign key constraint'
when contype = 't' then 'trigger constraint'
when contype = 'x' then 'exclusion constraint'
else 'unknown constraint'
end as type,
case
when contype = 'p' then 1
when contype = 'u' then 2
when contype = 'c' then 3
when contype = 'f' then 4
else 5
end as order_by,
conname::text as name,
connamespace::regnamespace::text as schema,
replace(
case when position('.' in conrelid::regclass::text) > 0
then split_part(conrelid::regclass::text, '.', 2)
else conrelid::regclass::text
end, '"', ''
) as table_name,
pg_get_constraintdef(oid, true) as definition
from
pg_catalog.pg_constraint
where
connamespace::regnamespace::text = any(_schemas)
) sub
$$;
/* #endregion _constraints */
/* #region _indexes */
create function schema._indexes(_schemas text[])
returns table (
type text,
schema text,
name text,
table_schema text,
table_name text,
comment text,
definition text
)
language sql
as
$$
select
case when position(' UNIQUE ' in indexdef) > 0 then 'unique ' else ''end ||
split_part(split_part(indexdef, 'USING ', 2), ' ', 1) || ' ' ||
'index' as type,
i.schemaname::text as schema,
i.indexname::text as name,
i.schemaname::text as table_schema,
i.tablename::text as table_name,
null::text as comment,
indexdef || ';' as definition
from
pg_indexes i
where
i.schemaname = any(_schemas)
$$;
/* #endregion _indexes */
/* #region _triggers */
create function schema._triggers(_schemas text[])
returns table (
type text,
schema text,
name text,
table_schema text,
table_name text,
comment text,
definition text
)
language sql
as
$$
select
'trigger' as type,
trigger_schema::text as schema,
trigger_name::text as name,
event_object_schema::text as table_schema,
event_object_table::text as table_name,
null::text as comment,
pg_get_triggerdef(oid) || ';' as definition
from
(
select distinct trigger_schema, trigger_name, event_object_schema, event_object_table
from information_schema.triggers
where
event_object_schema = any(_schemas)
) tr
join pg_trigger pg
on tr.trigger_name = pg.tgname
and (quote_ident(event_object_schema) || '.' || quote_ident(event_object_table))::regclass::oid = tgrelid
$$;
/* #endregion _triggers */
/* #region _policies */
create function schema._policies(_schemas text[])
returns table (
type text,
schema text,
name text,
table_schema text,
table_name text,
comment text,
definition text
)
language sql
as
$$
select
'policy' as type,
p.schema,
p.name,
p.schema as table_schema,
p.table_name,
null::text as comment,
concat(
'CREATE POLICY ',
quote_ident(p.name),
' ON ',
quote_ident(p.schema), '.', quote_ident(p.table_name),
' AS ',
permissive,
' FOR ',
for_cmd,
' TO ',
roles,
' ', using_exp, ' (',
exp, ');'
) as definition
from (
select
n.nspname::text as schema,
pol.polname::text as name,
c.relname::text as table_name,
case when pol.polpermissive then 'PERMISSIVE'::text else 'RESTRICTIVE'::text end as permissive,
case when pol.polroles = '{0}'::oid[] then 'public' else (
select string_agg(pg_authid.rolname, ', ')
from pg_authid
where pg_authid.oid = any(pol.polroles)
)
end as roles,
case pol.polcmd
when 'r'::"char" then 'SELECT'
when 'a'::"char" then 'INSERT'
when 'w'::"char" then 'UPDATE'
when 'd'::"char" then 'DELETE'
when '*'::"char" then 'ALL'
end::text as for_cmd,
case when pol.polwithcheck is not null and pol.polqual is null then 'WITH CHECK' else 'USING' end as using_exp,
case
when pol.polwithcheck is not null and pol.polqual is null
then pg_get_expr(pol.polwithcheck, pol.polrelid)
else pg_get_expr(pol.polqual, pol.polrelid)
end as exp
from pg_policy pol
join pg_class c on c.oid = pol.polrelid
left join pg_namespace n on n.oid = c.relnamespace
where
n.nspname = any(_schemas)
) p
$$;
/* #endregion _policies */
/* #region _columns */
create function schema._columns(_schemas text[])
returns table (
type text,
schema text,
name text,
table_schema text,
table_name text,
order_by int,
comment text,
short_definition text,
definition text
)
language sql
as
$$
select
sub.type,
sub.schema,
sub.name,
sub.table_schema,
sub.table_name,
sub.order_by,
sub.comment,
sub.definition as short_definition,
concat(
'ALTER TABLE ',
schema._ident(sub.table_schema, sub.table_name),
' ADD COLUMN ',
sub.definition,
';',
case when sub.comment is not null then concat(
E'\n',
'COMMENT ON COLUMN ',
schema._ident(sub.table_schema, sub.table_name), '.', quote_ident(sub.name),
' IS ',
schema._quote(sub.comment),
';'
) else '' end
) as definition
from (
select
'column' as type,
c.table_schema::text as schema,
c.column_name::text as name,
c.table_schema::text as table_schema,
c.table_name::text as table_name,
c.ordinal_position as order_by,
pgdesc.description as comment,
concat(
quote_ident(c.column_name),
' ',
(c.udt_schema || '.' || c.udt_name)::regtype::text,
' ',
schema._parse_column(c)
) as definition
from
information_schema.columns c
join information_schema.tables t
on c.table_schema = t.table_schema and c.table_name = t.table_name and t.table_type = 'BASE TABLE'
left outer join pg_catalog.pg_statio_user_tables pgtbl
on pgtbl.schemaname = c.table_schema and c.table_name = pgtbl.relname
left outer join pg_catalog.pg_description pgdesc
on pgtbl.relid = pgdesc.objoid and c.ordinal_position = pgdesc.objsubid
where
c.table_schema = any(_schemas)
) sub
$$;
/* #endregion _columns */
/* #region _tables_full */
create function schema._tables_full(_schemas text[])
returns table (
type text,
schema text,
name text,
comment text,
definition text
)
language plpgsql
as
$$
begin
perform schema._create_table_temp_tables(
_schemas => _schemas,
_create_columns => not schema._temp_exists('_columns'),
_create_constraints => not schema._temp_exists('_constraints'),
_create_indexes => not schema._temp_exists('_indexes'),
_create_triggers => not schema._temp_exists('_triggers'),
_create_policies => not schema._temp_exists('_policies'),
_create_sequences => not schema._temp_exists('_sequences')
);
return query
select
'table' as type,
t.table_schema::text as schema,
t.table_name::text as name,
pgdesc.description as comment,
concat(
'CREATE TABLE ',
schema._ident(t.table_schema, t.table_name),