def connection(request, settings): """ sets up a SQLAlchemy engine and returns a connection to the database. :param settings: the settings of the test (given by the testing fixture) :returns: a sqlalchemy connection object """ # the following setup is based on `kotti.resources.initialize_sql`, # except that it explicitly binds the session to a specific connection # enabling us to use savepoints independent from the orm, thus allowing # to `rollback` after using `transaction.commit`... initialize_test_database(settings) from autonomie.models.base import DBSESSION, DBBASE engine = engine_from_config(settings, prefix='sqlalchemy.') _connection = engine.connect() DBSESSION.registry.clear() DBSESSION.configure(bind=_connection) DBBASE.metadata.bind = engine def drop_db(): """ drop the test database """ if __current_test_ini_file().endswith('travis.ini'): return options = get_test_options_from_settings(settings) cmd = "echo \"echo 'drop database {db};' | {mysql_cmd}\" | at now" launch_cmd(options, cmd) request.addfinalizer(drop_db) return _connection
def initialize_sql(engine): """ Initialize the database engine """ DBSESSION.configure(bind=engine) DBBASE.metadata.bind = engine if not engine.table_names(): fetch_head() DBBASE.metadata.create_all(engine) return DBSESSION
def initialize_sql(engine): """ Initialize the database engine """ DBSESSION.configure(bind=engine) DBBASE.metadata.bind = engine if not engine.table_names(): fetch_head() print("Setting the metadatas") DBBASE.metadata.create_all(engine) from transaction import commit commit() print("Populating the config") populate_config(DBSESSION()) return DBSESSION