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).
This commit is contained in:
Benjamin Dauvergne 2021-08-14 11:19:32 +02:00
parent 5baffc8156
commit 47a85d8531
2 changed files with 24 additions and 0 deletions

View File

@ -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"

View File

@ -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()