diff --git a/scripts-available/CDB_TableMetadata.sql b/scripts-available/CDB_TableMetadata.sql index 6f65606..bca7c47 100644 --- a/scripts-available/CDB_TableMetadata.sql +++ b/scripts-available/CDB_TableMetadata.sql @@ -66,9 +66,11 @@ CREATE OR REPLACE FUNCTION _CDB_TableMetadata_Updated() RETURNS trigger AS $$ DECLARE - tabname TEXT; + tabname regclass; rec RECORD; found BOOL; + schema_name TEXT; + table_name TEXT; BEGIN IF TG_OP = 'UPDATE' or TG_OP = 'INSERT' THEN @@ -103,9 +105,10 @@ BEGIN AND u.usesuper ORDER BY n.nspname LOOP + SELECT n.nspname, c.relname FROM pg_class c, pg_namespace n WHERE c.oid=tabname AND c.relnamespace = n.oid INTO schema_name, table_name; EXECUTE 'SELECT ' || quote_ident(rec.nspname) || '.' || quote_ident(rec.proname) - || '(' || quote_literal(tabname) || ')'; + || '(' || quote_literal(quote_ident(schema_name) || '.' || quote_ident(table_name)) || ')'; found := true; EXIT; END LOOP; @@ -116,11 +119,11 @@ END; $$ LANGUAGE plpgsql VOLATILE SECURITY DEFINER; -DROP TRIGGER IF EXISTS table_modified ON CDB_TableMetadata; +DROP TRIGGER IF EXISTS table_modified ON public.CDB_TableMetadata; -- NOTE: on DELETE we would be unable to convert the table -- oid (regclass) to its name CREATE TRIGGER table_modified AFTER INSERT OR UPDATE -ON CDB_TableMetadata FOR EACH ROW EXECUTE PROCEDURE +ON public.CDB_TableMetadata FOR EACH ROW EXECUTE PROCEDURE _CDB_TableMetadata_Updated(); diff --git a/test/extension/test.sh b/test/extension/test.sh index 389eba3..34f697d 100755 --- a/test/extension/test.sh +++ b/test/extension/test.sh @@ -350,6 +350,41 @@ function test_cdb_tablemetadatatouch_fails_from_user_without_permission() { sql postgres "REVOKE ALL ON CDB_TableMetadata FROM cdb_testmember_1;" } +function test_cdb_tablemetadatatouch_fully_qualifies_names() { + sql postgres "CREATE TABLE touch_invalidations (table_name text);" + sql postgres "create or replace function cartodb.cdb_invalidate_varnish(table_name text) returns void as \$\$ begin insert into touch_invalidations select table_name; end; \$\$ language 'plpgsql';" + + #default schema + sql "CREATE TABLE touch_example (a int);" + sql postgres "SELECT CDB_TableMetadataTouch('touch_example');" + sql postgres "SELECT table_name FROM touch_invalidations" should "public.touch_example" + sql postgres "TRUNCATE TABLE touch_invalidations" + sql postgres "DROP TABLE touch_example" + + #setup different schema + sql postgres "CREATE SCHEMA test_schema;" + sql postgres "CREATE TABLE test_schema.touch_example (a int);" + + #different schema outside search_path + sql postgres "SELECT CDB_TableMetadataTouch('test_schema.touch_example');" + sql postgres "SELECT table_name FROM touch_invalidations" should "test_schema.touch_example" + sql postgres "TRUNCATE TABLE touch_invalidations" + + #different schema in default search_path + sql postgres "SET search_path=test_schema,public,cartodb; SELECT CDB_TableMetadataTouch('test_schema.touch_example');" + sql postgres "SELECT table_name FROM touch_invalidations" should "test_schema.touch_example" + sql postgres "TRUNCATE TABLE touch_invalidations" + + #teardown different schema + sql postgres 'DROP TABLE test_schema.touch_example;' + sql postgres 'DROP SCHEMA test_schema;' + + + + sql postgres 'DROP FUNCTION cartodb.cdb_invalidate_varnish(table_name text);' + sql postgres 'DROP TABLE touch_invalidations' +} + function test_cdb_column_names() { sql cdb_testmember_1 'CREATE TABLE cdb_testmember_1.table_cnames(c int, a int, r int, t int, o int);' sql cdb_testmember_2 'CREATE TABLE cdb_testmember_2.table_cnames(d int, b int);'