From f4011e5d8c25b48dc420ebde15f53e982933a360 Mon Sep 17 00:00:00 2001 From: Diogo Biazus Date: Mon, 15 Jun 2015 21:06:51 -0400 Subject: [PATCH] Changes the insertable to true in views that are insertable through triggers [fixes #206] --- src/PostgREST/PgStructure.hs | 9 +++++---- test/Feature/StructureSpec.hs | 1 + test/Unit/PgStructureSpec.hx | 2 +- test/fixtures/schema.sql | 31 +++++++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/PostgREST/PgStructure.hs b/src/PostgREST/PgStructure.hs index 0f4f0182d..85132fd68 100644 --- a/src/PostgREST/PgStructure.hs +++ b/src/PostgREST/PgStructure.hs @@ -44,8 +44,9 @@ tables schema = do rows <- H.listEx $ [H.stmt| select table_schema, table_name, - is_insertable_into - from information_schema.tables + 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 |] schema @@ -135,8 +136,8 @@ data Column = Column { , colFK :: Maybe ForeignKey } deriving (Show) -tableFromRow :: (Text, Text, Text) -> Table -tableFromRow (s, n, i) = Table s n (toBool i) +tableFromRow :: (Text, Text, Bool) -> Table +tableFromRow (s, n, i) = Table s n i columnFromRow :: (Text, Text, Text, Int, Text, Text, diff --git a/test/Feature/StructureSpec.hs b/test/Feature/StructureSpec.hs index ebd7cdd22..fbc0b08d7 100644 --- a/test/Feature/StructureSpec.hs +++ b/test/Feature/StructureSpec.hs @@ -17,6 +17,7 @@ spec = around withApp $ do {"schema":"1","name":"auto_incrementing_pk","insertable":true} , {"schema":"1","name":"compound_pk","insertable":true} , {"schema":"1","name":"has_fk","insertable":true} + , {"schema":"1","name":"insertable_view_with_join","insertable":true} , {"schema":"1","name":"items","insertable":true} , {"schema":"1","name":"json","insertable":true} , {"schema":"1","name":"menagerie","insertable":true} diff --git a/test/Unit/PgStructureSpec.hx b/test/Unit/PgStructureSpec.hx index 08c044bf5..0ca6279fb 100644 --- a/test/Unit/PgStructureSpec.hx +++ b/test/Unit/PgStructureSpec.hx @@ -14,7 +14,7 @@ spec = around dbWithSchema $ beforeWith setRole $ do it "shows all the tables" $ \conn -> do ts <- tables "1" conn map tableName ts `shouldBe` ["authors_only","auto_incrementing_pk", - "compound_pk","has_fk","items","menagerie","no_pk", "simple_pk"] + "compound_pk","has_fk","insertable_view_with_join","items","menagerie","no_pk", "simple_pk"] describe "columns" $ do it "responds with each column for the table" $ \conn -> do diff --git a/test/fixtures/schema.sql b/test/fixtures/schema.sql index 3201a76fa..3a010b691 100755 --- a/test/fixtures/schema.sql +++ b/test/fixtures/schema.sql @@ -84,6 +84,17 @@ $$; ALTER FUNCTION postgrest.set_authors_only_owner() OWNER TO postgrest_test; +CREATE FUNCTION "1".insert_insertable_view_with_join() RETURNS trigger + LANGUAGE plpgsql + AS $$ +begin + INSERT INTO "1".auto_incrementing_pk (nullable_string, non_nullable_string) VALUES (NEW.nullable_string, NEW.non_nullable_string); + RETURN NEW; +end; +$$; + +ALTER FUNCTION "1".insert_insertable_view_with_join() OWNER TO postgrest_test; + SET search_path = "1", pg_catalog; SET default_tablespace = ''; @@ -159,6 +170,18 @@ ALTER TABLE "1".has_fk_id_seq OWNER TO postgrest_test; ALTER SEQUENCE has_fk_id_seq OWNED BY has_fk.id; +CREATE VIEW "1".insertable_view_with_join AS + SELECT has_fk.id, + has_fk.auto_inc_fk, + has_fk.simple_fk, + auto_incrementing_pk.nullable_string, + auto_incrementing_pk.non_nullable_string, + auto_incrementing_pk.inserted_at + FROM (has_fk + JOIN auto_incrementing_pk USING (id)); + + +ALTER TABLE "1".insertable_view_with_join OWNER TO postgrest_test; CREATE TABLE items ( @@ -341,6 +364,9 @@ SET search_path = "1", pg_catalog; ALTER TABLE ONLY authors_only ADD CONSTRAINT authors_only_pkey PRIMARY KEY (secret); +CREATE TRIGGER insert_insertable_view_with_join INSTEAD OF INSERT ON "1".insertable_view_with_join FOR EACH ROW EXECUTE PROCEDURE "1".insert_insertable_view_with_join(); + + CREATE TRIGGER secrets_owner_track BEFORE INSERT OR UPDATE ON authors_only FOR EACH ROW EXECUTE PROCEDURE postgrest.set_authors_only_owner(); @@ -511,6 +537,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 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; +GRANT ALL ON TABLE insertable_view_with_join TO postgrest_anonymous; + SET search_path = postgrest, pg_catalog;