Skip to content

Commit 615773c

Browse files
committed
refactor: add type for all_m2o_and_o2o_rels
1 parent 45a72d7 commit 615773c

4 files changed

Lines changed: 200 additions & 185 deletions

File tree

sql/pgrst_schema_cache.sql

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,21 @@
44
-- and pg doesn't allow it as a view column.
55
create type ref_pair as (col text, ref text);
66

7+
create type relationship as (
8+
table_schema text,
9+
table_name text,
10+
foreign_table_schema text,
11+
foreign_table_name text,
12+
is_self bool ,
13+
constraint_name text,
14+
cols_and_fcols ref_pair[],
15+
is_one_to_one bool
16+
);
17+
718
-- same query as PostgREST's allM2OandO2ORels
8-
create view all_m2o_and_o2o_rels as
19+
create function all_m2o_and_o2o_rels ()
20+
returns setof relationship as $$
21+
922
with
1023
pks_uniques_cols as (
1124
select
@@ -44,6 +57,8 @@ where traint.contype = 'f'
4457
and traint.conparentid = 0
4558
order by traint.conrelid, traint.conname;
4659

60+
$$ language sql stable;
61+
4762

4863
-- type for the array of (column_name, view_columns[]), otherwise the creation of the function would fail
4964
CREATE TYPE col_dep AS (

test/expected/m2o_o2o.out

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
select * from all_m2o_and_o2o_rels;
2-
table_schema | table_name | foreign_table_schema | foreign_table_name | is_self | constraint_name | cols_and_fcols | one_to_one
3-
------------------+--------------------------+----------------------+----------------------+---------+-------------------------------------------------------------+-----------------------------------------------------------------------------------------+------------
1+
select * from all_m2o_and_o2o_rels();
2+
table_schema | table_name | foreign_table_schema | foreign_table_name | is_self | constraint_name | cols_and_fcols | is_one_to_one
3+
------------------+--------------------------+----------------------+----------------------+---------+-------------------------------------------------------------+-----------------------------------------------------------------------------------------+---------------
44
public | public_orders | public | public_consumers | f | public_orders_consumer_fkey | {"(consumer,id)"} | f
55
test | has_fk | test | auto_incrementing_pk | f | has_fk_fk_fkey | {"(auto_inc_fk,id)"} | f
66
test | has_fk | test | simple_pk | f | has_fk_simple_fk_fkey | {"(simple_fk,k)"} | f

0 commit comments

Comments
 (0)