Skip to content

Commit 95bdd26

Browse files
authored
refactor: make the function that queries all tables more reusable
- Merges postgrest_get_all_tables and postgrest_get_all_composite_types - Removes the need of using unnest(), making the queries a bit more performant
1 parent 040f8ef commit 95bdd26

File tree

3 files changed

+209
-459
lines changed

3 files changed

+209
-459
lines changed

sql/components.sql

Lines changed: 77 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,84 @@ $$;
1717
create or replace function oas_build_component_schemas(schemas text[])
1818
returns jsonb language sql stable as
1919
$$
20-
select oas_build_component_schemas_from_tables(schemas) ||
21-
oas_build_component_schemas_from_composite_types(schemas) ||
20+
select oas_build_component_schemas_from_tables_and_composite_types(schemas) ||
2221
oas_build_component_schemas_headers()
2322
$$;
2423

25-
create or replace function oas_build_component_schemas_from_tables(schemas text[])
24+
create or replace function oas_build_component_schemas_from_tables_and_composite_types(schemas text[])
2625
returns jsonb language sql stable as
2726
$$
28-
select jsonb_object_agg(x.table_name, x.oas_schema)
27+
with recursive all_rels as (
28+
select *
29+
from postgrest_get_all_tables_and_composite_types()
30+
),
31+
recursive_rels_in_schema as (
32+
select *
33+
from all_rels
34+
where
35+
-- All the tables or views in the exposed schemas
36+
(table_schema = any(schemas) and (is_table or is_view))
37+
-- All the composite types or tables that are present in function arguments
38+
-- TODO: tweak postgrest_get_all_functions() or use another CTE for a more performant filter
39+
or table_oid in (
40+
select unnest(composite_args_ret)
41+
from postgrest_get_all_functions(schemas)
42+
)
43+
union
44+
-- Tables may have columns with composite or table types, so we recursively
45+
-- look for these composite types or tables outside of the exposed schemas
46+
-- in order to build the types correctly for the OpenAPI output.
47+
select e.*
48+
from all_rels e, recursive_rels_in_schema r
49+
where e.table_oid = r.column_composite_relid
50+
),
51+
all_tables_and_composite_types as (
52+
select
53+
table_schema,
54+
table_name,
55+
table_description,
56+
is_composite,
57+
array_agg(column_name order by column_position) filter (where not column_is_nullable) AS required_cols,
58+
jsonb_object_agg(
59+
column_name,
60+
case when column_item_data_type is null and column_composite_relid <> 0 then
61+
oas_build_reference_to_schemas(column_data_type)
62+
else
63+
oas_schema_object(
64+
description := column_description,
65+
type := postgrest_pgtype_to_oastype(column_data_type),
66+
format := column_data_type::text,
67+
maxlength := column_character_maximum_length,
68+
-- "default" := to_jsonb(info.column_default),
69+
enum := to_jsonb(column_enums),
70+
items :=
71+
case
72+
when column_item_data_type is null then
73+
null
74+
when column_composite_relid <> 0 then
75+
oas_build_reference_to_schemas(column_item_data_type)
76+
else
77+
oas_schema_object(
78+
type := postgrest_pgtype_to_oastype(column_item_data_type),
79+
format := column_item_data_type::text
80+
)
81+
end
82+
)
83+
end order by column_position
84+
) as columns
85+
from recursive_rels_in_schema
86+
group by table_schema, table_name, table_description, is_composite
87+
)
88+
select jsonb_object_agg(x.component_name, x.oas_schema)
2989
from (
30-
select table_name,
90+
select case when is_composite then table_schema || '.' else '' end || table_name as component_name,
3191
oas_schema_object(
3292
description := table_description,
33-
properties := columns,
93+
properties := coalesce(columns, '{}'),
3494
required := required_cols,
3595
type := 'object'
3696
) as oas_schema
37-
from postgrest_get_all_tables(schemas)
38-
where table_schema = any(schemas)
39-
) x;
40-
$$;
41-
42-
create or replace function oas_build_component_schemas_from_composite_types(schemas text[])
43-
returns jsonb language sql stable as
44-
$$
45-
SELECT coalesce(jsonb_object_agg(x.ct_name, x.oas_schema), '{}')
46-
FROM (
47-
SELECT comptype_schema || '.' || comptype_name as ct_name,
48-
oas_schema_object(
49-
description := comptype_description,
50-
properties := columns,
51-
type := 'object'
52-
) AS oas_schema
53-
FROM postgrest_get_all_composite_types(schemas)
97+
from all_tables_and_composite_types
5498
) x;
5599
$$;
56100

@@ -195,10 +239,11 @@ from (
195239
)
196240
) as param_schema
197241
from (
198-
select table_schema, table_name, unnest(all_cols) as column_name
199-
from postgrest_get_all_tables(schemas)
242+
select table_schema, is_table, is_view, table_name, column_name
243+
from postgrest_get_all_tables_and_composite_types()
244+
where table_schema = any(schemas)
245+
and (is_table or is_view)
200246
) _
201-
where table_schema = any(schemas)
202247
) x;
203248
$$;
204249

@@ -617,8 +662,10 @@ from (
617662
)
618663
)
619664
end as may_be_empty_response
620-
from postgrest_get_all_tables(schemas)
665+
from postgrest_get_all_tables_and_composite_types()
621666
where table_schema = any(schemas)
667+
and (is_table or is_view)
668+
group by table_schema, table_name, insertable, is_table, is_view
622669
) as x
623670
$$;
624671

@@ -695,9 +742,11 @@ from (
695742
)
696743
)
697744
) as oas_req_body
698-
from postgrest_get_all_tables(schemas)
745+
from postgrest_get_all_tables_and_composite_types()
699746
where table_schema = any(schemas)
747+
and (is_table or is_view)
700748
and insertable
749+
group by table_schema, table_name, insertable
701750
) as x;
702751
$$;
703752

sql/paths.sql

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,12 @@ from (
124124
end
125125
) as oas_path_item
126126
from (
127-
select table_schema, table_name, table_description, insertable, updatable, deletable, unnest(all_cols) as column_name
128-
from postgrest_get_all_tables(schemas)
127+
select table_schema, table_name, table_description, insertable, updatable, deletable, column_name
128+
from postgrest_get_all_tables_and_composite_types()
129+
where table_schema = any(schemas)
130+
and (is_table or is_view)
131+
order by table_schema, table_name, column_position
129132
) _
130-
where table_schema = any(schemas)
131133
group by table_schema, table_name, table_description, insertable, updatable, deletable
132134
) x;
133135
$$;

0 commit comments

Comments
 (0)