Fix oslo.db >= 15.0.0 compatibility

Minimal refactor of SQLAlchemy api module to be compatible with
oslo.db >= 15.0.0 where autocommit behaviour was dropped.

Closes-Bug: #2056181
Change-Id: I33be53f647faae2aad30a43c10980df950d5d7c2
(cherry picked from commit bc5922c684)
This commit is contained in:
James Page 2024-03-27 09:28:13 +00:00 committed by Dr. Jens Harbott
parent 9d58a6d457
commit 6b433b3547

View File

@ -44,11 +44,7 @@ _FACADE = None
def _create_facade_lazily(): def _create_facade_lazily():
global _FACADE global _FACADE
if _FACADE is None: if _FACADE is None:
# FIXME(amoralej): Remove autocommit=True (and ideally use of _FACADE = db_session.EngineFacade.from_config(CONF)
# LegacyEngineFacade) asap since it's not compatible with SQLAlchemy
# 2.0.
_FACADE = db_session.EngineFacade.from_config(CONF,
autocommit=True)
return _FACADE return _FACADE
@ -252,26 +248,31 @@ class Connection(api.BaseConnection):
return query return query
def _create(self, model, values): def _create(self, model, values):
obj = model() session = get_session()
cleaned_values = {k: v for k, v in values.items() with session.begin():
if k not in self._get_relationships(model)} obj = model()
obj.update(cleaned_values) cleaned_values = {k: v for k, v in values.items()
obj.save() if k not in self._get_relationships(model)}
obj.update(cleaned_values)
obj.save(session=session)
session.commit()
return obj return obj
def _get(self, context, model, fieldname, value, eager): def _get(self, context, model, fieldname, value, eager):
query = model_query(model) session = get_session()
if eager: with session.begin():
query = self._set_eager_options(model, query) query = model_query(model, session=session)
if eager:
query = self._set_eager_options(model, query)
query = query.filter(getattr(model, fieldname) == value) query = query.filter(getattr(model, fieldname) == value)
if not context.show_deleted: if not context.show_deleted:
query = query.filter(model.deleted_at.is_(None)) query = query.filter(model.deleted_at.is_(None))
try: try:
obj = query.one() obj = query.one()
except exc.NoResultFound: except exc.NoResultFound:
raise exception.ResourceNotFound(name=model.__name__, id=value) raise exception.ResourceNotFound(name=model.__name__, id=value)
return obj return obj