Skip to content

Commit

Permalink
Fix embedding through hidden views
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfgangwalther committed Nov 3, 2020
1 parent 511cb40 commit 1ed7090
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
10 changes: 5 additions & 5 deletions src/PostgREST/DbStructure.hs
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ allSourceColumns cols pgVer =
from pg_class c
join pg_namespace n on n.oid = c.relnamespace
join pg_rewrite r on r.ev_class = c.oid
where c.relkind in ('v', 'm') and n.nspname = ANY ($1)
where c.relkind in ('v', 'm') and n.nspname not in ('pg_catalog', 'information_schema')
),
removed_subselects as(
select
Expand Down Expand Up @@ -744,8 +744,7 @@ allSourceColumns cols pgVer =
recursion as(
select r.*
from results r
join any_part_of_fk using (resorigtbl, resorigcol)
join pg_class c on c.oid = r.resorigtbl and c.relkind not in ('v', 'm')
where view_schema = ANY ($1)
union all
select
view.view_id,
Expand All @@ -754,8 +753,8 @@ allSourceColumns cols pgVer =
view.view_column,
tab.resorigtbl,
tab.resorigcol
from recursion tab
join results view on view.resorigtbl=tab.view_id and view.resorigcol=tab.view_column
from recursion view
join results tab on view.resorigtbl=tab.view_id and view.resorigcol=tab.view_column
)
select
sch.nspname as table_schema,
Expand All @@ -769,6 +768,7 @@ allSourceColumns cols pgVer =
join pg_attribute col on col.attrelid = tbl.oid and col.attnum = rec.resorigcol
join pg_attribute vcol on vcol.attrelid = rec.view_id and vcol.attnum = rec.view_column
join pg_namespace sch on sch.oid = tbl.relnamespace
join any_part_of_fk using (resorigtbl, resorigcol)
order by view_schema, view_name, view_column_name; |]

getPgVersion :: H.Session PgVersion
Expand Down
10 changes: 7 additions & 3 deletions test/Feature/QuerySpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,6 @@ spec actualPgVersion = do
it "can detect fk relations through materialized views to tables in the public schema" $
get "/materialized_projects?select=*,users(*)" `shouldRespondWith` 200

it "can detect fk relations through multiple views recursively" $
get "/consumers_view_view?select=*,orders_view(*)" `shouldRespondWith` 200

it "can request two parents" $
get "/articleStars?select=createdAt,article:articles(owner),user:users(name)&limit=1" `shouldRespondWith`
[json|[{"createdAt":"2015-12-08T04:22:57.472738","article":{"owner": "postgrest_test_authenticator"},"user":{"name": "Angela Martin"}}]|]
Expand Down Expand Up @@ -424,6 +421,13 @@ spec actualPgVersion = do
[json|[ { "title": "To Kill a Mockingbird", "author": { "name": "Harper Lee" } } ]|]
{ matchHeaders = [matchContentTypeJson] }

describe "can detect fk relations through multiple views recursively" $ do
it "when all views are public" $
get "/consumers_view_view?select=*,orders_view(*)" `shouldRespondWith` 200

it "when middle view is private" $
get "/consumers_private_view?select=*,orders_view(*)" `shouldRespondWith` 200

it "works with views that have subselects" $
get "/authors_books_number?select=*,books(title)&id=eq.1" `shouldRespondWith`
[json|[ {"id":1, "name":"George Orwell","num_in_forties":1,"num_in_fifties":0,"num_in_sixties":0,"num_in_all_decades":1,
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/privileges.sql
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ GRANT ALL ON TABLE
, public.public_orders
, consumers_view
, consumers_view_view
, consumers_private_view
, orders_view
, images
, images_base64
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ create view consumers_view as
create view consumers_view_view as
select * from consumers_view;

create view private.consumers_private as
select * from consumers_view;

create view consumers_private_view as
select * from private.consumers_private;

--
-- Name: getitemrange(bigint, bigint); Type: FUNCTION; Schema: test; Owner: -
--
Expand Down

0 comments on commit 1ed7090

Please sign in to comment.