diff --git a/CHANGELOG.md b/CHANGELOG.md index e9fd94ddf..9257ee671 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 - Allow the use of columns named "count" in relations queried 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 57e03d426..867361360 100644 --- a/test/Feature/StructureSpec.hs +++ b/test/Feature/StructureSpec.hs @@ -21,6 +21,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 d3f9dadbb..805d53273 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, @@ -563,6 +569,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;