diff --git a/Makefile b/Makefile index 63e5569..70a5b84 100644 --- a/Makefile +++ b/Makefile @@ -20,4 +20,5 @@ include $(PGXS) $(EXTENSION)--$(EXTVERSION).sql: $(CDBSCRIPTS) cartodb_hooks.sql Makefile cat $(CDBSCRIPTS) | sed 's/\ $@ + echo "GRANT USAGE ON SCHEMA cartodb TO public;" >> $@ cat cartodb_hooks.sql >> $@ diff --git a/cartodb_hooks.sql b/cartodb_hooks.sql index d70ef3b..33fc88e 100644 --- a/cartodb_hooks.sql +++ b/cartodb_hooks.sql @@ -49,12 +49,6 @@ BEGIN RAISE DEBUG 'Relation % of kind % dropped from namespace oid %', event_info.old_relation_oid, (event_info.old).relkind, (event_info.old).relnamespace; - -- We don't want to react to alters triggered by superuser, - IF current_setting('is_superuser') = 'on' THEN - RAISE DEBUG 'no ddl trigger for superuser'; - RETURN; - END IF; - -- delete record from CDB_TableMetadata (should invalidate varnish) DELETE FROM cartodb.CDB_TableMetadata WHERE tabname = event_info.old_relation_oid; @@ -91,7 +85,9 @@ BEGIN PERFORM cartodb.cdb_enable_ddl_hooks(); - -- TODO: update CDB_TableMetadata.updated_at (should invalidate varnish) + -- update CDB_TableMetadata.updated_at (should invalidate varnish) + UPDATE cartodb.CDB_TableMetadata SET updated_at = NOW() + WHERE tabname = event_info.relation; END; $$; -- } @@ -126,7 +122,9 @@ BEGIN PERFORM cartodb.cdb_enable_ddl_hooks(); - -- TODO: update CDB_TableMetadata.updated_at (should invalidate varnish) + -- update CDB_TableMetadata.updated_at (should invalidate varnish) + UPDATE cartodb.CDB_TableMetadata SET updated_at = NOW() + WHERE tabname = event_info.relation; END; $$; -- } @@ -155,7 +153,9 @@ BEGIN RETURN; END IF; - -- TODO: update CDB_TableMetadata.updated_at (should invalidate varnish) + -- update CDB_TableMetadata.updated_at (should invalidate varnish) + UPDATE cartodb.CDB_TableMetadata SET updated_at = NOW() + WHERE tabname = event_info.relation; END; $$; -- } diff --git a/sql/test_ddl_triggers.sql b/sql/test_ddl_triggers.sql index c146a39..797f3e4 100644 --- a/sql/test_ddl_triggers.sql +++ b/sql/test_ddl_triggers.sql @@ -13,7 +13,7 @@ BEGIN PERFORM cdb_invalidate_varnish(0); EXCEPTION WHEN undefined_function THEN - CREATE OR REPLACE FUNCTION cartodb.cdb_invalidate_varnish(tabname regclass) + CREATE OR REPLACE FUNCTION cartodb.cdb_invalidate_varnish(tabname text) RETURNS void AS '' LANGUAGE 'sql'; END; $$ LANGUAGE 'plpgsql'; @@ -23,39 +23,107 @@ $$ LANGUAGE 'plpgsql'; create schema c; -CREATE TABLE IF NOT EXISTS - public.CDB_TableMetadata ( - tabname regclass not null primary key, - updated_at timestamp with time zone not null default now() - ); - CREATE USER cartodb_postgresql_unpriv_user; GRANT ALL ON SCHEMA c to cartodb_postgresql_unpriv_user; -GRANT SELECT ON public.CDB_TableMetadata to cartodb_postgresql_unpriv_user; SET SESSION AUTHORIZATION 'cartodb_postgresql_unpriv_user'; --SELECT session_user, current_user; +---------------------- +-- CREATE TABLE +---------------------- ---create table c.t3(a int); select 1 as i INTO c.t3; -select * from c.t3; -select tabname::text, updated_at from CDB_TableMetadata; + +select + cartodb_id, created_at=updated_at as "c=u", + NOW() - updated_at < '1 secs' as "u<1s", + the_geom, the_geom_webmercator, + i +from c.t3; + +select + tabname::text, + round(extract('secs' from now() - updated_at)) as age +FROM CDB_TableMetadata; + +---------------------------- +-- ALTER TABLE RENAME COLUMN +---------------------------- + +select pg_sleep(1); alter table c.t3 rename column the_geom_webmercator to webmerc; -select * from c.t3; -select tabname::text, updated_at from CDB_TableMetadata; + +select + cartodb_id, created_at=updated_at as "c=u", + NOW() - updated_at < '1 secs' as "u<1s", + the_geom, the_geom_webmercator, + i, webmerc +from c.t3; + +select + tabname::text, + round(extract('secs' from now() - updated_at)) as age +FROM CDB_TableMetadata; + +select pg_sleep(1); alter table c.t3 rename column the_geom_webmercator to webmerc2; -select * from c.t3; -select tabname::text, updated_at from CDB_TableMetadata; + +select + cartodb_id, created_at=updated_at as "c=u", + NOW() - updated_at < '1 secs' as "u<1s", + the_geom, the_geom_webmercator, + i, webmerc, webmerc2 +from c.t3; + +select + tabname::text, + round(extract('secs' from now() - updated_at)) as age +FROM CDB_TableMetadata; + +---------------------------- +-- ALTER TABLE DROP COLUMN +---------------------------- + +select pg_sleep(1); alter table c.t3 drop column the_geom_webmercator; -select * from c.t3; -select tabname::text, updated_at from CDB_TableMetadata; + +select + cartodb_id, created_at=updated_at as "c=u", + NOW() - updated_at < '1 secs' as "u<1s", + the_geom, the_geom_webmercator, + i, webmerc, webmerc2 +from c.t3; + +select + tabname::text, + round(extract('secs' from now() - updated_at)) as age +FROM CDB_TableMetadata; + +---------------------------- +-- ALTER TABLE ADD COLUMN +---------------------------- + +select pg_sleep(1); alter table c.t3 add column id2 int; -select * from c.t3; -select tabname::text, updated_at from CDB_TableMetadata; + +select + cartodb_id, created_at=updated_at as "c=u", + NOW() - updated_at < '1 secs' as "u<1s", + the_geom, the_geom_webmercator, + i, webmerc, webmerc2, id2 +from c.t3; + +select + tabname::text, + round(extract('secs' from now() - updated_at)) as age +FROM CDB_TableMetadata; + +---------------------------- +-- DROP TABLE +---------------------------- RESET SESSION AUTHORIZATION; drop schema c cascade; -select tabname::text, updated_at from CDB_TableMetadata; +select count(*) from CDB_TableMetadata; -DROP TABLE public.CDB_TableMetadata; DROP USER cartodb_postgresql_unpriv_user;