class GlobalInit: def __init__(self, config_path='conf/config.json'): from sm.engine.db import ConnectionPool # pylint: disable=import-outside-toplevel self.sm_config = on_startup(config_path) self.pool = ConnectionPool(self.sm_config['db']) def __enter__(self): return self.sm_config def __exit__(self, ext_type, ext_value, traceback): self.pool.close()
def empty_test_db(sm_config): db_name = sm_config['db']['database'] db_owner = sm_config['db']['user'] _autocommit_execute( { **sm_config['db'], 'database': 'postgres' }, f'DROP DATABASE IF EXISTS {db_name}', f'CREATE DATABASE {db_name} OWNER {db_owner}', ) conn_pool = ConnectionPool(sm_config['db']) yield conn_pool.close() _autocommit_execute({ **sm_config['db'], 'database': 'postgres' }, f'DROP DATABASE IF EXISTS {db_name}')