From 7cf3030e80eee882c8acdecacb8794d713a7a08c Mon Sep 17 00:00:00 2001 From: Jonathan Gray Date: Thu, 6 Jan 2022 10:01:27 +0000 Subject: [PATCH] Add a trigger to make cache tables owned by flowmachine #4714 --- CHANGELOG.md | 1 + flowdb/bin/build/9000_last_create_roles.sh | 23 ++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d32e73ec1e4..8ab33744306 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Added ### Changed +- Tables created under the cache schema in FlowDB will automatically be set to be owned by the `flowmachine` user. [#4714](https://github.com/Flowminder/FlowKit/issues/4714) ### Fixed diff --git a/flowdb/bin/build/9000_last_create_roles.sh b/flowdb/bin/build/9000_last_create_roles.sh index 3d542112fe2..8d6fc7c02e1 100644 --- a/flowdb/bin/build/9000_last_create_roles.sh +++ b/flowdb/bin/build/9000_last_create_roles.sh @@ -186,3 +186,26 @@ psql --dbname="$POSTGRES_DB" -c " GRANT SELECT ON TABLES TO $FLOWAPI_FLOWDB_USER; GRANT USAGE ON SCHEMA geography TO $FLOWAPI_FLOWDB_USER; " + +# Create event trigger to change owner of tables under the cache schema +# Note that we hardcode the schema and username because event trigger functions cannot take params + +psql --dbname="$POSTGRES_DB" -c " + CREATE OR REPLACE FUNCTION trg_create_in_cache_set_owner_to_flowmachine() + RETURNS event_trigger + LANGUAGE plpgsql + AS \$\$ + DECLARE + obj record; + BEGIN + FOR obj IN SELECT * FROM pg_event_trigger_ddl_commands() WHERE command_tag='CREATE TABLE' AND schema_name='cache' LOOP + EXECUTE format('ALTER TABLE %s OWNER TO flowmachine', obj.object_identity); + END LOOP; + END; + \$\$; + + CREATE EVENT TRIGGER trg_create_in_cache_set_owner_to_flowmachine + ON ddl_command_end + WHEN tag IN ('CREATE TABLE') + EXECUTE PROCEDURE trg_create_in_cache_set_owner_to_flowmachine(); + " \ No newline at end of file