From 84f68c68cb67934df75449db8ea2cce6e484836d Mon Sep 17 00:00:00 2001 From: Joe Nelson Date: Sun, 12 Feb 2017 16:32:15 -0800 Subject: [PATCH] Public schema table embedding (#803) * Alter fixture to test objects in public schema Properly erase and restore the public schema -- requires permissions to install pgcrypto each time. * Test for fks through public schema tables * Thanks @fab1an --- CHANGELOG.md | 2 ++ src/PostgREST/DbStructure.hs | 2 +- test/Feature/QuerySpec.hs | 4 +++ test/create_test_db | 4 ++- test/fixtures/database.sql | 4 +-- test/fixtures/privileges.sql | 5 ++++ test/fixtures/roles.sql | 2 +- test/fixtures/schema.sql | 48 +++++++++++++++++++----------------- 8 files changed, 43 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dc9ee4857..f73778911 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Fixed +- Resource embedding in views referencing tables in public schema - @fab1an + ## [0.4.0.0] - 2017-01-19 ### Added diff --git a/src/PostgREST/DbStructure.hs b/src/PostgREST/DbStructure.hs index dfcfb94e3..135e589c2 100644 --- a/src/PostgREST/DbStructure.hs +++ b/src/PostgREST/DbStructure.hs @@ -606,7 +606,7 @@ allSynonyms cols = select case when match is not null then coalesce(match[8], match[7], match[4]) end from regexp_matches( CONCAT('SELECT ', SPLIT_PART(vcu.view_definition, 'SELECT', 2)), - CONCAT('SELECT.*?((',vcu.table_name,')|(\w+))\.(', vcu.column_name, ')(\s+AS\s+("([^"]+)"|([^, \n\t]+)))?.*?FROM.*?',vcu.table_schema,'\.(\2|',vcu.table_name,'\s+(as\s)?\3)'), + CONCAT('SELECT.*?((',vcu.table_name,')|(\w+))\.(', vcu.column_name, ')(\s+AS\s+("([^"]+)"|([^, \n\t]+)))?.*?FROM.*?(',vcu.table_schema,'\.|)(\2|',vcu.table_name,'\s+(as\s)?\3)'), 'nsi' ) match ) as view_column_name diff --git a/test/Feature/QuerySpec.hs b/test/Feature/QuerySpec.hs index af22b6011..44231aee1 100644 --- a/test/Feature/QuerySpec.hs +++ b/test/Feature/QuerySpec.hs @@ -302,6 +302,10 @@ spec = do get "/projects?id=in.1,3&select=id,name,client_id,client{id,name}" `shouldRespondWith` [str|[{"id":1,"name":"Windows 7","client_id":1,"client":{"id":1,"name":"Microsoft"}},{"id":3,"name":"IOS","client_id":2,"client":{"id":2,"name":"Apple"}}]|] + it "can detect fk relations through views to tables in the public schema" $ + get "/consumers_view?select=*,orders_view{*}" `shouldRespondWith` 200 + + describe "ordering response" $ do it "by a column asc" $ get "/items?id=lte.2&order=id.asc" diff --git a/test/create_test_db b/test/create_test_db index 7a382b74a..8690b53f7 100755 --- a/test/create_test_db +++ b/test/create_test_db @@ -43,8 +43,10 @@ WHERE pg_stat_activity.datname = '$DB' DROP DATABASE IF EXISTS $DB; DROP ROLE IF EXISTS $TEST_USER_NAME; -CREATE USER $TEST_USER_NAME WITH LOGIN NOINHERIT PASSWORD '$TEST_USER_PASS' CREATEROLE; +CREATE USER $TEST_USER_NAME WITH SUPERUSER LOGIN NOINHERIT PASSWORD '$TEST_USER_PASS' CREATEROLE; CREATE DATABASE $DB OWNER $TEST_USER_NAME; +\\connect $DB +ALTER SCHEMA public OWNER TO $TEST_USER_NAME; EOF PGDATABASE=$DB PGOPTIONS='-c client_min_messages=WARNING' psql "$URI" --set=db=$DB -Xq < ''), + primary key (id) +); + +create table public_orders ( + id serial not null unique, + consumer integer not null references public_consumers(id), + number integer not null, + primary key (id) +); + -- -- Name: anti_id(test.items); Type: FUNCTION; Schema: public; Owner: - -- @@ -161,6 +155,14 @@ CREATE TABLE موارد ( SET search_path = test, pg_catalog; + +create view orders_view as + select * from public.public_orders; + +create view consumers_view as + select * from public.public_consumers; + + -- -- Name: getitemrange(bigint, bigint); Type: FUNCTION; Schema: test; Owner: - --