Merge pull request #350 from CartoDB/explicit-schema-for-ghost-tables

Add explicit schema for Ghost Tables functions
This commit is contained in:
Gonzalo Riestra 2019-03-13 10:42:13 +01:00 committed by GitHub
commit 4f3d19ce7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 15 deletions

View File

@ -1,5 +1,5 @@
-- Enqueues a job to run Ghost tables linking process for the provided username
CREATE OR REPLACE FUNCTION _CDB_LinkGhostTables(username text, db_name text, event_name text)
CREATE OR REPLACE FUNCTION cartodb._CDB_LinkGhostTables(username text, db_name text, event_name text)
RETURNS void
AS $$
if not username:
@ -46,37 +46,36 @@ AS $$
$$ LANGUAGE 'plpythonu' VOLATILE PARALLEL UNSAFE;
-- Enqueues a job to run Ghost tables linking process for the current user
CREATE OR REPLACE FUNCTION CDB_LinkGhostTables(event_name text DEFAULT 'USER')
CREATE OR REPLACE FUNCTION cartodb.CDB_LinkGhostTables(event_name text DEFAULT 'USER')
RETURNS void
AS $$
DECLARE
username TEXT;
db_name TEXT;
BEGIN
EXECUTE 'SELECT CDB_Username();' INTO username;
EXECUTE 'SELECT cartodb.CDB_Username();' INTO username;
EXECUTE 'SELECT current_database();' INTO db_name;
PERFORM _CDB_LinkGhostTables(username, db_name, event_name);
PERFORM cartodb._CDB_LinkGhostTables(username, db_name, event_name);
RAISE NOTICE '_CDB_LinkGhostTables() called with username=%, event_name=%', username, event_name;
END;
$$ LANGUAGE plpgsql VOLATILE PARALLEL UNSAFE SECURITY DEFINER;
-- Trigger function to call CDB_LinkGhostTables()
CREATE OR REPLACE FUNCTION _CDB_LinkGhostTablesTrigger()
CREATE OR REPLACE FUNCTION cartodb._CDB_LinkGhostTablesTrigger()
RETURNS trigger
AS $$
DECLARE
ddl_tag TEXT;
BEGIN
EXECUTE 'SELECT tag FROM cartodb.cdb_ddl_execution WHERE txid = txid_current();' INTO ddl_tag;
DELETE FROM cartodb.cdb_ddl_execution WHERE txid = txid_current();
PERFORM CDB_LinkGhostTables(ddl_tag);
EXECUTE 'DELETE FROM cartodb.cdb_ddl_execution WHERE txid = txid_current() RETURNING tag;' INTO ddl_tag;
PERFORM cartodb.CDB_LinkGhostTables(ddl_tag);
RETURN NULL;
END;
$$ LANGUAGE plpgsql VOLATILE PARALLEL UNSAFE SECURITY DEFINER;
-- Event trigger to save the current transaction in cartodb.cdb_ddl_execution
CREATE OR REPLACE FUNCTION CDB_SaveDDLTransaction()
CREATE OR REPLACE FUNCTION cartodb.CDB_SaveDDLTransaction()
RETURNS event_trigger
AS $$
BEGIN
@ -85,7 +84,7 @@ AS $$
$$ LANGUAGE plpgsql VOLATILE PARALLEL UNSAFE SECURITY DEFINER;
-- Creates the trigger on DDL events to link ghost tables
CREATE OR REPLACE FUNCTION CDB_EnableGhostTablesTrigger()
CREATE OR REPLACE FUNCTION cartodb.CDB_EnableGhostTablesTrigger()
RETURNS void
AS $$
BEGIN
@ -99,17 +98,17 @@ AS $$
AFTER INSERT ON cartodb.cdb_ddl_execution
INITIALLY DEFERRED
FOR EACH ROW
EXECUTE PROCEDURE _CDB_LinkGhostTablesTrigger();
EXECUTE PROCEDURE cartodb._CDB_LinkGhostTablesTrigger();
CREATE EVENT TRIGGER link_ghost_tables
ON ddl_command_end
WHEN TAG IN ('CREATE TABLE', 'SELECT INTO', 'DROP TABLE', 'ALTER TABLE', 'CREATE TRIGGER', 'DROP TRIGGER', 'CREATE VIEW', 'DROP VIEW', 'ALTER VIEW')
EXECUTE PROCEDURE CDB_SaveDDLTransaction();
EXECUTE PROCEDURE cartodb.CDB_SaveDDLTransaction();
END;
$$ LANGUAGE plpgsql VOLATILE PARALLEL UNSAFE;
-- Drops the trigger on DDL events to link ghost tables
CREATE OR REPLACE FUNCTION CDB_DisableGhostTablesTrigger()
CREATE OR REPLACE FUNCTION cartodb.CDB_DisableGhostTablesTrigger()
RETURNS void
AS $$
BEGIN

View File

@ -1,6 +1,6 @@
-- Returns the cartodb username of the current PostgreSQL session
CREATE OR REPLACE FUNCTION CDB_Username()
CREATE OR REPLACE FUNCTION cartodb.CDB_Username()
RETURNS text
AS $$
SELECT CDB_Conf_GetConf(CONCAT('api_keys_', session_user))->>'username';
SELECT cartodb.CDB_Conf_GetConf(CONCAT('api_keys_', session_user))->>'username';
$$ LANGUAGE SQL STABLE PARALLEL SAFE SECURITY DEFINER;