diff --git a/scripts-available/CDB_ForeignTable.sql b/scripts-available/CDB_ForeignTable.sql index a284efc..d48465b 100644 --- a/scripts-available/CDB_ForeignTable.sql +++ b/scripts-available/CDB_ForeignTable.sql @@ -241,14 +241,24 @@ BEGIN END $$ LANGUAGE plpgsql VOLATILE PARALLEL UNSAFE SECURITY DEFINER; + +-- A function to drop a user-defined foreign server and all related objects +-- It does not read from CDB_Conf +-- +-- Sample call: +-- SELECT cartodb.CDB_Drop_User_Foreign_Server('amazon') +-- +-- Note: if there's any dependent object (i.e. foreign table) this call will fail CREATE OR REPLACE FUNCTION @extschema@.CDB_Drop_User_Foreign_Server(fdw_name NAME) RETURNS void AS $$ BEGIN EXECUTE FORMAT ('DROP SCHEMA %I', fdw_name); EXECUTE FORMAT ('DROP USER MAPPING FOR public SERVER %I', fdw_name); EXECUTE FORMAT ('DROP SERVER %I', fdw_name); + EXECUTE FORMAT ('REVOKE USAGE ON FOREIGN DATA WRAPPER postgres_fdw FROM %I', fdw_name); + EXECUTE FORMAT ('DROP ROLE %I', fdw_name); END -$$ LANGUAGE plpgsql VOLATILE PARALLEL UNSAFE; +$$ LANGUAGE plpgsql VOLATILE PARALLEL UNSAFE SECURITY DEFINER; -- Set up a user foreign table diff --git a/test/extension/test.sh b/test/extension/test.sh index d93d89b..c4af8b7 100755 --- a/test/extension/test.sh +++ b/test/extension/test.sh @@ -626,6 +626,9 @@ EOF sql cdb_testmember_2 "SELECT a from test_user_fdw.foo LIMIT 1;" should 42 sql cdb_testmember_1 "SELECT cartodb.CDB_Organization_Revoke_Role('test_user_fdw');" + # If there are dependent objects, we cannot drop the foreign server + sql cdb_testmember_1 "SELECT cartodb.CDB_Drop_User_Foreign_Server('test_user_fdw')" fails + # Teardown DATABASE=fdw_target sql postgres 'REVOKE USAGE ON SCHEMA test_fdw FROM fdw_user;'