本文整理汇总了Python中sqlalchemy.exc方法的典型用法代码示例。如果您正苦于以下问题:Python sqlalchemy.exc方法的具体用法?Python sqlalchemy.exc怎么用?Python sqlalchemy.exc使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy
的用法示例。
在下文中一共展示了sqlalchemy.exc方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _retry_on_exceptions
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import exc [as 别名]
def _retry_on_exceptions(exc):
if not isinstance(exc, exception.DBError):
return False
inn_e = exc.inner_exception
if not isinstance(inn_e, sqlalchemy.exc.InternalError):
return False
return ((
pymysql and
isinstance(inn_e.orig, pymysql.err.InternalError) and
(inn_e.orig.args[0] == pymysql.constants.ER.TABLE_DEF_CHANGED)
) or (
# HACK(jd) Sometimes, PostgreSQL raises an error such as "current
# transaction is aborted, commands ignored until end of transaction
# block" on its own catalog, so we need to retry, but this is not
# caught by oslo.db as a deadlock. This is likely because when we use
# Base.metadata.create_all(), sqlalchemy itself gets an error it does
# not catch or something. So this is why this function exists. To
# paperover I guess.
psycopg2
and isinstance(inn_e.orig, psycopg2.InternalError)
# current transaction is aborted
and inn_e.orig.pgcode == '25P02'
))
示例2: del_rse
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import exc [as 别名]
def del_rse(rse_id, session=None):
"""
Disable a rse with the given rse id.
:param rse_id: the rse id.
:param session: The database session in use.
"""
old_rse = None
try:
old_rse = session.query(models.RSE).filter_by(id=rse_id, deleted=False).one()
if not rse_is_empty(rse_id=rse_id, session=session):
raise exception.RSEOperationNotSupported('RSE \'%s\' is not empty' % get_rse_name(rse_id=rse_id, session=session))
except sqlalchemy.orm.exc.NoResultFound:
raise exception.RSENotFound('RSE with id \'%s\' cannot be found' % rse_id)
rse = old_rse.rse
old_rse.delete(session=session)
try:
del_rse_attribute(rse_id=rse_id, key=rse, session=session)
except exception.RSEAttributeNotFound:
pass
示例3: restore_rse
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import exc [as 别名]
def restore_rse(rse_id, session=None):
"""
Restore a rse with the given rse id.
:param rse_id: the rse id.
:param session: The database session in use.
"""
old_rse = None
try:
old_rse = session.query(models.RSE).filter_by(id=rse_id, deleted=True).one()
except sqlalchemy.orm.exc.NoResultFound:
raise exception.RSENotFound('RSE with id \'%s\' cannot be found' % rse_id)
old_rse.deleted = False
old_rse.deleted_at = None
old_rse.save(session=session)
rse = old_rse.rse
add_rse_attribute(rse_id=rse_id, key=rse, value=True, session=session)
示例4: get_rse
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import exc [as 别名]
def get_rse(rse_id, session=None):
"""
Get a RSE or raise if it does not exist.
:param rse_id: The rse id.
:param session: The database session in use.
:raises RSENotFound: If referred RSE was not found in the database.
"""
false_value = False # To make pep8 checker happy ...
try:
tmp = session.query(models.RSE).\
filter(sqlalchemy.and_(models.RSE.deleted == false_value,
models.RSE.id == rse_id))\
.one()
tmp['type'] = tmp.rse_type
return tmp
except sqlalchemy.orm.exc.NoResultFound:
raise exception.RSENotFound('RSE with id \'%s\' cannot be found' % rse_id)
示例5: get_schema_version
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import exc [as 别名]
def get_schema_version(engine):
results = None
# try the new style storage
try:
results = engine.execute(
sqlalchemy.text("SELECT value FROM storage_data WHERE namespace = :namespace AND key = 'schema_version'"),
namespace=_metadata_namespace
).fetchone()
except sqlalchemy.exc.DatabaseError:
pass
if results:
return _metadata_serializer.loads(results[0])
# try the old style storage
try:
results = engine.execute(sqlalchemy.text("SELECT value_type, value FROM meta_data WHERE id = 'schema_version'")).fetchone()
except sqlalchemy.exc.DatabaseError:
pass
if results:
value_type, value = results
if value_type != 'int':
raise TypeError('the value is not of type: int')
return int(value)
return models.SCHEMA_VERSION
示例6: error
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import exc [as 别名]
def error(exc, request):
"""Error method to return jsonapi compliant errors."""
request.response.content_type = 'application/vnd.api+json'
request.response.status_code = exc.code
errors = {
'errors': [
{
'code': str(exc.code),
'detail': exc.detail,
'title': exc.title,
}
]
}
if asbool(request.registry.settings.get('pyramid_jsonapi.debug_traceback', False)):
errors['traceback'] = traceback.format_exc()
return errors
示例7: test_ensure_backend_available_no_connection_raises
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import exc [as 别名]
def test_ensure_backend_available_no_connection_raises(self):
log = self.useFixture(fixtures.FakeLogger())
err = OperationalError("Can't connect to database", None, None)
with mock.patch.object(
sqlalchemy.engine.base.Engine, 'connect') as mock_connect:
mock_connect.side_effect = err
exc = self.assertRaises(
exception.BackendNotAvailable,
provision.Backend._ensure_backend_available,
self.connect_string)
self.assertEqual(
"Backend 'postgresql' is unavailable: "
"Could not connect", str(exc))
self.assertEqual(
"The postgresql backend is unavailable: %s" % err,
log.output.strip())
示例8: test_ensure_backend_available_no_dbapi_raises
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import exc [as 别名]
def test_ensure_backend_available_no_dbapi_raises(self):
log = self.useFixture(fixtures.FakeLogger())
with mock.patch.object(sqlalchemy, 'create_engine') as mock_create:
mock_create.side_effect = ImportError(
"Can't import DBAPI module foobar")
exc = self.assertRaises(
exception.BackendNotAvailable,
provision.Backend._ensure_backend_available,
self.connect_string)
mock_create.assert_called_once_with(
sa_url.make_url(self.connect_string))
self.assertEqual(
"Backend 'postgresql' is unavailable: "
"No DBAPI installed", str(exc))
self.assertEqual(
"The postgresql backend is unavailable: Can't import "
"DBAPI module foobar", log.output.strip())
示例9: __call__
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import exc [as 别名]
def __call__(self):
"""
Alias for serialize
"""
# try:
[idx.drop(bind=self.engine) for idx in Orf.__table__.indexes]
self.serialize()
[idx.create(bind=self.engine) for idx in Orf.__table__.indexes]
# except (sqlalchemy.exc.IntegrityError, sqlite3.IntegrityError) as exc:
# self.logger.error("DB corrupted, reloading data. Error: %s",
# exc)
# self.session.query(Query).delete()
# self.session.query(Orf).delete()
# self.serialize()
# except InvalidSerialization:
# raise
示例10: scoped_session
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import exc [as 别名]
def scoped_session(self, rollback=True, fail=False):
"""
Provide a transactional scope around a series of operations.
:param rollback: rollback transactions after catch
:param fail: raise error after catch
:type rollback: bool
:type fail: bool
"""
session = self._Session()
try:
yield session
# commit transactions
session.commit()
# catch errors
except sqlalchemy.exc.SQLAlchemyError:
# log the error
logging.exception('caught SQL exception')
# rollback transactions
if rollback:
session.rollback()
# reraise error
if fail:
raise RuntimeError('SQL Failed')
# cleanup
finally:
session.close()
示例11: get_rse_id
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import exc [as 别名]
def get_rse_id(rse, session=None, include_deleted=True):
"""
Get a RSE ID or raise if it does not exist.
:param rse: the rse name.
:param session: The database session in use.
:param include_deleted: Flag to toggle finding rse's marked as deleted.
:returns: The rse id.
:raises RSENotFound: If referred RSE was not found in the database.
"""
if include_deleted:
cache_key = 'rse-id_{}'.format(rse).replace(' ', '.')
result = REGION.get(cache_key)
if result != NO_VALUE:
return result
try:
query = session.query(models.RSE.id).filter_by(rse=rse)
if not include_deleted:
query = query.filter_by(deleted=False)
result = query.one()[0]
except sqlalchemy.orm.exc.NoResultFound:
raise exception.RSENotFound('RSE \'%s\' cannot be found' % rse)
if include_deleted:
REGION.set(cache_key, result)
return result
示例12: get_rse_name
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import exc [as 别名]
def get_rse_name(rse_id, session=None, include_deleted=True):
"""
Get a RSE name or raise if it does not exist.
:param rse_id: the rse uuid from the database.
:param session: The database session in use.
:param include_deleted: Flag to toggle finding rse's marked as deleted.
:returns: The rse name.
:raises RSENotFound: If referred RSE was not found in the database.
"""
if include_deleted:
cache_key = 'rse-name_{}'.format(rse_id)
result = REGION.get(cache_key)
if result != NO_VALUE:
return result
try:
query = session.query(models.RSE.rse).filter_by(id=rse_id)
if not include_deleted:
query = query.filter_by(deleted=False)
result = query.one()[0]
except sqlalchemy.orm.exc.NoResultFound:
raise exception.RSENotFound('RSE with ID \'%s\' cannot be found' % rse_id)
if include_deleted:
REGION.set(cache_key, result)
return result
示例13: update_rse
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import exc [as 别名]
def update_rse(rse_id, parameters, session=None):
"""
Update RSE properties like availability or name.
:param rse_id: the id of the new rse.
:param parameters: A dictionnary with property (name, read, write, delete as keys).
:param session: The database session in use.
:raises RSENotFound: If RSE is not found.
"""
try:
query = session.query(models.RSE).filter_by(id=rse_id).one()
except sqlalchemy.orm.exc.NoResultFound:
raise exception.RSENotFound('RSE \'%s\' cannot be found' % rse_id)
availability = 0
rse = query.rse
for column in query:
if column[0] == 'availability':
availability = column[1] or availability
param = {}
availability_mapping = {'availability_read': 4, 'availability_write': 2, 'availability_delete': 1}
for key in parameters:
if key == 'name' and parameters['name'] != rse: # Needed due to wrongly setting name in pre1.22.7 clients
param['rse'] = parameters['name']
elif key in ['availability_read', 'availability_write', 'availability_delete']:
if parameters[key] is True:
availability = availability | availability_mapping[key]
else:
availability = availability & ~availability_mapping[key]
elif key in ['latitude', 'longitude', 'time_zone', 'rse_type', 'volatile', 'deterministic', 'region_code', 'country_name', 'city', 'staging_area']:
param[key] = parameters[key]
param['availability'] = availability
query.update(param)
if 'rse' in param:
add_rse_attribute(rse_id=rse_id, key=parameters['name'], value=True, session=session)
query = session.query(models.RSEAttrAssociation).filter_by(rse_id=rse_id).filter(models.RSEAttrAssociation.key == rse)
rse_attr = query.one()
rse_attr.delete(session=session)
示例14: job_context
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import exc [as 别名]
def job_context(self, jobid=None, joburl=None):
assert jobid or joburl, "JobID and JobURL are both None!"
tries = 0
while 1:
tries += 1
with self.db.session_context() as sess:
try:
if jobid:
job = sess.query(self.db.RawWebPages) \
.filter(self.db.RawWebPages.id == jobid) \
.scalar()
elif joburl:
job = sess.query(self.db.RawWebPages) \
.filter(self.db.RawWebPages.url == joburl) \
.scalar()
else:
raise RuntimeError("You need to pass either a JobID or a JobURL")
yield sess, job
sess.commit()
return
except sqlalchemy.exc.OperationalError:
self.log.warning("sqlalchemy.exc.OperationalError!")
sess.rollback()
if tries > 5:
traceback.print_exc()
except sqlalchemy.exc.InvalidRequestError:
self.log.warning("sqlalchemy.exc.InvalidRequestError!")
sess.rollback()
if tries > 5:
traceback.print_exc()
except Exception as e:
print("Exception!")
print(traceback.print_exc())
raise e
示例15: test2
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import exc [as 别名]
def test2():
fetcher = RawArchiver.RawJobDispatcher.RawJobFetcher()
with common.database.session_context() as sess:
try:
job = common.database.RawWebPages(
state = 'new',
url = 'http://somethingpositive.net',
starturl = 'http://somethingpositive.net',
netloc = 'somethingpositive.net',
priority = common.database.DB_LOW_PRIORITY,
distance = 0,
)
sess.add(job)
sess.commit()
except sqlalchemy.exc.IntegrityError:
sess.rollback()
r = sess.query(common.database.RawWebPages) \
.filter(common.database.RawWebPages.url == 'http://somethingpositive.net') \
.update({'state' : 'new', 'ignoreuntiltime' : datetime.datetime.min })
sess.commit()
print("Did update?")
print(r)
archiver = RawSiteArchiver(total_worker_count=1, worker_num=0, new_job_queue=fetcher.get_queue(), cookie_lock=None, response_queue=None, db_interface=sess, db=common.database)
archiver.taskProcess()