Example #1
0
def data_migration_db(request) -> Generator:
    # Data migrations performed before the API goes up, therefore there's no project member yet
    # that's the only difference between this fixture and the db fixture. because of the parameterization it was hard to
    # share code between them, we anyway going to remove filedb soon, then there won't be params, and we could re-use
    # code
    # TODO: fix duplication
    if request.param == "sqldb":
        dsn = "sqlite:///:memory:?check_same_thread=false"
        config.httpdb.dsn = dsn
        _init_engine()

        # memory sqldb remove it self when all session closed, this session will keep it up during all test
        db_session = create_session()
        try:
            init_data()
            db = SQLDB(dsn)
            db.initialize(db_session)
            initialize_db(db)
            yield db
        finally:
            close_session(db_session)
    elif request.param == "filedb":
        db = FileDB(config.httpdb.dirpath)
        db_session = create_session(request.param)
        try:
            db.initialize(db_session)

            yield db
        finally:
            shutil.rmtree(config.httpdb.dirpath,
                          ignore_errors=True,
                          onerror=None)
            close_session(db_session)
    else:
        raise Exception("Unknown db type")
Example #2
0
def db(request) -> Generator:
    if request.param == "sqldb":
        dsn = "sqlite:///:memory:?check_same_thread=false"
        config.httpdb.dsn = dsn
        _init_engine()

        # memory sqldb remove it self when all session closed, this session will keep it up during all test
        db_session = create_session()
        try:
            init_data()
            db = SQLDB(dsn)
            db.initialize(db_session)
            initialize_db(db)
            initialize_project_member()
            yield db
        finally:
            close_session(db_session)
    elif request.param == "filedb":
        db = FileDB(config.httpdb.dirpath)
        db_session = create_session(request.param)
        try:
            db.initialize(db_session)

            yield db
        finally:
            shutil.rmtree(config.httpdb.dirpath,
                          ignore_errors=True,
                          onerror=None)
            close_session(db_session)
    else:
        raise Exception("Unknown db type")
Example #3
0
def _initialize_db():
    global db
    if config.httpdb.db_type == "filedb":
        logger.info("using FileRunDB")
        db = FileDB(config.httpdb.dirpath)
        db.initialize(None)
    else:
        logger.info("using SQLDB")
        db = SQLDB(config.httpdb.dsn)
        db_session = None
        try:
            db_session = create_session()
            db.initialize(db_session)
        finally:
            db_session.close()
Example #4
0
def initialize_db(override_db=None):
    global db
    if override_db:
        db = override_db
        return
    if config.httpdb.db_type == "filedb":
        logger.info("Creating file db")
        db = FileDB(config.httpdb.dirpath)
        db.initialize(None)
    else:
        logger.info("Creating sql db")
        db = SQLDB(config.httpdb.dsn)
        db_session = None
        try:
            db_session = create_session()
            db.initialize(db_session)
        finally:
            db_session.close()