From 47a85d85315ae15f0a8f03cac4db3d810cc0d692 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Sat, 14 Aug 2021 11:19:32 +0200 Subject: [PATCH] feeder: add option post-sync-commands (#56164) Exemple: [wcs-olap] post-sync-commands = ALTER DEFAULT PRIVILEGES IN SCHEMA {schema} GRANT SELECT ON TABLES TO user; GRANT USAGE ON SCHEMA {schema} TO user; GRANT SELECT ON ALL TABLES IN SCHEMA {schema} TO user; Those commands will be launched after the synchronization, interpolation variables are usable (like {schema} for the target schema name). --- tests/test_feeder.py | 21 +++++++++++++++++++++ wcs_olap/feeder.py | 3 +++ 2 files changed, 24 insertions(+) diff --git a/tests/test_feeder.py b/tests/test_feeder.py index 32c16e4..47265f9 100644 --- a/tests/test_feeder.py +++ b/tests/test_feeder.py @@ -1,4 +1,5 @@ import logging +from unittest import mock import pytest @@ -20,3 +21,23 @@ def test_sql_error_logging(caplog): with pytest.raises(Exception): feeder.ex('COIN') assert 'Failed to execute' in caplog.text + + +def test_post_sync_commands(mock_cursor_execute, wcs, postgres_db, olap_cmd): + with olap_cmd.config() as config: + config.set( + 'wcs-olap', + 'post-sync-commands', + "NOTIFY coucou, '{schema}';\nSELECT * FROM information_schema.tables") + + queries = [] + + def side_effect(query, *args, **kwargs): + queries.append(query) + return mock.DEFAULT + + with mock_cursor_execute(side_effect=side_effect): + assert olap_cmd() == 0 + + # verify post-sync-commands are in the last executed queries + assert queries[-1] == "NOTIFY coucou, 'olap';\nSELECT * FROM information_schema.tables" diff --git a/wcs_olap/feeder.py b/wcs_olap/feeder.py index bd1dee4..85580f5 100644 --- a/wcs_olap/feeder.py +++ b/wcs_olap/feeder.py @@ -632,6 +632,9 @@ class WcsOlapFeeder(object): model_path = os.path.join(self.config['cubes_model_dirs'], '%s.model' % self.schema) with open(model_path, 'w') as f: json.dump(self.model, f, indent=2, sort_keys=True) + post_sync_commands = self.config.get('post-sync-commands') + if post_sync_commands: + self.ex(post_sync_commands) finally: # prevent connection from remaining open self.cur.close()