Skip to content

Commit

Permalink
refactor: make the function that queries all tables more reusable
Browse files Browse the repository at this point in the history
- Merges postgrest_get_all_tables and postgrest_get_all_composite_types
- Removes the need of using unnest(), making the queries a bit more performant
  • Loading branch information
laurenceisla authored Sep 2, 2024
1 parent 040f8ef commit 95bdd26
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 459 deletions.
105 changes: 77 additions & 28 deletions sql/components.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,84 @@ $$;
create or replace function oas_build_component_schemas(schemas text[])
returns jsonb language sql stable as
$$
select oas_build_component_schemas_from_tables(schemas) ||
oas_build_component_schemas_from_composite_types(schemas) ||
select oas_build_component_schemas_from_tables_and_composite_types(schemas) ||
oas_build_component_schemas_headers()
$$;

create or replace function oas_build_component_schemas_from_tables(schemas text[])
create or replace function oas_build_component_schemas_from_tables_and_composite_types(schemas text[])
returns jsonb language sql stable as
$$
select jsonb_object_agg(x.table_name, x.oas_schema)
with recursive all_rels as (
select *
from postgrest_get_all_tables_and_composite_types()
),
recursive_rels_in_schema as (
select *
from all_rels
where
-- All the tables or views in the exposed schemas
(table_schema = any(schemas) and (is_table or is_view))
-- All the composite types or tables that are present in function arguments
-- TODO: tweak postgrest_get_all_functions() or use another CTE for a more performant filter
or table_oid in (
select unnest(composite_args_ret)
from postgrest_get_all_functions(schemas)
)
union
-- Tables may have columns with composite or table types, so we recursively
-- look for these composite types or tables outside of the exposed schemas
-- in order to build the types correctly for the OpenAPI output.
select e.*
from all_rels e, recursive_rels_in_schema r
where e.table_oid = r.column_composite_relid
),
all_tables_and_composite_types as (
select
table_schema,
table_name,
table_description,
is_composite,
array_agg(column_name order by column_position) filter (where not column_is_nullable) AS required_cols,
jsonb_object_agg(
column_name,
case when column_item_data_type is null and column_composite_relid <> 0 then
oas_build_reference_to_schemas(column_data_type)
else
oas_schema_object(
description := column_description,
type := postgrest_pgtype_to_oastype(column_data_type),
format := column_data_type::text,
maxlength := column_character_maximum_length,
-- "default" := to_jsonb(info.column_default),
enum := to_jsonb(column_enums),
items :=
case
when column_item_data_type is null then
null
when column_composite_relid <> 0 then
oas_build_reference_to_schemas(column_item_data_type)
else
oas_schema_object(
type := postgrest_pgtype_to_oastype(column_item_data_type),
format := column_item_data_type::text
)
end
)
end order by column_position
) as columns
from recursive_rels_in_schema
group by table_schema, table_name, table_description, is_composite
)
select jsonb_object_agg(x.component_name, x.oas_schema)
from (
select table_name,
select case when is_composite then table_schema || '.' else '' end || table_name as component_name,
oas_schema_object(
description := table_description,
properties := columns,
properties := coalesce(columns, '{}'),
required := required_cols,
type := 'object'
) as oas_schema
from postgrest_get_all_tables(schemas)
where table_schema = any(schemas)
) x;
$$;

create or replace function oas_build_component_schemas_from_composite_types(schemas text[])
returns jsonb language sql stable as
$$
SELECT coalesce(jsonb_object_agg(x.ct_name, x.oas_schema), '{}')
FROM (
SELECT comptype_schema || '.' || comptype_name as ct_name,
oas_schema_object(
description := comptype_description,
properties := columns,
type := 'object'
) AS oas_schema
FROM postgrest_get_all_composite_types(schemas)
from all_tables_and_composite_types
) x;
$$;

Expand Down Expand Up @@ -195,10 +239,11 @@ from (
)
) as param_schema
from (
select table_schema, table_name, unnest(all_cols) as column_name
from postgrest_get_all_tables(schemas)
select table_schema, is_table, is_view, table_name, column_name
from postgrest_get_all_tables_and_composite_types()
where table_schema = any(schemas)
and (is_table or is_view)
) _
where table_schema = any(schemas)
) x;
$$;

Expand Down Expand Up @@ -617,8 +662,10 @@ from (
)
)
end as may_be_empty_response
from postgrest_get_all_tables(schemas)
from postgrest_get_all_tables_and_composite_types()
where table_schema = any(schemas)
and (is_table or is_view)
group by table_schema, table_name, insertable, is_table, is_view
) as x
$$;

Expand Down Expand Up @@ -695,9 +742,11 @@ from (
)
)
) as oas_req_body
from postgrest_get_all_tables(schemas)
from postgrest_get_all_tables_and_composite_types()
where table_schema = any(schemas)
and (is_table or is_view)
and insertable
group by table_schema, table_name, insertable
) as x;
$$;

Expand Down
8 changes: 5 additions & 3 deletions sql/paths.sql
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,12 @@ from (
end
) as oas_path_item
from (
select table_schema, table_name, table_description, insertable, updatable, deletable, unnest(all_cols) as column_name
from postgrest_get_all_tables(schemas)
select table_schema, table_name, table_description, insertable, updatable, deletable, column_name
from postgrest_get_all_tables_and_composite_types()
where table_schema = any(schemas)
and (is_table or is_view)
order by table_schema, table_name, column_position
) _
where table_schema = any(schemas)
group by table_schema, table_name, table_description, insertable, updatable, deletable
) x;
$$;
Expand Down
Loading

0 comments on commit 95bdd26

Please sign in to comment.