Python sqlalchemy.exc.DatabaseError() Examples

The following are 30 code examples of sqlalchemy.exc.DatabaseError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sqlalchemy.exc , or try the search function .
Example #1
Source File: upgrade.py    From amir with GNU General Public License v3.0 6 votes vote down vote up
def checkInputDb(inputfile):
    try:
        engine = create_engine('sqlite:///%s' % inputfile, echo=True)
    except exc.DatabaseError:
        logging.error(sys.exc_info()[0])
        return -2
    metadata = MetaData(bind=engine)

    try:
        table = Table('ledger', metadata, autoload=True)
        table = Table('sub_ledger', metadata, autoload=True)
        table = Table('moin', metadata, autoload=True)

    except exc.DatabaseError:
        logging.error(sys.exc_info()[0])
        return -2
    except exc.NoSuchTableError:
        return -1
    return 0 
Example #2
Source File: orm_repo.py    From monasca-notification with Apache License 2.0 6 votes vote down vote up
def get_notification(self, notification_id):
        try:
            with self._orm_engine.connect() as conn:
                LOG.debug('Orm query {%s}', str(self._orm_get_notification))
                result = conn.execute(self._orm_get_notification,
                                      notification_id=notification_id)
                notification = result.fetchone()
                if notification is None:
                    return None
                else:
                    return [
                        notification[0],
                        notification[1].lower(),
                        notification[2],
                        notification[3]]
        except DatabaseError as e:
            LOG.exception("Couldn't fetch the notification method %s", e)
            raise exc.DatabaseException(e) 
Example #3
Source File: test_lockmode.py    From sqlalchemy with MIT License 6 votes vote down vote up
def test_outer_joinedload_w_limit(self):
        User = self.classes.User
        sess = Session()
        q = sess.query(User).options(
            joinedload(User.addresses, innerjoin=False)
        )

        if testing.against("postgresql"):
            q = q.with_for_update(of=User)
        else:
            q = q.with_for_update()

        q = q.limit(1)

        if testing.against("oracle"):
            assert_raises_message(exc.DatabaseError, "ORA-02014", q.all)
        else:
            q.all()
        sess.close() 
Example #4
Source File: finisher.py    From rucio with Apache License 2.0 6 votes vote down vote up
def __handle_terminated_replicas(replicas, prepend_str=''):
    """
    Used by finisher to handle available and unavailable replicas.

    :param replicas: List of replicas.
    :param prepend_str: String to prepend to logging.
    """

    for req_type in replicas:
        for rule_id in replicas[req_type]:
            try:
                __update_bulk_replicas(replicas[req_type][rule_id])
            except (UnsupportedOperation, ReplicaNotFound):
                # one replica in the bulk cannot be found. register it one by one
                logging.warn('%s Problem to bulk update the replicas states. Will try one by one', prepend_str)
                for replica in replicas[req_type][rule_id]:
                    try:
                        __update_replica(replica)
                    except (DatabaseException, DatabaseError) as error:
                        if re.match('.*ORA-00054.*', error.args[0]) or re.match('.*ORA-00060.*', error.args[0]) or 'ERROR 1205 (HY000)' in error.args[0]:
                            logging.warn("%s Locks detected when handling replica %s:%s at RSE %s", prepend_str, replica['scope'], replica['name'], replica['rse_id'])
                        else:
                            logging.error("%s Could not finish handling replicas %s:%s at RSE %s (%s)", prepend_str, replica['scope'], replica['name'], replica['rse_id'], traceback.format_exc())
                    except Exception as error:
                        logging.error("%s Something unexpected happened when updating replica state for transfer %s:%s at %s (%s)", prepend_str, replica['scope'], replica['name'], replica['rse_id'], str(error))
            except (DatabaseException, DatabaseError) as error:
                if re.match('.*ORA-00054.*', error.args[0]) or re.match('.*ORA-00060.*', error.args[0]) or 'ERROR 1205 (HY000)' in error.args[0]:
                    logging.warn("%s Locks detected when handling replicas on %s rule %s, update updated time.", prepend_str, req_type, rule_id)
                    try:
                        request_core.touch_requests_by_rule(rule_id)
                    except (DatabaseException, DatabaseError):
                        logging.error("%s Failed to touch requests by rule(%s): %s", prepend_str, rule_id, traceback.format_exc())
                else:
                    logging.error("%s Could not finish handling replicas on %s rule %s: %s", prepend_str, req_type, rule_id, traceback.format_exc())
            except Exception as error:
                logging.error("%s Something unexpected happened when handling replicas on %s rule %s: %s", prepend_str, req_type, rule_id, str(error)) 
Example #5
Source File: test_except.py    From sqlalchemy with MIT License 6 votes vote down vote up
def test_tostring_with_newlines(self):
        try:
            raise sa_exceptions.DBAPIError.instance(
                "this is a message\nthis is the next line\nthe last line",
                None,
                OperationalError(),
                DatabaseError,
            )
        except sa_exceptions.DBAPIError as exc:
            eq_(
                str(exc),
                "(test.base.test_except.OperationalError) \n"
                "[SQL: this is a message\nthis is the next line\n"
                "the last line]\n"
                "(Background on this error at: http://sqlalche.me/e/%s/e3q8)"
                % sa_exceptions._version_token,
            ) 
Example #6
Source File: replica.py    From rucio with Apache License 2.0 6 votes vote down vote up
def touch_collection_replicas(collection_replicas, session=None):
    """
    Update the accessed_at timestamp of the given collection replicas.

    :param collection_replicas: the list of collection replicas.
    :param session: The database session in use.

    :returns: True, if successful, False otherwise.
    """

    now = datetime.utcnow()
    for collection_replica in collection_replicas:
        try:
            session.query(models.CollectionReplica).filter_by(scope=collection_replica['scope'], name=collection_replica['name'], rse_id=collection_replica['rse_id']).\
                update({'accessed_at': collection_replica.get('accessed_at') or now}, synchronize_session=False)
        except DatabaseError:
            return False

    return True 
Example #7
Source File: test_except.py    From sqlalchemy with MIT License 6 votes vote down vote up
def test_statement_error_w_code(self):
        try:
            raise sa_exceptions.DBAPIError.instance(
                "select * from table",
                [{"x": 1}],
                sa_exceptions.InvalidRequestError("hello", code="abcd"),
                DatabaseError,
            )
        except sa_exceptions.StatementError as err:
            eq_(
                str(err),
                "(sqlalchemy.exc.InvalidRequestError) hello\n"
                "[SQL: select * from table]\n"
                "[parameters: [{'x': 1}]]\n"
                "(Background on this error at: http://sqlalche.me/e/%s/abcd)"
                % sa_exceptions._version_token,
            )
            eq_(err.args, ("(sqlalchemy.exc.InvalidRequestError) hello",)) 
Example #8
Source File: did.py    From rucio with Apache License 2.0 6 votes vote down vote up
def touch_dids(dids, session=None):
    """
    Update the accessed_at timestamp and the access_cnt of the given dids.

    :param replicas: the list of dids.
    :param session: The database session in use.

    :returns: True, if successful, False otherwise.
    """

    now = datetime.utcnow()
    none_value = None
    try:
        for did in dids:
            session.query(models.DataIdentifier).\
                filter_by(scope=did['scope'], name=did['name'], did_type=did['type']).\
                update({'accessed_at': did.get('accessed_at') or now,
                        'access_cnt': case([(models.DataIdentifier.access_cnt == none_value, 1)],
                                           else_=(models.DataIdentifier.access_cnt + 1))},
                       synchronize_session=False)
    except DatabaseError:
        return False

    return True 
Example #9
Source File: test_except.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_db_error_noncompliant_dbapi(self):
        try:
            raise sa_exceptions.DBAPIError.instance(
                "", [], OutOfSpec(), DatabaseError
            )
        except sa_exceptions.DBAPIError as e:
            # OutOfSpec subclasses DatabaseError
            self.assert_(e.__class__ is sa_exceptions.DatabaseError)
        except OutOfSpec:
            self.assert_(False)

        try:
            raise sa_exceptions.DBAPIError.instance(
                "", [], sa_exceptions.ArgumentError(), DatabaseError
            )
        except sa_exceptions.DBAPIError as e:
            self.assert_(e.__class__ is sa_exceptions.DBAPIError)
        except sa_exceptions.ArgumentError:
            self.assert_(False)

        dialect = self._translating_dialect_fixture()
        try:
            raise sa_exceptions.DBAPIError.instance(
                "",
                [],
                sa_exceptions.ArgumentError(),
                DatabaseError,
                dialect=dialect,
            )
        except sa_exceptions.DBAPIError as e:
            self.assert_(e.__class__ is sa_exceptions.DBAPIError)
        except sa_exceptions.ArgumentError:
            self.assert_(False) 
Example #10
Source File: orm_repo.py    From monasca-notification with Apache License 2.0 5 votes vote down vote up
def get_alarm_current_state(self, alarm_id):
        try:
            with self._orm_engine.connect() as conn:
                LOG.debug('Orm query {%s}', str(self._orm_get_alarm_state))
                result = conn.execute(self._orm_get_alarm_state,
                                      alarm_id=alarm_id)
                row = result.fetchone()
                state = row[0] if row is not None else None
                return state
        except DatabaseError as e:
            LOG.exception("Couldn't fetch the current alarm state %s", e)
            raise exc.DatabaseException(e) 
Example #11
Source File: orm_repo.py    From monasca-notification with Apache License 2.0 5 votes vote down vote up
def fetch_notification_method_types(self):
        try:
            with self._orm_engine.connect() as conn:
                LOG.debug('Orm query {%s}', str(self._orm_nmt_query))
                notification_method_types = conn.execute(self._orm_nmt_query).fetchall()

                return [row[0] for row in notification_method_types]
        except DatabaseError as e:
            LOG.exception("Couldn't fetch notification method types %s", e)
            raise exc.DatabaseException(e) 
Example #12
Source File: orm_repo.py    From monasca-notification with Apache License 2.0 5 votes vote down vote up
def insert_notification_method_types(self, notification_types):
        # This function can be called by multiple processes at same time.
        # In that case, only the first one will succeed and the others will fail.
        # We can ignore this error when the types have already been registered.
        try:
            with self._orm_engine.connect() as conn:
                for notification_type in notification_types:
                    conn.execute(self._orm_add_notification_type, b_name=notification_type)

        except DatabaseError as e:
            LOG.debug("Failed to insert notification types %s", notification_types)
            raise exc.DatabaseException(e) 
Example #13
Source File: test_except.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_db_error_normal(self):
        try:
            raise sa_exceptions.DBAPIError.instance(
                "", [], OperationalError(), DatabaseError
            )
        except sa_exceptions.DBAPIError:
            self.assert_(True) 
Example #14
Source File: test_except.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_tostring(self):
        try:
            raise sa_exceptions.DBAPIError.instance(
                "this is a message", None, OperationalError(), DatabaseError
            )
        except sa_exceptions.DBAPIError as exc:
            eq_(
                str(exc),
                "(test.base.test_except.OperationalError) \n"
                "[SQL: this is a message]\n"
                "(Background on this error at: http://sqlalche.me/e/%s/e3q8)"
                % sa_exceptions._version_token,
            ) 
Example #15
Source File: test_except.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_statement_error_no_code(self):
        try:
            raise sa_exceptions.DBAPIError.instance(
                "select * from table",
                [{"x": 1}],
                sa_exceptions.InvalidRequestError("hello"),
                DatabaseError,
            )
        except sa_exceptions.StatementError as err:
            eq_(
                str(err),
                "(sqlalchemy.exc.InvalidRequestError) hello\n"
                "[SQL: select * from table]\n[parameters: [{'x': 1}]]",
            )
            eq_(err.args, ("(sqlalchemy.exc.InvalidRequestError) hello",)) 
Example #16
Source File: test_except.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_tostring_large_list(self):
        try:
            raise sa_exceptions.DBAPIError.instance(
                "this is a message",
                [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
                OperationalError(),
                DatabaseError,
            )
        except sa_exceptions.DBAPIError as ex:
            assert str(ex).startswith(
                "(test.base.test_except.OperationalError) \n"
                "[SQL: this is a message]\n[parameters: "
                "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]]"
            ) 
Example #17
Source File: test_except.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_db_error_busted_dbapi(self):
        try:
            raise sa_exceptions.DBAPIError.instance(
                "", [], ProgrammingError(), DatabaseError
            )
        except sa_exceptions.DBAPIError as e:
            self.assert_(True)
            self.assert_("Error in str() of DB-API" in e.args[0]) 
Example #18
Source File: provision.py    From android_universal with MIT License 5 votes vote down vote up
def _ora_drop_ignore(conn, dbname):
    try:
        conn.execute("drop user %s cascade" % dbname)
        log.info("Reaped db: %s" % dbname)
        return True
    except exc.DatabaseError as err:
        log.warn("couldn't drop db: %s" % err)
        return False 
Example #19
Source File: test_except.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_db_error_dbapi_uses_wrong_names(self):
        dialect = self._translating_dialect_fixture()

        try:
            raise sa_exceptions.DBAPIError.instance(
                "", [], IntegrityError(), DatabaseError, dialect=dialect
            )
        except sa_exceptions.DBAPIError as e:
            self.assert_(e.__class__ is sa_exceptions.IntegrityError)

        try:
            raise sa_exceptions.DBAPIError.instance(
                "",
                [],
                SpecificIntegrityError(),
                DatabaseError,
                dialect=dialect,
            )
        except sa_exceptions.DBAPIError as e:
            self.assert_(e.__class__ is sa_exceptions.IntegrityError)

        try:
            raise sa_exceptions.DBAPIError.instance(
                "", [], SpecificIntegrityError(), DatabaseError
            )
        except sa_exceptions.DBAPIError as e:
            # doesn't work without a dialect
            self.assert_(e.__class__ is not sa_exceptions.IntegrityError) 
Example #20
Source File: test_except.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_db_error_keyboard_interrupt(self):
        try:
            raise sa_exceptions.DBAPIError.instance(
                "", [], KeyboardInterrupt(), DatabaseError
            )
        except sa_exceptions.DBAPIError:
            self.assert_(False)
        except KeyboardInterrupt:
            self.assert_(True) 
Example #21
Source File: test_dialect.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_limit_offset_for_update(self):
        metadata = self.metadata
        # oracle can't actually do the ROWNUM thing with FOR UPDATE
        # very well.

        t = Table(
            "t1",
            metadata,
            Column("id", Integer, primary_key=True),
            Column("data", Integer),
        )
        metadata.create_all()

        t.insert().execute(
            {"id": 1, "data": 1},
            {"id": 2, "data": 7},
            {"id": 3, "data": 12},
            {"id": 4, "data": 15},
            {"id": 5, "data": 32},
        )

        # here, we can't use ORDER BY.
        eq_(
            t.select().with_for_update().limit(2).execute().fetchall(),
            [(1, 1), (2, 7)],
        )

        # here, its impossible.  But we'd prefer it to raise ORA-02014
        # instead of issuing a syntax error.
        assert_raises_message(
            exc.DatabaseError,
            "ORA-02014",
            t.select().with_for_update().limit(2).offset(3).execute,
        ) 
Example #22
Source File: test_lockmode.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_inner_joinedload_w_limit(self):
        User = self.classes.User
        sess = Session()
        q = (
            sess.query(User)
            .options(joinedload(User.addresses, innerjoin=True))
            .with_for_update()
            .limit(1)
        )

        if testing.against("oracle"):
            assert_raises_message(exc.DatabaseError, "ORA-02014", q.all)
        else:
            q.all()
        sess.close() 
Example #23
Source File: provision.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
def _ora_drop_ignore(conn, dbname):
    try:
        conn.execute("drop user %s cascade" % dbname)
        log.info("Reaped db: %s", dbname)
        return True
    except exc.DatabaseError as err:
        log.warning("couldn't drop db: %s", err)
        return False 
Example #24
Source File: provision.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
def _mssql_drop_ignore(conn, ident):
    try:
        # typically when this happens, we can't KILL the session anyway,
        # so let the cleanup process drop the DBs
        # for row in conn.execute("select session_id from sys.dm_exec_sessions "
        #        "where database_id=db_id('%s')" % ident):
        #    log.info("killing SQL server sesssion %s", row['session_id'])
        #    conn.execute("kill %s" % row['session_id'])

        conn.execute("drop database %s" % ident)
        log.info("Reaped db: %s", ident)
        return True
    except exc.DatabaseError as err:
        log.warning("couldn't drop db: %s", err)
        return False 
Example #25
Source File: provision.py    From android_universal with MIT License 5 votes vote down vote up
def _ora_drop_ignore(conn, dbname):
    try:
        conn.execute("drop user %s cascade" % dbname)
        log.info("Reaped db: %s", dbname)
        return True
    except exc.DatabaseError as err:
        log.warning("couldn't drop db: %s", err)
        return False 
Example #26
Source File: provision.py    From android_universal with MIT License 5 votes vote down vote up
def _mssql_drop_ignore(conn, ident):
    try:
        # typically when this happens, we can't KILL the session anyway,
        # so let the cleanup process drop the DBs
        # for row in conn.execute("select session_id from sys.dm_exec_sessions "
        #        "where database_id=db_id('%s')" % ident):
        #    log.info("killing SQL server sesssion %s", row['session_id'])
        #    conn.execute("kill %s" % row['session_id'])

        conn.execute("drop database %s" % ident)
        log.info("Reaped db: %s", ident)
        return True
    except exc.DatabaseError as err:
        log.warning("couldn't drop db: %s", err)
        return False 
Example #27
Source File: users.py    From vault with MIT License 5 votes vote down vote up
def validation_key_validate(key):
    """
        Verify if a validation key is valid
    """

    # validation key from database
    try:
        user = get_session().query(UserModel).filter(
            UserModel.key == 'key_validation').order_by(UserModel.id.desc()).first()
    except exc.DatabaseError:  # In case of encrypted db, if the encryption key is invalid
        # Drop db sessions to force a re-connection with the new key
        drop_sessions()

        return False

    # Concatenate user given key and config's salt
    key_salt = key + global_scope['conf'].salt.encode()

    # Key is valid
    try:
        if global_scope['enc'].decrypt(user.value) == key_salt:
            return True
    except ValueError:  # Decryption error
        return False

    return False 
Example #28
Source File: did.py    From rucio with Apache License 2.0 5 votes vote down vote up
def set_new_dids(dids, new_flag, session=None):
    """
    Set/reset the flag new

    :param dids: A list of dids
    :param new_flag: A boolean to flag new DIDs.
    :param session: The database session in use.
    """
    if session.bind.dialect.name == 'postgresql':
        new_flag = bool(new_flag)
    for did in dids:
        try:

            rowcount = session.query(models.DataIdentifier).\
                filter_by(scope=did['scope'], name=did['name']).\
                update({'is_new': new_flag}, synchronize_session=False)
            if not rowcount:
                raise exception.DataIdentifierNotFound("Data identifier '%s:%s' not found" % (did['scope'], did['name']))
        except DatabaseError as error:
            raise exception.DatabaseException('%s : Cannot update %s:%s' % (error.args[0], did['scope'], did['name']))
    try:
        session.flush()
    except IntegrityError as error:
        raise exception.RucioException(error.args[0])
    except DatabaseError as error:
        raise exception.RucioException(error.args[0])
    return True 
Example #29
Source File: provision.py    From jbox with MIT License 5 votes vote down vote up
def _ora_drop_ignore(conn, dbname):
    try:
        conn.execute("drop user %s cascade" % dbname)
        log.info("Reaped db: %s", dbname)
        return True
    except exc.DatabaseError as err:
        log.warning("couldn't drop db: %s", err)
        return False 
Example #30
Source File: provision.py    From jbox with MIT License 5 votes vote down vote up
def _ora_drop_ignore(conn, dbname):
    try:
        conn.execute("drop user %s cascade" % dbname)
        log.info("Reaped db: %s" % dbname)
        return True
    except exc.DatabaseError as err:
        log.warn("couldn't drop db: %s" % err)
        return False