Skip to content

Commit

Permalink
Add a trigger to make cache tables owned by flowmachine #4714
Browse files Browse the repository at this point in the history
  • Loading branch information
greenape committed Jan 7, 2022
1 parent ac299dc commit 0e93864
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 23 additions & 0 deletions flowdb/bin/build/9000_last_create_roles.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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();
"

0 comments on commit 0e93864

Please sign in to comment.