From ab0170ffafc134542ab34b529fbf94fc349982a7 Mon Sep 17 00:00:00 2001 From: Diogo Biazus Date: Sat, 1 Aug 2015 01:09:12 -0400 Subject: [PATCH 1/2] Adds materialized views to list of relations in GET / [#242] --- src/PostgREST/PgStructure.hs | 26 ++++++++++++++++++++------ test/Feature/StructureSpec.hs | 1 + test/fixtures/schema.sql | 11 +++++++++++ 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/PostgREST/PgStructure.hs b/src/PostgREST/PgStructure.hs index 8d7d7e64d..7e42bb544 100644 --- a/src/PostgREST/PgStructure.hs +++ b/src/PostgREST/PgStructure.hs @@ -43,12 +43,26 @@ tables :: Text -> H.Tx P.Postgres s [Table] tables schema = do rows <- H.listEx $ [H.stmt| - select table_schema, table_name, - t.is_insertable_into::boolean OR coalesce(is_trigger_insertable_into::boolean, false) - from information_schema.tables t - left join information_schema.views using(table_catalog, table_schema, table_name) - where table_schema = ? - order by table_name + select + n.nspname as table_schema, + relname as table_name, + c.relkind = 'r' or (c.relkind IN ('v', 'f')) and (pg_relation_is_updatable(c.oid::regclass, false) & 8) = 8 + or (exists ( + select 1 + from pg_trigger + where pg_trigger.tgrelid = c.oid and (pg_trigger.tgtype::integer & 69) = 69) + ) as insertable + from + pg_class c + join pg_namespace n on n.oid = c.relnamespace + where + c.relkind in ('v', 'r', 'm') + and n.nspname = ? + and ( + pg_has_role(c.relowner, 'USAGE'::text) + or has_table_privilege(c.oid, 'SELECT, INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER'::text) or has_any_column_privilege(c.oid, 'SELECT, INSERT, UPDATE, REFERENCES'::text) + ) + order by relname |] schema return $ map tableFromRow rows diff --git a/test/Feature/StructureSpec.hs b/test/Feature/StructureSpec.hs index a1b7f19f0..70061f093 100644 --- a/test/Feature/StructureSpec.hs +++ b/test/Feature/StructureSpec.hs @@ -20,6 +20,7 @@ spec = around withApp $ do , {"schema":"1","name":"insertable_view_with_join","insertable":true} , {"schema":"1","name":"items","insertable":true} , {"schema":"1","name":"json","insertable":true} + , {"schema":"1","name":"materialized_view","insertable":false} , {"schema":"1","name":"menagerie","insertable":true} , {"schema":"1","name":"no_pk","insertable":true} , {"schema":"1","name":"nullable_integer","insertable":true} diff --git a/test/fixtures/schema.sql b/test/fixtures/schema.sql index d5a54adff..48e9d2017 100755 --- a/test/fixtures/schema.sql +++ b/test/fixtures/schema.sql @@ -169,6 +169,12 @@ ALTER TABLE "1".has_fk_id_seq OWNER TO postgrest_test; ALTER SEQUENCE has_fk_id_seq OWNED BY has_fk.id; +CREATE MATERIALIZED VIEW "1".materialized_view AS + SELECT + version(); + +ALTER TABLE "1".materialized_view OWNER TO postgrest_test; + CREATE VIEW "1".insertable_view_with_join AS SELECT has_fk.id, has_fk.auto_inc_fk, @@ -558,6 +564,11 @@ REVOKE ALL ON TABLE tsearch FROM postgrest_test; GRANT ALL ON TABLE tsearch TO postgrest_test; GRANT ALL ON TABLE tsearch TO postgrest_anonymous; +REVOKE ALL ON TABLE materialized_view FROM PUBLIC; +REVOKE ALL ON TABLE materialized_view FROM postgrest_test; +GRANT ALL ON TABLE materialized_view TO postgrest_test; +GRANT ALL ON TABLE materialized_view TO postgrest_anonymous; + REVOKE ALL ON TABLE insertable_view_with_join FROM PUBLIC; REVOKE ALL ON TABLE insertable_view_with_join FROM postgrest_test; GRANT ALL ON TABLE insertable_view_with_join TO postgrest_test; From b45953dff80fd3175847a9052e33e6209aace4ff Mon Sep 17 00:00:00 2001 From: Diogo Biazus Date: Sat, 1 Aug 2015 01:23:31 -0400 Subject: [PATCH 2/2] Mentions fix in CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f8e945796..cc8f2e8e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - Allow filters by computed columns ### Fixed +- Add materialized views to results in GET / - Indicate insertable=true for views that are insertable through triggers - Builds under GHC 7.10