db: Replace use of deprecated API

We were missing a number of users of SQLALchemy APIs that were either
changed or removed in 2.0. These were yielding the following errors in
2.0.

  TypeError: MetaData.__init__() got an unexpected keyword argument
  'bind'

  TypeError: TableClause.insert() got an unexpected keyword argument
  'values'

  TypeError: TableClause.update() got an unexpected keyword argument
  'values'

  AttributeError: 'Engine' object has no attribute 'execute'

Resolve these issues. We will fix the test class issue in a follow-up.

Change-Id: I8724b0738f097739f55cd8566bb30b7ddbe154d9
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
This commit is contained in:
Stephen Finucane 2023-04-17 11:04:10 +01:00
parent d7e9a330f0
commit f71603d695
3 changed files with 58 additions and 30 deletions

View File

@ -31,10 +31,23 @@ depends_on = None
def upgrade():
connection = context.get_bind()
metadata = MetaData(bind=connection)
resource_providers = Table('resource_providers', metadata, autoload=True)
query = select(sqlfunc.count()).select_from(resource_providers).where(
resource_providers.c.root_provider_id == sa.null())
meta = MetaData()
meta.reflect(bind=connection)
resource_providers = Table(
'resource_providers',
meta,
autoload_with=connection,
)
query = select(
sqlfunc.count(),
).select_from(
resource_providers,
).where(
resource_providers.c.root_provider_id == sa.null()
)
if connection.scalar(query):
raise Exception('There is at least one resource provider table '
'record which is missing its root provider id. '

View File

@ -31,12 +31,16 @@ depends_on = None
def upgrade():
connection = context.get_bind()
metadata = sa.MetaData(bind=connection)
consumers = sa.Table('consumers', metadata, autoload=True)
allocations = sa.Table('allocations', metadata, autoload=True)
meta = sa.MetaData()
meta.reflect(bind=connection)
consumers = sa.Table('consumers', meta, autoload_with=connection)
allocations = sa.Table('allocations', meta, autoload_with=connection)
alloc_to_consumer = sa.outerjoin(
allocations, consumers,
allocations.c.consumer_id == consumers.c.uuid)
allocations.c.consumer_id == consumers.c.uuid,
)
sel = sa.select(sqlfunc.count())
sel = sel.select_from(alloc_to_consumer)
sel = sel.where(consumers.c.id.is_(None))

View File

@ -177,10 +177,12 @@ class MigrationCheckersMixin(object):
self.migration_api.upgrade('b4ed3a175331')
# Now insert a resource provider with no root.
rps = db_utils.get_table(self.engine, 'resource_providers')
ins_stmt = rps.insert(values={
'name': 'fake-rp-name', 'uuid': uuids.rp_uuid
})
rp_id = self.engine.execute(ins_stmt).inserted_primary_key[0]
ins_stmt = rps.insert().values(
name='fake-rp-name',
uuid=uuids.rp_uuid,
)
with self.engine.connect() as conn, conn.begin():
rp_id = conn.execute(ins_stmt).inserted_primary_key[0]
# Now run the blocker migration and it should raise an error.
ex = self.assertRaises( # noqa H202
Exception, self.migration_api.upgrade, '611cd6dffd7b')
@ -189,10 +191,11 @@ class MigrationCheckersMixin(object):
'record which is missing its root provider id.',
str(ex))
# Now update the resource provider with a root_provider_id.
update_stmt = rps.update(
values={'root_provider_id': rp_id}).where(
rps.c.id == rp_id)
self.engine.execute(update_stmt)
update_stmt = rps.update().values(
root_provider_id=rp_id,
).where(rps.c.id == rp_id)
with self.engine.connect() as conn, conn.begin():
conn.execute(update_stmt)
# Re-run the upgrade and it should be OK.
self.migration_api.upgrade('611cd6dffd7b')
@ -205,18 +208,23 @@ class MigrationCheckersMixin(object):
self.migration_api.upgrade('b4ed3a175331')
# Now insert a resource provider to build off
rps = db_utils.get_table(self.engine, 'resource_providers')
ins_stmt = rps.insert(values={
'name': 'fake-rp-name', 'uuid': uuids.rp_uuid,
'root_provider_id': 1
})
rp_id = self.engine.execute(ins_stmt).inserted_primary_key[0]
ins_stmt = rps.insert().values(
name='fake-rp-name',
uuid=uuids.rp_uuid,
root_provider_id=1,
)
with self.engine.connect() as conn, conn.begin():
rp_id = conn.execute(ins_stmt).inserted_primary_key[0]
# Now insert an allocation
allocations = db_utils.get_table(self.engine, 'allocations')
ins_stmt = allocations.insert(values={
'resource_provider_id': rp_id, 'resource_class_id': 1,
'used': 5, 'consumer_id': uuids.consumer1
})
self.engine.execute(ins_stmt).inserted_primary_key[0]
ins_stmt = allocations.insert().values(
resource_provider_id=rp_id,
resource_class_id=1,
used=5,
consumer_id=uuids.consumer1,
)
with self.engine.connect() as conn, conn.begin():
conn.execute(ins_stmt).inserted_primary_key[0]
# Now run the blocker migration and it should raise an error.
ex = self.assertRaises( # noqa H202
Exception, self.migration_api.upgrade, 'b5c396305c25')
@ -226,10 +234,13 @@ class MigrationCheckersMixin(object):
str(ex))
# Add a (faked) consumer record and try again
consumers = db_utils.get_table(self.engine, 'consumers')
ins_stmt = consumers.insert(values={
'uuid': uuids.consumer1, 'project_id': 1, 'user_id': 1
})
self.engine.execute(ins_stmt).inserted_primary_key[0]
ins_stmt = consumers.insert().values(
uuid=uuids.consumer1,
project_id=1,
user_id=1,
)
with self.engine.connect() as conn, conn.begin():
conn.execute(ins_stmt).inserted_primary_key[0]
self.migration_api.upgrade('b5c396305c25')
def test_consumer_types_422ece571366(self):