db: Use explicit transactions

Resolve the following sqlalchemy.exc.RemovedIn20Warning warning:

  The current statement is being autocommitted using implicit
  autocommit, which will be removed in SQLAlchemy 2.0. Use the .begin()
  method of Engine or Connection in order to use an explicit transaction
  for DML and DDL statements.

For more information, refer to http://sqlalche.me/e/b8d9.

Change-Id: I6fc825906a63da8b0dbe51b7b859c2eb7fa1af82
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
This commit is contained in:
Stephen Finucane 2021-07-15 13:51:21 +01:00
parent 3aca87e4fc
commit e5b89581c5
3 changed files with 11 additions and 7 deletions

View File

@ -79,7 +79,8 @@ class TestResourceClassCache(base.TestCase):
id=1001,
name='IRON_NFV'
)
conn.execute(ins_stmt)
with conn.begin():
conn.execute(ins_stmt)
self.assertEqual('IRON_NFV', cache.string_from_id(1001))
self.assertEqual(1001, cache.id_from_string('IRON_NFV'))
@ -107,7 +108,8 @@ class TestResourceClassCache(base.TestCase):
upd_stmt = attribute_cache._RC_TBL.update().where(
attribute_cache._RC_TBL.c.id == 1001).values(
name='IRON_NFV', updated_at=timeutils.utcnow())
conn.execute(upd_stmt)
with conn.begin():
conn.execute(upd_stmt)
# reset cache
cache = attribute_cache.ResourceClassCache(self.context)

View File

@ -181,6 +181,7 @@ class PlacementDbBaseTestCase(base.TestCase):
def create_aggregate(self, agg_uuid):
conn = self.placement_db.get_engine().connect()
ins_stmt = rp_obj._AGG_TBL.insert().values(uuid=agg_uuid)
res = conn.execute(ins_stmt)
with conn.begin():
res = conn.execute(ins_stmt)
agg_id = res.inserted_primary_key[0]
return agg_id

View File

@ -43,10 +43,11 @@ class ResourceClassListTestCase(tb.PlacementDbBaseTestCase):
('CUSTOM_IRON_ENTERPRISE', 10002),
]
with self.placement_db.get_engine().connect() as conn:
for custom in customs:
c_name, c_id = custom
ins = rc_obj._RC_TBL.insert().values(id=c_id, name=c_name)
conn.execute(ins)
with conn.begin():
for custom in customs:
c_name, c_id = custom
ins = rc_obj._RC_TBL.insert().values(id=c_id, name=c_name)
conn.execute(ins)
rcs = rc_obj.get_all(self.ctx)
expected_count = (len(orc.STANDARDS) + len(customs))