Skip to content

Commit eac61c5

Browse files
committed
db: Remove use of 'bind' arguments
Resolve the following RemovedIn20Warning warnings: The MetaData.bind argument is deprecated and will be removed in SQLAlchemy 2.0. The ``bind`` argument for schema methods that invoke SQL against an engine or connection will be required in SQLAlchemy 2.0. We must retain a couple of workarounds to keep sqlalchemy-migrate happy. This shouldn't be an issue since this library is not compatible with SQLAlchemy 2.x and will have to be removed (along with the legacy migration files themselves) when we eventually support 2.x. Change-Id: I946b4c7926ba2583b84ab95a1fe7aedc6923d9f8 Signed-off-by: Stephen Finucane <[email protected]>
1 parent be4e01c commit eac61c5

File tree

7 files changed

+44
-39
lines changed

7 files changed

+44
-39
lines changed

nova/db/api/legacy_migrations/versions/067_train.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ def InetSmall():
2727

2828
def upgrade(migrate_engine):
2929
meta = sa.MetaData()
30+
# NOTE(stephenfin): This is not compatible with SQLAlchemy 2.0 but neither
31+
# is sqlalchemy-migrate which requires this. We'll remove these migrations
32+
# when dropping SQLAlchemy < 2.x support
3033
meta.bind = migrate_engine
3134

3235
cell_mappings = sa.Table('cell_mappings', meta,

nova/db/main/api.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4233,8 +4233,9 @@ def _get_fk_stmts(metadata, conn, table, column, records):
42334233
return inserts, deletes
42344234

42354235

4236-
def _archive_deleted_rows_for_table(metadata, tablename, max_rows, before,
4237-
task_log):
4236+
def _archive_deleted_rows_for_table(
4237+
metadata, engine, tablename, max_rows, before, task_log,
4238+
):
42384239
"""Move up to max_rows rows from one tables to the corresponding
42394240
shadow table.
42404241
@@ -4249,7 +4250,7 @@ def _archive_deleted_rows_for_table(metadata, tablename, max_rows, before,
42494250
- number of extra rows archived (due to FK constraints)
42504251
dict of {tablename: rows_archived}
42514252
"""
4252-
conn = metadata.bind.connect()
4253+
conn = engine.connect()
42534254
# NOTE(tdurakov): table metadata should be received
42544255
# from models, not db tables. Default value specified by SoftDeleteMixin
42554256
# is known only by models, not DB layer.
@@ -4382,8 +4383,9 @@ def archive_deleted_rows(context=None, max_rows=None, before=None,
43824383
table_to_rows_archived = collections.defaultdict(int)
43834384
deleted_instance_uuids = []
43844385
total_rows_archived = 0
4385-
meta = sa.MetaData(get_engine(use_slave=True, context=context))
4386-
meta.reflect()
4386+
meta = sa.MetaData()
4387+
engine = get_engine(use_slave=True, context=context)
4388+
meta.reflect(bind=engine)
43874389
# Get the sorted list of tables in order of foreign key dependency.
43884390
# Process the parent tables and find their dependent records in order to
43894391
# archive the related records in a single database transactions. The goal
@@ -4409,7 +4411,7 @@ def archive_deleted_rows(context=None, max_rows=None, before=None,
44094411

44104412
rows_archived, _deleted_instance_uuids, extras = (
44114413
_archive_deleted_rows_for_table(
4412-
meta, tablename,
4414+
meta, engine, tablename,
44134415
max_rows=max_rows - total_rows_archived,
44144416
before=before,
44154417
task_log=task_log))
@@ -4437,8 +4439,7 @@ def purge_shadow_tables(context, before_date, status_fn=None):
44374439
engine = get_engine(context=context)
44384440
conn = engine.connect()
44394441
metadata = sa.MetaData()
4440-
metadata.bind = engine
4441-
metadata.reflect()
4442+
metadata.reflect(bind=engine)
44424443
total_deleted = 0
44434444

44444445
if status_fn is None:

nova/db/main/legacy_migrations/versions/402_train.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,13 @@ def process(element, compiler, **kw):
4545

4646

4747
def _create_shadow_tables(migrate_engine):
48-
meta = sa.MetaData(migrate_engine)
48+
meta = sa.MetaData()
4949
meta.reflect(migrate_engine)
5050
table_names = list(meta.tables.keys())
5151

52+
# NOTE(stephenfin): This is not compatible with SQLAlchemy 2.0 but neither
53+
# is sqlalchemy-migrate which requires this. We'll remove these migrations
54+
# when dropping SQLAlchemy < 2.x support
5255
meta.bind = migrate_engine
5356

5457
for table_name in table_names:
@@ -184,6 +187,9 @@ def _create_shadow_tables(migrate_engine):
184187

185188
def upgrade(migrate_engine):
186189
meta = sa.MetaData()
190+
# NOTE(stephenfin): This is not compatible with SQLAlchemy 2.0 but neither
191+
# is sqlalchemy-migrate which requires this. We'll remove these migrations
192+
# when dropping SQLAlchemy < 2.x support
187193
meta.bind = migrate_engine
188194

189195
agent_builds = sa.Table('agent_builds', meta,

nova/db/main/migrations/versions/8f2f1571d55b_initial_version.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ def process(element, compiler, **kw):
5353

5454

5555
def _create_shadow_tables(connection):
56-
meta = sa.MetaData(connection)
57-
meta.reflect(connection)
56+
meta = sa.MetaData()
57+
meta.reflect(bind=connection)
5858
table_names = list(meta.tables.keys())
5959

6060
for table_name in table_names:

nova/tests/fixtures/nova.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -847,18 +847,6 @@ def setUp(self):
847847
message=r'The current statement is being autocommitted .*',
848848
category=sqla_exc.SADeprecationWarning)
849849

850-
warnings.filterwarnings(
851-
'ignore',
852-
module='nova',
853-
message=r'The MetaData.bind argument is deprecated .*',
854-
category=sqla_exc.SADeprecationWarning)
855-
856-
warnings.filterwarnings(
857-
'ignore',
858-
module='nova',
859-
message=r'The ``bind`` argument for schema methods .*',
860-
category=sqla_exc.SADeprecationWarning)
861-
862850
warnings.filterwarnings(
863851
'ignore',
864852
module='nova',

nova/tests/functional/db/test_archive.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ def test_archive_deleted_rows_incomplete(self):
177177
def _get_table_counts(self):
178178
engine = db.get_engine()
179179
conn = engine.connect()
180-
meta = sa.MetaData(engine)
181-
meta.reflect()
180+
meta = sa.MetaData()
181+
meta.reflect(bind=engine)
182182
shadow_tables = db._purgeable_tables(meta)
183183
results = {}
184184
for table in shadow_tables:

nova/tests/unit/db/main/test_api.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5652,7 +5652,7 @@ class ArchiveTestCase(test.TestCase, ModelsObjectComparatorMixin):
56525652
def setUp(self):
56535653
super(ArchiveTestCase, self).setUp()
56545654
self.engine = db.get_engine()
5655-
self.metadata = sa.MetaData(self.engine)
5655+
self.metadata = sa.MetaData()
56565656
self.conn = self.engine.connect()
56575657
self.instance_id_mappings = models.InstanceIdMapping.__table__
56585658
self.shadow_instance_id_mappings = sqlalchemyutils.get_table(
@@ -5684,8 +5684,8 @@ def _assert_shadow_tables_empty_except(self, *exceptions):
56845684
except for specificially named exceptions, are empty. This
56855685
makes sure that archiving isn't moving unexpected content.
56865686
"""
5687-
metadata = sa.MetaData(bind=self.engine)
5688-
metadata.reflect()
5687+
metadata = sa.MetaData()
5688+
metadata.reflect(bind=self.engine)
56895689
for table in metadata.tables:
56905690
if table.startswith("shadow_") and table not in exceptions:
56915691
rows = self.conn.exec_driver_sql(
@@ -5698,8 +5698,8 @@ def test_shadow_tables(self):
56985698
56995699
Shadow tables should have an identical schema to the main table.
57005700
"""
5701-
metadata = sa.MetaData(bind=self.engine)
5702-
metadata.reflect()
5701+
metadata = sa.MetaData()
5702+
metadata.reflect(bind=self.engine)
57035703
for table_name in metadata.tables:
57045704
# some tables don't have shadow tables so skip these
57055705
if table_name in [
@@ -5942,7 +5942,9 @@ def _test_archive_deleted_rows_for_one_uuid_table(self, tablename):
59425942
self.assertEqual(len(rows), 0)
59435943
# Archive 2 rows
59445944
db._archive_deleted_rows_for_table(
5945-
self.metadata, tablename, max_rows=2, before=None, task_log=False)
5945+
self.metadata, self.engine, tablename, max_rows=2, before=None,
5946+
task_log=False,
5947+
)
59465948
# Verify we have 4 left in main
59475949
rows = self.conn.execute(qmt).fetchall()
59485950
self.assertEqual(len(rows), 4)
@@ -5951,7 +5953,9 @@ def _test_archive_deleted_rows_for_one_uuid_table(self, tablename):
59515953
self.assertEqual(len(rows), 2)
59525954
# Archive 2 more rows
59535955
db._archive_deleted_rows_for_table(
5954-
self.metadata, tablename, max_rows=2, before=None, task_log=False)
5956+
self.metadata, self.engine, tablename, max_rows=2, before=None,
5957+
task_log=False,
5958+
)
59555959
# Verify we have 2 left in main
59565960
rows = self.conn.execute(qmt).fetchall()
59575961
self.assertEqual(len(rows), 2)
@@ -5960,7 +5964,9 @@ def _test_archive_deleted_rows_for_one_uuid_table(self, tablename):
59605964
self.assertEqual(len(rows), 4)
59615965
# Try to archive more, but there are no deleted rows left.
59625966
db._archive_deleted_rows_for_table(
5963-
self.metadata, tablename, max_rows=2, before=None, task_log=False)
5967+
self.metadata, self.engine, tablename, max_rows=2, before=None,
5968+
task_log=False,
5969+
)
59645970
# Verify we still have 2 left in main
59655971
rows = self.conn.execute(qmt).fetchall()
59665972
self.assertEqual(len(rows), 2)
@@ -6019,8 +6025,8 @@ def test_archive_deleted_rows_for_migrations(self):
60196025
# Archiving instances should result in migrations related to the
60206026
# instances also being archived.
60216027
num = db._archive_deleted_rows_for_table(
6022-
self.metadata, "instances", max_rows=None, before=None,
6023-
task_log=False)
6028+
self.metadata, self.engine, "instances", max_rows=None,
6029+
before=None, task_log=False)
60246030
self.assertEqual(1, num[0])
60256031
self._assert_shadow_tables_empty_except(
60266032
'shadow_instances',
@@ -6386,7 +6392,8 @@ def call_api(*args, **kwargs):
63866392

63876393

63886394
class TestSqlalchemyTypesRepr(
6389-
test_fixtures.OpportunisticDBTestMixin, test.NoDBTestCase):
6395+
test_fixtures.OpportunisticDBTestMixin, test.NoDBTestCase,
6396+
):
63906397

63916398
def setUp(self):
63926399
# NOTE(sdague): the oslo_db base test case completely
@@ -6397,15 +6404,15 @@ def setUp(self):
63976404

63986405
super(TestSqlalchemyTypesRepr, self).setUp()
63996406
self.engine = enginefacade.writer.get_engine()
6400-
meta = sa.MetaData(bind=self.engine)
6407+
meta = sa.MetaData()
64016408
self.table = sa.Table(
64026409
'cidr_tbl',
64036410
meta,
64046411
sa.Column('id', sa.Integer, primary_key=True),
64056412
sa.Column('addr', col_types.CIDR())
64066413
)
6407-
self.table.create()
6408-
self.addCleanup(meta.drop_all)
6414+
meta.create_all(self.engine)
6415+
self.addCleanup(meta.drop_all, self.engine)
64096416

64106417
def test_cidr_repr(self):
64116418
addrs = [('192.168.3.0/24', '192.168.3.0/24'),

0 commit comments

Comments
 (0)