From 1f513f24a58f77a3fc2b76f872aee50038a9a80d Mon Sep 17 00:00:00 2001 From: steve-chavez Date: Thu, 7 Mar 2019 10:59:05 -0500 Subject: [PATCH] Fix #1242, embed a view having a select in a where --- CHANGELOG.md | 1 + src/PostgREST/DbStructure.hs | 7 +++++-- test/Feature/QuerySpec.hs | 5 +++++ test/fixtures/privileges.sql | 1 + test/fixtures/schema.sql | 13 +++++++++++++ 5 files changed, 25 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aea96f9cb..4e845468c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - #1223, Fix incorrect OpenAPI externalDocs url - @steve-chavez - #1221, Fix embedding other resources when having a self join - @steve-chavez +- #1242, Fix embedding a view having a select in a where - @steve-chavez ## [5.2.0] - 2018-12-12 diff --git a/src/PostgREST/DbStructure.hs b/src/PostgREST/DbStructure.hs index 36b27a986..af1870582 100644 --- a/src/PostgREST/DbStructure.hs +++ b/src/PostgREST/DbStructure.hs @@ -707,8 +707,11 @@ allSynonyms cols pgVer = -- query explanation at https://gist.github.com/steve-chavez/7ee0e6590cddafb532e5f00c46275569 where subselectRegex :: Text - subselectRegex | pgVer < pgVersion100 = ":subselect {.*?:constraintDeps <>} :location" - | otherwise = ":subselect {.*?:stmt_len 0} :location" + -- "result" appears when the subselect is used inside "case when", see `authors_have_book_in_decade` fixture + -- "resno" appears in every other case + -- when copying the query into pg make sure you omit one backslash from \\d+, it should be like `\d+` for the regex + subselectRegex | pgVer < pgVersion100 = ":subselect {.*?:constraintDeps <>} :location \\d+} :res(no|ult)" + | otherwise = ":subselect {.*?:stmt_len 0} :location \\d+} :res(no|ult)" sql = [qc| with views as ( diff --git a/test/Feature/QuerySpec.hs b/test/Feature/QuerySpec.hs index f8e4fda7f..186373ac6 100644 --- a/test/Feature/QuerySpec.hs +++ b/test/Feature/QuerySpec.hs @@ -433,6 +433,11 @@ spec = do {"number_of_projects":2,"client":{"name":"Apple"}}] |] { matchHeaders = [matchContentTypeJson] } + it "can embed a view that has a subselect containing a select in a where" $ + get "/authors_w_entities?select=name,entities,books(title)&id=eq.1" `shouldRespondWith` + [json| [{"name":"George Orwell","entities":[3, 4],"books":[{"title":"1984"}]}] |] + { matchHeaders = [matchContentTypeJson] } + describe "path fixed" $ do it "works when requesting children 2 levels" $ get "/clients?id=eq.1&select=id,projects:projects.client_id(id,tasks(id))" `shouldRespondWith` diff --git a/test/fixtures/privileges.sql b/test/fixtures/privileges.sql index c78a11172..e31123f74 100644 --- a/test/fixtures/privileges.sql +++ b/test/fixtures/privileges.sql @@ -99,6 +99,7 @@ GRANT ALL ON TABLE , projects_count_grouped_by , "Server Today" , pgrst_reserved_chars + , authors_w_entities TO postgrest_test_anonymous; GRANT INSERT ON TABLE insertonly TO postgrest_test_anonymous; diff --git a/test/fixtures/schema.sql b/test/fixtures/schema.sql index dcefec6d6..8ac8942d4 100755 --- a/test/fixtures/schema.sql +++ b/test/fixtures/schema.sql @@ -1545,6 +1545,19 @@ select from projects group by client_id; +create view test.authors_w_entities as +select + id, + name, + ( + select json_agg(id) + from test.entities + where id not in ( + select parent_id from test.child_entities + ) + ) as entities +from private.authors; + CREATE TABLE test."Foo"( id int primary key, name text