Remove use of MetaData.bind argument

Resolves the following RemovedIn20Warning warning:

  The MetaData.bind argument is deprecated and will be removed in
  SQLAlchemy 2.0.

Change-Id: I468048c01455d9dfe12004736c181806dc218e37
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
This commit is contained in:
Stephen Finucane 2021-07-16 15:43:29 +01:00 committed by Mike Bayer
parent 8d7607266c
commit 22b44ee18b
4 changed files with 94 additions and 69 deletions

View File

@ -441,8 +441,7 @@ def get_table(engine, name):
with ForeignKey creation.
"""
metadata = MetaData()
metadata.bind = engine
return Table(name, metadata, autoload=True)
return Table(name, metadata, autoload_with=engine)
def _get_not_supported_column(col_name_col_instance, column_name):
@ -476,9 +475,8 @@ def drop_old_duplicate_entries_from_table(engine, table_name,
:param uc_column_names: Unique constraint columns
"""
meta = MetaData()
meta.bind = engine
table = Table(table_name, meta, autoload=True)
table = Table(table_name, meta, autoload_with=engine)
columns_for_group_by = [table.c[name] for name in uc_column_names]
columns_for_select = [func.max(table.c.id)]
@ -565,15 +563,24 @@ def change_deleted_column_type_to_boolean(engine, table_name,
table = get_table(engine, table_name)
old_deleted = Column('old_deleted', Boolean, default=False)
old_deleted.create(table, populate_default=False)
table.metadata.bind = engine
try:
old_deleted.create(table, populate_default=False)
finally:
table.metadata.bind = None
table.update().\
where(table.c.deleted == table.c.id).\
values(old_deleted=True).\
execute()
engine.execute(
table.update().
where(table.c.deleted == table.c.id).
values(old_deleted=True)
)
table.c.deleted.drop()
table.c.old_deleted.alter(name="deleted")
table.metadata.bind = engine
try:
table.c.deleted.drop()
table.c.old_deleted.alter(name="deleted")
finally:
table.metadata.bind = None
_restore_indexes_on_deleted_columns(engine, table_name, indexes)
@ -603,7 +610,7 @@ def _change_deleted_column_type_to_boolean_sqlite(engine, table_name,
meta = table.metadata
new_table = Table(table_name + "__tmp__", meta,
*(columns + constraints))
new_table.create()
new_table.create(engine)
indexes = []
for index in get_indexes(engine, table_name):
@ -618,15 +625,21 @@ def _change_deleted_column_type_to_boolean_sqlite(engine, table_name,
else:
c_select.append(table.c.deleted == table.c.id)
table.drop()
table.drop(engine)
for index in indexes:
index.create(engine)
new_table.rename(table_name)
new_table.update().\
where(new_table.c.deleted == new_table.c.id).\
values(deleted=True).\
execute()
table.metadata.bind = engine
try:
new_table.rename(table_name)
finally:
table.metadata.bind = None
engine.execute(
new_table.update().
where(new_table.c.deleted == new_table.c.id).
values(deleted=True)
)
@debtcollector.removals.remove(
@ -645,15 +658,24 @@ def change_deleted_column_type_to_id_type(engine, table_name,
new_deleted = Column('new_deleted', table.c.id.type,
default=_get_default_deleted_value(table))
new_deleted.create(table, populate_default=True)
table.metadata.bind = engine
try:
new_deleted.create(table, populate_default=True)
finally:
table.metadata.bind = None
deleted = True # workaround for pyflakes
table.update().\
where(table.c.deleted == deleted).\
values(new_deleted=table.c.id).\
execute()
table.c.deleted.drop()
table.c.new_deleted.alter(name="deleted")
engine.execute(
table.update().
where(table.c.deleted == deleted).
values(new_deleted=table.c.id)
)
table.metadata.bind = engine
try:
table.c.deleted.drop()
table.c.new_deleted.alter(name="deleted")
finally:
table.metadata.bind = None
_restore_indexes_on_deleted_columns(engine, table_name, indexes)
@ -682,8 +704,8 @@ def _change_deleted_column_type_to_id_type_sqlite(engine, table_name,
# 2) Copy all data from old to new table.
# 3) Drop old table.
# 4) Rename new table to old table name.
meta = MetaData(bind=engine)
table = Table(table_name, meta, autoload=True)
meta = MetaData()
table = Table(table_name, meta, autoload_with=engine)
default_deleted_value = _get_default_deleted_value(table)
columns = []
@ -711,7 +733,7 @@ def _change_deleted_column_type_to_id_type_sqlite(engine, table_name,
new_table = Table(table_name + "__tmp__", meta,
*(columns + constraints))
new_table.create()
new_table.create(engine)
indexes = []
for index in get_indexes(engine, table_name):
@ -719,23 +741,30 @@ def _change_deleted_column_type_to_id_type_sqlite(engine, table_name,
indexes.append(Index(index["name"], *column_names,
unique=index["unique"]))
table.drop()
table.drop(engine)
for index in indexes:
index.create(engine)
new_table.rename(table_name)
new_table.metadata.bind = engine
try:
new_table.rename(table_name)
finally:
new_table.metadata.bind = None
deleted = True # workaround for pyflakes
new_table.update().\
where(new_table.c.deleted == deleted).\
values(deleted=new_table.c.id).\
execute()
engine.execute(
new_table.update().
where(new_table.c.deleted == deleted).
values(deleted=new_table.c.id)
)
# NOTE(boris-42): Fix value of deleted column: False -> "" or 0.
deleted = False # workaround for pyflakes
new_table.update().\
where(new_table.c.deleted == deleted).\
values(deleted=default_deleted_value).\
execute()
engine.execute(
new_table.update().
where(new_table.c.deleted == deleted).
values(deleted=default_deleted_value)
)
def get_db_connection_info(conn_pieces):
@ -804,7 +833,7 @@ def add_index(engine, table_name, index_name, idx_columns):
index = Index(
index_name, *[getattr(table.c, col) for col in idx_columns]
)
index.create()
index.create(engine)
else:
raise ValueError("Index '%s' already exists!" % index_name)
@ -819,7 +848,7 @@ def drop_index(engine, table_name, index_name):
table = get_table(engine, table_name)
for index in table.indexes:
if index.name == index_name:
index.drop()
index.drop(engine)
break
else:
raise ValueError("Index '%s' not found!" % index_name)

View File

@ -37,11 +37,6 @@ class WarningsFixture(fixtures.Fixture):
# ...but filter everything out until we get around to fixing them
# FIXME(stephenfin): Remove all of these
warnings.filterwarnings(
'once',
message=r'The MetaData.bind argument is deprecated .*',
category=sqla_exc.SADeprecationWarning)
warnings.filterwarnings(
'once',
message=r'The ``bind`` argument for schema methods .*',

View File

@ -252,7 +252,7 @@ class TestNonExistentConstraint(
def setUp(self):
super(TestNonExistentConstraint, self).setUp()
meta = sqla.MetaData(bind=self.engine)
meta = sqla.MetaData()
self.table_1 = sqla.Table(
"resource_foo", meta,
@ -260,7 +260,7 @@ class TestNonExistentConstraint(
mysql_engine='InnoDB',
mysql_charset='utf8',
)
self.table_1.create()
self.table_1.create(self.engine)
class TestNonExistentConstraintPostgreSQL(
@ -321,7 +321,7 @@ class TestNonExistentTable(
def setUp(self):
super(TestNonExistentTable, self).setUp()
self.meta = sqla.MetaData(bind=self.engine)
self.meta = sqla.MetaData()
self.table_1 = sqla.Table(
"foo", self.meta,
@ -465,7 +465,7 @@ class TestReferenceErrorSQLite(
def setUp(self):
super(TestReferenceErrorSQLite, self).setUp()
meta = sqla.MetaData(bind=self.engine)
meta = sqla.MetaData()
self.table_1 = sqla.Table(
"resource_foo", meta,
@ -474,7 +474,7 @@ class TestReferenceErrorSQLite(
mysql_engine='InnoDB',
mysql_charset='utf8',
)
self.table_1.create()
self.table_1.create(self.engine)
self.table_2 = sqla.Table(
"resource_entity", meta,
@ -484,7 +484,7 @@ class TestReferenceErrorSQLite(
mysql_engine='InnoDB',
mysql_charset='utf8',
)
self.table_2.create()
self.table_2.create(self.engine)
def test_raise(self):
self.engine.execute("PRAGMA foreign_keys = ON;")

View File

@ -666,9 +666,11 @@ class TestMigrationUtils(db_test_base._DbTestCase):
def setUp(self):
super(TestMigrationUtils, self).setUp()
self.meta = MetaData(bind=self.engine)
self.meta = MetaData()
self.conn = self.engine.connect()
self.addCleanup(self.meta.drop_all)
# self.conn would be better here but does not work right now
self.addCleanup(self.meta.drop_all, self.engine)
self.addCleanup(self.conn.close)
def _populate_db_for_drop_duplicate_entries(self, engine, meta,
@ -695,7 +697,7 @@ class TestMigrationUtils(db_test_base._DbTestCase):
Column('deleted_at', DateTime),
Column('updated_at', DateTime))
test_table.create()
test_table.create(engine)
engine.execute(test_table.insert(), values)
return test_table, values
@ -731,7 +733,6 @@ class TestMigrationUtils(db_test_base._DbTestCase):
in_file_engine = session.EngineFacade(
'sqlite:///%s' % tmp_db_file).get_engine()
meta = MetaData()
meta.bind = in_file_engine
test_table, values = self._populate_db_for_drop_duplicate_entries(
in_file_engine, meta, table_name)
utils.drop_old_duplicate_entries_from_table(
@ -793,7 +794,7 @@ class TestMigrationUtils(db_test_base._DbTestCase):
Column('b', String(255)),
Column('deleted', Boolean),
*index_instances)
table.create()
table.create(self.engine)
utils.change_deleted_column_type_to_id_type(self.engine, table_name)
utils.change_deleted_column_type_to_boolean(self.engine, table_name)
@ -811,7 +812,7 @@ class TestMigrationUtils(db_test_base._DbTestCase):
table = Table(table_name, self.meta,
Column('id', Integer, primary_key=True),
Column('deleted', Boolean))
table.create()
table.create(self.engine)
utils.change_deleted_column_type_to_id_type(self.engine, table_name)
table = utils.get_table(self.engine, table_name)
@ -822,7 +823,7 @@ class TestMigrationUtils(db_test_base._DbTestCase):
table = Table(table_name, self.meta,
Column('id', String(255), primary_key=True),
Column('deleted', Boolean))
table.create()
table.create(self.engine)
utils.change_deleted_column_type_to_id_type(self.engine, table_name)
table = utils.get_table(self.engine, table_name)
@ -835,7 +836,7 @@ class TestMigrationUtils(db_test_base._DbTestCase):
Column('id', Integer, primary_key=True),
Column('foo', CustomType),
Column('deleted', Boolean))
table.create()
table.create(self.engine)
fooColumn = Column('foo', CustomType())
utils.change_deleted_column_type_to_id_type(self.engine, table_name,
@ -851,7 +852,7 @@ class TestMigrationUtils(db_test_base._DbTestCase):
table = Table(table_name, self.meta,
Column('id', Integer, primary_key=True),
Column('deleted', Integer))
table.create()
table.create(self.engine)
utils.change_deleted_column_type_to_boolean(self.engine, table_name)
@ -867,14 +868,14 @@ class TestMigrationUtils(db_test_base._DbTestCase):
table_1 = Table(table_name_1, self.meta,
Column('id', Integer, primary_key=True),
Column('deleted', Integer))
table_1.create()
table_1.create(self.engine)
table_2 = Table(table_name_2, self.meta,
Column('id', Integer, primary_key=True),
Column('foreign_id', Integer,
ForeignKey('%s.id' % table_name_1)),
Column('deleted', Integer))
table_2.create()
table_2.create(self.engine)
utils.change_deleted_column_type_to_boolean(self.engine, table_name_2)
@ -889,7 +890,7 @@ class TestMigrationUtils(db_test_base._DbTestCase):
Column('id', Integer, primary_key=True),
Column('foo', CustomType),
Column('deleted', Integer))
table.create()
table.create(self.engine)
fooColumn = Column('foo', CustomType())
utils.change_deleted_column_type_to_boolean(self.engine, table_name,
@ -921,7 +922,7 @@ class TestMigrationUtils(db_test_base._DbTestCase):
table = Table(table_name, self.meta,
Column('id', Integer, primary_key=True),
Column('deleted', Boolean))
table.create()
table.create(self.engine)
utils._change_deleted_column_type_to_id_type_sqlite(self.engine,
table_name)
@ -954,7 +955,7 @@ class TestMigrationUtils(db_test_base._DbTestCase):
name='table_name_2_fk1'),
Column('deleted', Integer))
self.meta.create_all(tables=[table_1, table_2])
self.meta.create_all(self.engine, tables=[table_1, table_2])
fkc = utils.get_foreign_key_constraint_name(self.engine,
'table_name_2',
'foreign_id')
@ -984,7 +985,7 @@ class TestMigrationUtils(db_test_base._DbTestCase):
['key', 'archive_id'], ['b.key', 'b.archive_id'],
name="some_composite_fk")
)
self.meta.create_all(tables=[a, b, c])
self.meta.create_all(self.engine, tables=[a, b, c])
def get_fk_entries():
inspector = sqlalchemy.inspect(self.engine)
@ -1257,15 +1258,15 @@ class TestModelQuery(test_base.BaseTestCase):
class TestUtils(db_test_base._DbTestCase):
def setUp(self):
super(TestUtils, self).setUp()
meta = MetaData(bind=self.engine)
meta = MetaData()
self.test_table = Table(
'test_table',
meta,
Column('a', Integer),
Column('b', Integer)
)
self.test_table.create()
self.addCleanup(meta.drop_all)
self.test_table.create(self.engine)
self.addCleanup(meta.drop_all, self.engine)
def test_get_indexes(self):
Index('index_a', self.test_table.c.a).create(self.engine)