Ejemplo n.º 1
0
def _get_db_object(CONF):
    sqlalchemy.engine.url.make_url(CONF.sql_connection)
    engine_args = {
        'pool_recycle': CONF.sql_idle_timeout,
        'echo': False,
        'convert_unicode': True}

    try:
        engine = sqlalchemy.create_engine(CONF.sql_connection, **engine_args)
        engine.connect = wrap_db_error(engine.connect, CONF)
        engine.connect()
    except Exception as err:
        msg = _("Error configuring registry database with supplied "
                "sql_connection '%s'. "
                "Got error:\n%s") % (CONF.sql_connection, err)
        LOG.error(msg)
        raise

    if CONF.db_auto_create:
        LOG.info(_('auto-creating staccato registry DB'))
        models.register_models(engine)
        try:
            migration.version_control(CONF)
        except exceptions.StaccatoDataBaseException:
            # only arises when the DB exists and is under version control
            pass
    else:
        LOG.info(_('not auto-creating staccato registry DB'))

    return engine
Ejemplo n.º 2
0
    try:
        engine = sqlalchemy.create_engine(CONF.sql_connection, **engine_args)
        engine.connect = wrap_db_error(engine.connect, CONF)
        engine.connect()
    except Exception, err:
        msg = _("Error configuring registry database with supplied "
                "sql_connection '%s'. "
                "Got error:\n%s") % (CONF.sql_connection, err)
        LOG.error(msg)
        raise

    if CONF.db_auto_create:
        LOG.info(_('auto-creating staccato registry DB'))
        models.register_models(engine)
        try:
            migration.version_control(CONF)
        except exceptions.StaccatoDataBaseException:
            # only arises when the DB exists and is under version control
            pass
    else:
        LOG.info(_('not auto-creating staccato registry DB'))

    return engine


def is_db_connection_error(args):
    """Return True if error in connecting to db."""
    # NOTE(adam_g): This is currently MySQL specific and needs to be extended
    #               to support Postgres and others.
    conn_err_codes = ('2002', '2003', '2006')
    for err_code in conn_err_codes: