Skip to content

Commit b88ea30

Browse files
committed
db: Don't pass strings to 'Connection.execute'
Resolve the following RemovedIn20Warning warning: Passing a string to Connection.execute() is deprecated and will be removed in version 2.0. Use the text() construct, or the Connection.exec_driver_sql() method to invoke a driver-level SQL string. Change-Id: I44d6bf1ebfaf24f00a21389364456bceaae7c4d1 Signed-off-by: Stephen Finucane <[email protected]>
1 parent 5d2399e commit b88ea30

File tree

6 files changed

+84
-59
lines changed

6 files changed

+84
-59
lines changed

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

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,19 +1574,25 @@ def upgrade(migrate_engine):
15741574

15751575
if migrate_engine.name == 'mysql':
15761576
# In Folsom we explicitly converted migrate_version to UTF8.
1577-
migrate_engine.execute(
1578-
'ALTER TABLE migrate_version CONVERT TO CHARACTER SET utf8')
1579-
# Set default DB charset to UTF8.
1580-
migrate_engine.execute(
1581-
'ALTER DATABASE `%s` DEFAULT CHARACTER SET utf8' %
1582-
migrate_engine.url.database)
1583-
1584-
# NOTE(cdent): The resource_providers table is defined as latin1 to be
1585-
# more efficient. Now we need the name column to be UTF8. We modify it
1586-
# here otherwise the declarative handling in sqlalchemy gets confused.
1587-
migrate_engine.execute(
1588-
'ALTER TABLE resource_providers MODIFY name '
1589-
'VARCHAR(200) CHARACTER SET utf8')
1577+
with migrate_engine.connect() as conn:
1578+
conn.exec_driver_sql(
1579+
'ALTER TABLE migrate_version CONVERT TO CHARACTER SET utf8'
1580+
)
1581+
# Set default DB charset to UTF8.
1582+
conn.exec_driver_sql(
1583+
'ALTER DATABASE `%s` DEFAULT CHARACTER SET utf8' % (
1584+
migrate_engine.url.database,
1585+
)
1586+
)
1587+
1588+
# NOTE(cdent): The resource_providers table is defined as latin1 to
1589+
# be more efficient. Now we need the name column to be UTF8. We
1590+
# modify it here otherwise the declarative handling in sqlalchemy
1591+
# gets confused.
1592+
conn.exec_driver_sql(
1593+
'ALTER TABLE resource_providers MODIFY name '
1594+
'VARCHAR(200) CHARACTER SET utf8'
1595+
)
15901596

15911597
_create_shadow_tables(migrate_engine)
15921598

@@ -1596,9 +1602,10 @@ def upgrade(migrate_engine):
15961602
# also
15971603

15981604
if migrate_engine.name == 'mysql':
1599-
# Use binary collation for extra specs table
1600-
migrate_engine.execute(
1601-
'ALTER TABLE instance_type_extra_specs '
1602-
'CONVERT TO CHARACTER SET utf8 '
1603-
'COLLATE utf8_bin'
1604-
)
1605+
with migrate_engine.connect() as conn:
1606+
# Use binary collation for extra specs table
1607+
conn.exec_driver_sql(
1608+
'ALTER TABLE instance_type_extra_specs '
1609+
'CONVERT TO CHARACTER SET utf8 '
1610+
'COLLATE utf8_bin'
1611+
)

nova/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ def enforce_fk_constraints(self, engine=None):
388388
engine = db_api.get_engine()
389389
dialect = engine.url.get_dialect()
390390
if dialect == sqlite.dialect:
391-
engine.connect().execute("PRAGMA foreign_keys = ON")
391+
engine.connect().exec_driver_sql("PRAGMA foreign_keys = ON")
392392

393393
def start_service(self, name, host=None, cell_name=None, **kwargs):
394394
# Disallow starting multiple scheduler services

nova/tests/fixtures/nova.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -889,12 +889,6 @@ def setUp(self):
889889
message=r'The Connection.connect\(\) method is considered .*',
890890
category=sqla_exc.SADeprecationWarning)
891891

892-
warnings.filterwarnings(
893-
'ignore',
894-
module='nova',
895-
message=r'Passing a string to Connection.execute\(\) is .*',
896-
category=sqla_exc.SADeprecationWarning)
897-
898892
warnings.filterwarnings(
899893
'ignore',
900894
module='nova',

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5688,7 +5688,9 @@ def _assert_shadow_tables_empty_except(self, *exceptions):
56885688
metadata.reflect()
56895689
for table in metadata.tables:
56905690
if table.startswith("shadow_") and table not in exceptions:
5691-
rows = self.conn.execute("select * from %s" % table).fetchall()
5691+
rows = self.conn.exec_driver_sql(
5692+
"SELECT * FROM %s" % table
5693+
).fetchall()
56925694
self.assertEqual(rows, [], "Table %s not empty" % table)
56935695

56945696
def test_shadow_tables(self):

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
from oslo_log.fixture import logging_error as log_fixture
3838
from oslo_log import log as logging
3939
from oslotest import base
40+
import sqlalchemy as sa
41+
import sqlalchemy.exc
4042

4143
from nova.db.main import models
4244
from nova.db import migration
@@ -137,21 +139,29 @@ class TestModelsSyncMySQL(
137139
def test_innodb_tables(self):
138140
self.db_sync(self.get_engine())
139141

140-
total = self.engine.execute(
141-
"SELECT count(*) "
142-
"FROM information_schema.TABLES "
143-
"WHERE TABLE_SCHEMA = '%(database)s'" %
144-
{'database': self.engine.url.database})
142+
with self.engine.connect() as conn:
143+
total = conn.execute(
144+
sa.text(
145+
"SELECT count(*) "
146+
"FROM information_schema.TABLES "
147+
"WHERE TABLE_SCHEMA = :database"
148+
),
149+
{'database': self.engine.url.database},
150+
)
145151
self.assertGreater(total.scalar(), 0, "No tables found. Wrong schema?")
146152

147-
noninnodb = self.engine.execute(
148-
"SELECT count(*) "
149-
"FROM information_schema.TABLES "
150-
"WHERE TABLE_SCHEMA='%(database)s' "
151-
"AND ENGINE != 'InnoDB' "
152-
"AND TABLE_NAME != 'migrate_version'" %
153-
{'database': self.engine.url.database})
154-
count = noninnodb.scalar()
153+
with self.engine.connect() as conn:
154+
noninnodb = conn.execute(
155+
sa.text(
156+
"SELECT count(*) "
157+
"FROM information_schema.TABLES "
158+
"WHERE TABLE_SCHEMA = :database "
159+
"AND ENGINE != 'InnoDB' "
160+
"AND TABLE_NAME != 'migrate_version'"
161+
),
162+
{'database': self.engine.url.database},
163+
)
164+
count = noninnodb.scalar()
155165
self.assertEqual(count, 0, "%d non InnoDB tables created" % count)
156166

157167

nova/tests/unit/test_fixtures.py

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from oslo_utils import timeutils
2929
from oslo_utils import uuidutils
3030
from oslotest import output
31-
import sqlalchemy
31+
import sqlalchemy as sa
3232
import testtools
3333

3434
from nova.compute import rpcapi as compute_rpcapi
@@ -125,17 +125,19 @@ def test_fixture_reset(self):
125125
self.useFixture(db_fixture)
126126
engine = main_db_api.get_engine()
127127
conn = engine.connect()
128-
result = conn.execute("select * from instance_types")
128+
result = conn.execute(sa.text("SELECT * FROM instance_types"))
129129
rows = result.fetchall()
130130
self.assertEqual(0, len(rows), "Rows %s" % rows)
131131

132132
# insert a 6th instance type, column 5 below is an int id
133133
# which has a constraint on it, so if new standard instance
134134
# types are added you have to bump it.
135-
conn.execute("insert into instance_types VALUES "
136-
"(NULL, NULL, NULL, 't1.test', 6, 4096, 2, 0, NULL, '87'"
137-
", 1.0, 40, 0, 0, 1, 0)")
138-
result = conn.execute("select * from instance_types")
135+
conn.execute(sa.text(
136+
"INSERT INTO instance_types VALUES "
137+
"(NULL, NULL, NULL, 't1.test', 6, 4096, 2, 0, NULL, '87'"
138+
", 1.0, 40, 0, 0, 1, 0)"
139+
))
140+
result = conn.execute(sa.text("SELECT * FROM instance_types"))
139141
rows = result.fetchall()
140142
self.assertEqual(1, len(rows), "Rows %s" % rows)
141143

@@ -145,7 +147,7 @@ def test_fixture_reset(self):
145147
db_fixture.reset()
146148
engine = main_db_api.get_engine()
147149
conn = engine.connect()
148-
result = conn.execute("select * from instance_types")
150+
result = conn.execute(sa.text("SELECT * FROM instance_types"))
149151
rows = result.fetchall()
150152
self.assertEqual(0, len(rows), "Rows %s" % rows)
151153

@@ -156,14 +158,19 @@ def test_api_fixture_reset(self):
156158
self.useFixture(db_fixture)
157159
engine = api_db_api.get_engine()
158160
conn = engine.connect()
159-
result = conn.execute("select * from cell_mappings")
161+
result = conn.execute(sa.text("SELECT * FROM cell_mappings"))
160162
rows = result.fetchall()
161163
self.assertEqual(0, len(rows), "Rows %s" % rows)
162164

163165
uuid = uuidutils.generate_uuid()
164-
conn.execute("insert into cell_mappings (uuid, name) VALUES "
165-
"('%s', 'fake-cell')" % (uuid,))
166-
result = conn.execute("select * from cell_mappings")
166+
conn.execute(
167+
sa.text(
168+
"INSERT INTO cell_mappings (uuid, name) VALUES (:uuid, :name)"
169+
),
170+
uuid=uuid,
171+
name='fake-cell',
172+
)
173+
result = conn.execute(sa.text("SELECT * FROM cell_mappings"))
167174
rows = result.fetchall()
168175
self.assertEqual(1, len(rows), "Rows %s" % rows)
169176

@@ -173,7 +180,7 @@ def test_api_fixture_reset(self):
173180
db_fixture.reset()
174181
engine = api_db_api.get_engine()
175182
conn = engine.connect()
176-
result = conn.execute("select * from cell_mappings")
183+
result = conn.execute(sa.text("SELECT * FROM cell_mappings"))
177184
rows = result.fetchall()
178185
self.assertEqual(0, len(rows), "Rows %s" % rows)
179186

@@ -202,9 +209,14 @@ def test_api_fixture_cleanup(self):
202209
engine = api_db_api.get_engine()
203210
conn = engine.connect()
204211
uuid = uuidutils.generate_uuid()
205-
conn.execute("insert into cell_mappings (uuid, name) VALUES "
206-
"('%s', 'fake-cell')" % (uuid,))
207-
result = conn.execute("select * from cell_mappings")
212+
conn.execute(
213+
sa.text(
214+
"INSERT INTO cell_mappings (uuid, name) VALUES (:uuid, :name)"
215+
),
216+
uuid=uuid,
217+
name='fake-cell',
218+
)
219+
result = conn.execute(sa.text("SELECT * FROM cell_mappings"))
208220
rows = result.fetchall()
209221
self.assertEqual(1, len(rows), "Rows %s" % rows)
210222

@@ -227,13 +239,13 @@ def test_flavors(self, mock_send_notification):
227239

228240
engine = api_db_api.get_engine()
229241
conn = engine.connect()
230-
result = conn.execute("select * from flavors")
242+
result = conn.execute(sa.text("SELECT * FROM flavors"))
231243
rows = result.fetchall()
232244
self.assertEqual(0, len(rows), "Rows %s" % rows)
233245

234246
self.useFixture(fixtures.DefaultFlavorsFixture())
235247

236-
result = conn.execute("select * from flavors")
248+
result = conn.execute(sa.text("SELECT * FROM flavors"))
237249
rows = result.fetchall()
238250
self.assertEqual(6, len(rows), "Rows %s" % rows)
239251

@@ -323,15 +335,15 @@ def test_submit_passes_through(self):
323335

324336
class TestBannedDBSchemaOperations(testtools.TestCase):
325337
def test_column(self):
326-
column = sqlalchemy.Column()
338+
column = sa.Column()
327339
with fixtures.BannedDBSchemaOperations(['Column']):
328340
self.assertRaises(exception.DBNotAllowed,
329341
column.drop)
330342
self.assertRaises(exception.DBNotAllowed,
331343
column.alter)
332344

333345
def test_table(self):
334-
table = sqlalchemy.Table()
346+
table = sa.Table()
335347
with fixtures.BannedDBSchemaOperations(['Table']):
336348
self.assertRaises(exception.DBNotAllowed,
337349
table.drop)

0 commit comments

Comments
 (0)