/
load_sql_context.sql
445 lines (440 loc) · 22.1 KB
/
load_sql_context.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
with search_path_oids(schema_oid) as (
select y::regnamespace::oid from unnest(current_schemas(false)) x(y)
),
schemas_with_privilege(oid, name) as (
-- Schema segregated multi-tenant environments need a shortcut to
-- exclude schemas without USAGE permission so context reload can ignore
-- irreleavnt tenants
-- https://github.com/supabase/pg_graphql/pull/480
select
pn.oid::int,
pn.nspname::text
from
pg_namespace pn
where
pg_catalog.has_schema_privilege(
current_user,
pn.oid,
'USAGE'
)
),
schemas_(oid, name) as (
select
pn.oid,
pn.name
from
schemas_with_privilege pn
-- filter to current schemas only
join search_path_oids cur_schemas(oid)
on pn.oid = cur_schemas.oid
)
select
jsonb_build_object(
'config', jsonb_build_object(
'search_path', (select array_agg(schema_oid) from search_path_oids),
'role', current_role,
'schema_version', graphql.get_schema_version()
),
'enums', coalesce(
(
-- CTE is for performance. Issue #321
with enums_(type_oid, enum_record) as (
select
pt.oid,
jsonb_build_object(
'oid', pt.oid::int,
'schema_oid', min(pt.typnamespace)::int,
'name', min(pt.typname),
'comment', pg_catalog.obj_description(pt.oid, 'pg_type'),
'directives', jsonb_build_object(
'mappings', graphql.comment_directive(pg_catalog.obj_description(pt.oid, 'pg_type')) -> 'mappings',
'name', graphql.comment_directive(pg_catalog.obj_description(pt.oid, 'pg_type')) ->> 'name'
),
'values', jsonb_agg(
jsonb_build_object(
'oid', pe.oid::int,
'name', pe.enumlabel,
'sort_order', (pe.enumsortorder * 100000)::int
)
order by pe.enumsortorder asc
),
'permissions', jsonb_build_object(
'is_usable', pg_catalog.has_type_privilege(current_user, pt.oid, 'USAGE')
)
)
from
pg_enum pe
join pg_type pt
on pt.oid = pe.enumtypid
join schemas_with_privilege swp
on pt.typnamespace = swp.oid
group by
pt.oid
)
select
jsonb_object_agg(enums_.type_oid, enums_.enum_record)
from
enums_
),
jsonb_build_object()
),
'types', coalesce(
(
select
jsonb_object_agg(
pt.oid::int,
jsonb_build_object(
'oid', pt.oid::int,
'schema_oid', pt.typnamespace::int,
'name', pt.typname,
'category', case
when pt.typcategory = 'A' then 'Array'
when pt.typcategory = 'E' then 'Enum'
when pt.typcategory = 'C'
and tabs.relkind in ('r', 't', 'v', 'm', 'f', 'p') then 'Table'
when pt.typcategory = 'C' and tabs.relkind = 'c' then 'Composite'
else 'Other'
end,
-- if category is 'Array', points at the underlying element type
'array_element_type_oid', nullif(pt.typelem::int, 0),
-- if category is 'Table' points to the table oid
'table_oid', tabs.oid::int,
'comment', pg_catalog.obj_description(pt.oid, 'pg_type'),
'permissions', jsonb_build_object(
'is_usable', pg_catalog.has_type_privilege(current_user, pt.oid, 'USAGE')
)
)
)
from
pg_type pt
join schemas_with_privilege swp
on pt.typnamespace = swp.oid
left join pg_class tabs
on pt.typrelid = tabs.oid
),
jsonb_build_object()
),
'composites', coalesce(
(
select
jsonb_agg(
jsonb_build_object(
'oid', pt.oid::int,
'schema_oid', pt.typnamespace::int
)
)
from
pg_type pt
join pg_class tabs
on pt.typrelid = tabs.oid
join schemas_with_privilege swp
on pt.typnamespace = swp.oid
where
pt.typcategory = 'C'
and tabs.relkind = 'c'
),
jsonb_build_array()
),
'foreign_keys', (
coalesce(
(
select
jsonb_agg(
jsonb_build_object(
'local_table_meta', jsonb_build_object(
'oid', pf.conrelid::int,
'name', pa_local.relname::text,
'schema', pa_local.relnamespace::regnamespace::text,
'column_names', (
select
array_agg(pa.attname order by pfck.attnum_ix asc)
from
unnest(pf.conkey) with ordinality pfck(attnum, attnum_ix)
join pg_attribute pa
on pfck.attnum = pa.attnum
where
pa.attrelid = pf.conrelid
)
),
'referenced_table_meta', jsonb_build_object(
'oid', pf.confrelid::int,
'name', pa_referenced.relname::text,
'schema', pa_referenced.relnamespace::regnamespace::text,
'column_names', (
select
array_agg(pa.attname order by pfck.attnum_ix asc)
from
unnest(pf.confkey) with ordinality pfck(attnum, attnum_ix)
join pg_attribute pa
on pfck.attnum = pa.attnum
where
pa.attrelid = pf.confrelid
)
),
'directives', jsonb_build_object(
'local_name', graphql.comment_directive(pg_catalog.obj_description(pf.oid, 'pg_constraint')) ->> 'local_name',
'foreign_name', graphql.comment_directive(pg_catalog.obj_description(pf.oid, 'pg_constraint')) ->> 'foreign_name'
)
)
)
from
pg_catalog.pg_constraint pf
join pg_class pa_local
on pf.conrelid = pa_local.oid
join pg_class pa_referenced
on pf.confrelid = pa_referenced.oid
-- Referenced tables must also be on the search path
join search_path_oids spo
on pa_referenced.relnamespace = spo.schema_oid
where
pf.contype = 'f' -- foreign key
),
jsonb_build_array()
)
),
'schemas', coalesce(
(
select
jsonb_object_agg(
pn.oid::int,
jsonb_build_object(
'oid', pn.oid::int,
'name', pn.name,
'comment', pg_catalog.obj_description(pn.oid, 'pg_namespace'),
'directives', jsonb_build_object(
'inflect_names', coalesce(
(graphql.comment_directive(pg_catalog.obj_description(pn.oid, 'pg_namespace')) -> 'inflect_names') = to_jsonb(true),
false
),
'max_rows', coalesce(
(graphql.comment_directive(pg_catalog.obj_description(pn.oid, 'pg_namespace')) ->> 'max_rows')::int,
30
)
)
)
)
from
schemas_ pn
join schemas_with_privilege swp
on pn.oid = swp.oid
),
jsonb_build_object()
),
'tables', coalesce(
(
select
jsonb_object_agg(
pc.oid::int,
jsonb_build_object(
'oid', pc.oid::int,
'name', pc.relname::text,
'relkind', pc.relkind::text,
'reltype', pc.reltype::int,
'schema', schemas_.name,
'schema_oid', pc.relnamespace::int,
'comment', pg_catalog.obj_description(pc.oid, 'pg_class'),
'directives', (
with directives(directive) as (
select graphql.comment_directive(pg_catalog.obj_description(pc.oid, 'pg_class'))
)
select
jsonb_build_object(
'name', d.directive ->> 'name',
'description', d.directive -> 'description',
'total_count', jsonb_build_object(
'enabled', coalesce(
(
d.directive -> 'totalCount' ->> 'enabled' = 'true'
),
false
)
),
'primary_key_columns', d.directive -> 'primary_key_columns',
'foreign_keys', d.directive -> 'foreign_keys'
)
from
directives d
),
'indexes', coalesce(
(
select
jsonb_agg(
jsonb_build_object(
'table_oid', pi.indrelid::int,
'column_names', coalesce(
(
select
array_agg(pa_i.attname)
from
unnest(pi.indkey) pic(attnum)
join pg_catalog.pg_attribute pa_i
on pa_i.attrelid = pi.indrelid -- same table
and pic.attnum = pa_i.attnum -- same attribute
),
array[]::text[]
),
'is_unique', pi.indisunique,
'is_primary_key', pi.indisprimary
)
)
from
pg_catalog.pg_index pi
where
pi.indrelid = pc.oid
),
jsonb_build_array()
),
'columns', (
select
jsonb_agg(
jsonb_build_object(
'name', pa.attname::text,
'type_oid', pa.atttypid::int,
-- includes type mod info like char(4)
'type_name', pg_catalog.format_type(pa.atttypid, pa.atttypmod),
-- char, bpchar, varchar, char[], bpchar[], carchar[]
-- the -4 removes the byte for the null terminated str
'max_characters', nullif(pa.atttypmod, -1) - 4,
'schema_oid', schemas_.oid::int,
'is_not_null', pa.attnotnull,
'attribute_num', pa.attnum,
'has_default', pd.adbin is not null, -- pg_get_expr(pd.adbin, pd.adrelid) shows expression
'is_serial', pg_get_serial_sequence(pc.oid::regclass::text, pa.attname::text) is not null,
'is_generated', pa.attgenerated <> ''::"char",
'permissions', jsonb_build_object(
'is_insertable', pg_catalog.has_column_privilege(
current_user,
pa.attrelid,
pa.attname,
'INSERT'
),
'is_selectable', pg_catalog.has_column_privilege(
current_user,
pa.attrelid,
pa.attname,
'SELECT'
),
'is_updatable', pg_catalog.has_column_privilege(
current_user,
pa.attrelid,
pa.attname,
'UPDATE'
)
),
'comment', pg_catalog.col_description(pc.oid, pa.attnum),
'directives', (
with directives(directive) as (
select graphql.comment_directive(pg_catalog.col_description(pc.oid, pa.attnum))
)
select
jsonb_build_object(
'name', d.directive ->> 'name',
'description', d.directive -> 'description'
)
from
directives d
)
)
order by pa.attnum
)
from
pg_catalog.pg_attribute pa
left join pg_catalog.pg_attrdef pd
on (pa.attrelid, pa.attnum) = (pd.adrelid, pd.adnum)
where
pc.oid = pa.attrelid
and pa.attnum > 0
and not pa.attisdropped
),
'permissions', jsonb_build_object(
'is_insertable', pg_catalog.has_table_privilege(
current_user,
pc.oid,
'INSERT'
),
'is_selectable', pg_catalog.has_table_privilege(
current_user,
pc.oid,
'SELECT'
),
'is_updatable', pg_catalog.has_table_privilege(
current_user,
pc.oid,
'UPDATE'
),
'is_deletable', pg_catalog.has_table_privilege(
current_user,
pc.oid,
'DELETE'
)
)
)
)
from
pg_class pc
join schemas_
on pc.relnamespace = schemas_.oid
where
pc.relkind in (
'r', -- table
'v', -- view
'm', -- mat view
'f' -- foreign table
)
),
jsonb_build_object()
),
'functions', coalesce(
(
select
jsonb_agg(
jsonb_build_object(
'oid', pp.oid::int,
'name', pp.proname::text,
'type_oid', pp.prorettype::oid::int,
'type_name', pp.prorettype::regtype::text,
'schema_oid', pronamespace::int,
'schema_name', pronamespace::regnamespace::text,
'arg_types', proargtypes::int[],
'arg_names', proargnames::text[],
'arg_defaults', pg_get_expr(proargdefaults, 0)::text,
'num_args', pronargs,
'num_default_args', pronargdefaults,
'arg_type_names', pp.proargtypes::regtype[]::text[],
'volatility', pp.provolatile,
-- Functions may be defined as "returns sefof <entity> rows 1"
-- those should return a single record, not a connection
-- this is important because set returning functions are inlined
-- and returning a single record isn't.
'is_set_of', pp.proretset::bool and pp.prorows <> 1,
'n_rows', pp.prorows::int,
'comment', pg_catalog.obj_description(pp.oid, 'pg_proc'),
'directives', (
with directives(directive) as (
select graphql.comment_directive(pg_catalog.obj_description(pp.oid, 'pg_proc'))
)
select
jsonb_build_object(
'name', d.directive ->> 'name',
'description', d.directive ->> 'description'
)
from
directives d
),
'permissions', jsonb_build_object(
'is_executable', pg_catalog.has_function_privilege(
current_user,
pp.oid,
'EXECUTE'
)
)
)
)
from
pg_catalog.pg_proc pp
join search_path_oids spo
on pp.pronamespace = spo.schema_oid
),
jsonb_build_array()
)
)