def connect_database(self, create_tables: bool = False) -> None: self.engine = get_engine(self.app_config) if create_tables: DeclarativeBase.metadata.create_all(self.engine) self.session_factory = get_session_factory(self.engine) self.session = get_tm_session(self.session_factory, transaction.manager)
def _populate_database( cls, settings: plaster_pastedeploy.ConfigDict, add_test_data: bool ) -> None: engine = get_engine(settings) session_factory = get_session_factory(engine) app_config = CFG(settings) print("- Populate database with default data -") with transaction.manager: dbsession = get_tm_session(session_factory, transaction.manager) try: fixtures = [BaseFixture] fixtures_loader = FixturesLoader(dbsession, app_config) fixtures_loader.loads(fixtures) transaction.commit() if add_test_data: app_config.configure_filedepot() fixtures = [ContentFixture] fixtures_loader.loads(fixtures) transaction.commit() print("Database initialized.") except IntegrityError as exc: transaction.abort() print('Database initialization failed') raise DatabaseInitializationFailed( 'Warning, there was a problem when adding default data' ', it may have already been added.' ) from exc
def __init__(self, application, config): super().__init__(application, config) self._application = application self.settings = config["tracim_settings"] self.app_config = CFG(self.settings) self.app_config.configure_filedepot() self.plugin_manager = init_plugin_manager(self.app_config) self.engine = get_engine(self.app_config) self.session_factory = get_session_factory(self.engine)
def setUp(self) -> None: self._set_logger() logger.debug(self, "Setup Test...") self.settings = plaster.get_settings(self.config_uri, self.config_section) self.config = testing.setUp(settings=self.settings) DepotManager._clear() DepotManager.configure( "test", {"depot.backend": "depot.io.memory.MemoryFileStorage"}) settings = self.config.get_settings() self.app_config = CFG(settings) init_models(self.config, self.app_config) from tracim_backend.models.setup_models import ( get_engine, get_session_factory, get_tm_session, ) self.engine = get_engine(self.app_config) self.session_factory = get_session_factory(self.engine) self.init_database() self.session = get_tm_session(self.session_factory, transaction.manager)
def worker_context() -> typing.Generator[TracimContext, None, None]: """Create a tracim context with a db session. The session is created using the current DatabaseWorker's engine. This context manager MUST be used through a RQ job that is executed by a DatabaseWorker worker which can be started with: rq worker -w tracim_backend.lib.rq.worker.DatabaseWorker """ engine = _engines.top config = _configs.top assert config and engine, "Can only be called in a RQ job" context = RqWorkerTracimContext(config=config) session_factory = get_session_factory(engine) session = create_dbsession_for_context(session_factory, transaction.manager, context) context._dbsession = session try: yield context transaction.commit() except Exception: transaction.abort() raise finally: context.cleanup()
def session_factory(engine): return get_session_factory(engine)