From a87f358253cc906fad3342f42eabe0bf86457269 Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Tue, 22 Oct 2013 18:16:57 +0200 Subject: [PATCH] Add drop table handler, manage CDB_TableMetadata entries on create/drop --- cartodb_hooks.sql | 33 ++++++++++++++++++++++++++++++--- hook_on_table_create.sql | 6 ++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/cartodb_hooks.sql b/cartodb_hooks.sql index 4915bb6..23ec215 100644 --- a/cartodb_hooks.sql +++ b/cartodb_hooks.sql @@ -29,6 +29,31 @@ BEGIN -- CDB_CartodbfyTable must not create tables, or infinite loop will happen PERFORM public.CDB_CartodbfyTable(event_info.relation); + -- Add entry to CDB_TableMetadata (should CartodbfyTable do this?) + INSERT INTO public.CDB_TableMetadata(tabname,updated_at) + VALUES (event_info.relation, now()); + +END; $$; +-- } + +-- Table drop +-- { +CREATE OR REPLACE FUNCTION cdb_handle_drop_table () +RETURNS event_trigger SECURITY DEFINER LANGUAGE plpgsql AS $$ +DECLARE + event_info RECORD; +BEGIN + event_info := schema_triggers.get_relation_drop_eventinfo(); + + -- We're only interested in real relations + IF (event_info.old).relkind != 'r' THEN RETURN; END IF; + + RAISE DEBUG 'Relation % of kind % dropped from namespace oid %', + event_info.old_relation_oid, (event_info.old).relkind, (event_info.old).relnamespace; + + -- delete record from CDB_TableMetadata (should invalidate varnish) + DELETE FROM public.CDB_TableMetadata WHERE tabname = event_info.old_relation_oid; + END; $$; -- } @@ -62,7 +87,7 @@ BEGIN PERFORM cdb_enable_ddl_hooks(); - -- TODO: invalidate varnish + -- TODO: update CDB_TableMetadata.updated_at (should invalidate varnish) END; $$; -- } @@ -97,7 +122,7 @@ BEGIN PERFORM cdb_enable_ddl_hooks(); - -- TODO: invalidate varnish + -- TODO: update CDB_TableMetadata.updated_at (should invalidate varnish) END; $$; -- } @@ -126,13 +151,14 @@ BEGIN RETURN; END IF; - -- TODO: invalidate varnish + -- TODO: update CDB_TableMetadata.updated_at (should invalidate varnish) END; $$; -- } CREATE OR REPLACE FUNCTION cdb_disable_ddl_hooks() returns void AS $$ DROP EVENT TRIGGER IF EXISTS cdb_on_relation_create; + DROP EVENT TRIGGER IF EXISTS cdb_on_relation_drop; DROP EVENT TRIGGER IF EXISTS cdb_on_alter_column; DROP EVENT TRIGGER IF EXISTS cdb_on_drop_column; DROP EVENT TRIGGER IF EXISTS cdb_on_add_column; @@ -141,6 +167,7 @@ $$ LANGUAGE sql; CREATE OR REPLACE FUNCTION cdb_enable_ddl_hooks() returns void AS $$ SELECT cdb_disable_ddl_hooks(); CREATE EVENT TRIGGER cdb_on_relation_create ON "relation_create" EXECUTE PROCEDURE cdb_handle_create_table(); + CREATE EVENT TRIGGER cdb_on_relation_drop ON "relation_drop" EXECUTE PROCEDURE cdb_handle_drop_table(); CREATE EVENT TRIGGER cdb_on_alter_column ON "column_alter" EXECUTE PROCEDURE cdb_handle_alter_column(); CREATE EVENT TRIGGER cdb_on_drop_column ON "column_drop" EXECUTE PROCEDURE cdb_handle_drop_column(); CREATE EVENT TRIGGER cdb_on_add_column ON "column_add" EXECUTE PROCEDURE cdb_handle_add_column(); diff --git a/hook_on_table_create.sql b/hook_on_table_create.sql index e9b9a3a..e972ddc 100644 --- a/hook_on_table_create.sql +++ b/hook_on_table_create.sql @@ -10,12 +10,18 @@ create schema c; --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; alter table c.t3 rename column the_geom_webmercator to webmerc; select * from c.t3; +select tabname::text, updated_at from CDB_TableMetadata; alter table c.t3 rename column the_geom_webmercator to webmerc2; select * from c.t3; +select tabname::text, updated_at from CDB_TableMetadata; alter table c.t3 drop column the_geom_webmercator; select * from c.t3; +select tabname::text, updated_at from CDB_TableMetadata; alter table c.t3 add column id2 int; select * from c.t3; +select tabname::text, updated_at from CDB_TableMetadata; drop schema c cascade; +select tabname::text, updated_at from CDB_TableMetadata;