Skip to content

Commit 5968e2d

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Centralize sqlite FK constraint enforcement" into stable/victoria
2 parents c7d9d6d + 382d64e commit 5968e2d

File tree

4 files changed

+22
-30
lines changed

4 files changed

+22
-30
lines changed

nova/test.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,12 @@
5151
from oslotest import mock_fixture
5252
import six
5353
from six.moves import builtins
54+
from sqlalchemy.dialects import sqlite
5455
import testtools
5556

5657
from nova.compute import rpcapi as compute_rpcapi
5758
from nova import context
59+
from nova.db.sqlalchemy import api as sqlalchemy_api
5860
from nova import exception
5961
from nova import objects
6062
from nova.objects import base as objects_base
@@ -376,6 +378,22 @@ def flags(self, **kw):
376378
for k, v in kw.items():
377379
CONF.set_override(k, v, group)
378380

381+
def enforce_fk_constraints(self, engine=None):
382+
if engine is None:
383+
engine = sqlalchemy_api.get_engine()
384+
dialect = engine.url.get_dialect()
385+
if dialect == sqlite.dialect:
386+
# We're seeing issues with foreign key support in SQLite 3.6.20
387+
# SQLAlchemy doesn't support it at all with < SQLite 3.6.19
388+
# It works fine in SQLite 3.7.
389+
# So return early to skip this test if running SQLite < 3.7
390+
import sqlite3
391+
tup = sqlite3.sqlite_version_info
392+
if tup[0] < 3 or (tup[0] == 3 and tup[1] < 7):
393+
self.skipTest(
394+
'sqlite version too old for reliable SQLA foreign_keys')
395+
engine.connect().execute("PRAGMA foreign_keys = ON")
396+
379397
def start_service(self, name, host=None, cell_name=None, **kwargs):
380398
# Disallow starting multiple scheduler services
381399
if name == 'scheduler' and self._service_fixture_count[name]:

nova/tests/functional/db/test_archive.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
from dateutil import parser as dateutil_parser
1919
from oslo_utils import timeutils
20-
from sqlalchemy.dialects import sqlite
2120
from sqlalchemy import func
2221
from sqlalchemy import MetaData
2322
from sqlalchemy import select
@@ -33,22 +32,7 @@ class TestDatabaseArchive(integrated_helpers._IntegratedTestBase):
3332

3433
def setUp(self):
3534
super(TestDatabaseArchive, self).setUp()
36-
# TODO(mriedem): pull this out so we can re-use it in
37-
# test_archive_deleted_rows_fk_constraint
38-
# SQLite doesn't enforce foreign key constraints without a pragma.
39-
engine = sqlalchemy_api.get_engine()
40-
dialect = engine.url.get_dialect()
41-
if dialect == sqlite.dialect:
42-
# We're seeing issues with foreign key support in SQLite 3.6.20
43-
# SQLAlchemy doesn't support it at all with < SQLite 3.6.19
44-
# It works fine in SQLite 3.7.
45-
# So return early to skip this test if running SQLite < 3.7
46-
import sqlite3
47-
tup = sqlite3.sqlite_version_info
48-
if tup[0] < 3 or (tup[0] == 3 and tup[1] < 7):
49-
self.skipTest(
50-
'sqlite version too old for reliable SQLA foreign_keys')
51-
engine.connect().execute("PRAGMA foreign_keys = ON")
35+
self.enforce_fk_constraints()
5236

5337
def test_archive_deleted_rows(self):
5438
# Boots a server, deletes it, and then tries to archive it.

nova/tests/functional/test_nova_manage.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,6 +1767,7 @@ class TestDBArchiveDeletedRows(integrated_helpers._IntegratedTestBase):
17671767

17681768
def setUp(self):
17691769
super(TestDBArchiveDeletedRows, self).setUp()
1770+
self.enforce_fk_constraints()
17701771
self.cli = manage.DbCommands()
17711772
self.output = StringIO()
17721773
self.useFixture(fixtures.MonkeyPatch('sys.stdout', self.output))
@@ -1812,6 +1813,7 @@ class TestDBArchiveDeletedRowsMultiCell(integrated_helpers.InstanceHelperMixin,
18121813

18131814
def setUp(self):
18141815
super(TestDBArchiveDeletedRowsMultiCell, self).setUp()
1816+
self.enforce_fk_constraints()
18151817
self.useFixture(nova_fixtures.NeutronFixture(self))
18161818
self.useFixture(nova_fixtures.GlanceFixture(self))
18171819
self.useFixture(func_fixtures.PlacementFixture())

nova/tests/unit/db/test_db_api.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import six
4040
from six.moves import range
4141
from sqlalchemy import Column
42-
from sqlalchemy.dialects import sqlite
4342
from sqlalchemy.exc import OperationalError
4443
from sqlalchemy.exc import SQLAlchemyError
4544
from sqlalchemy import inspect
@@ -6440,18 +6439,7 @@ def test_archive_deleted_rows_shadow_insertions_equals_deletions(self):
64406439

64416440
def _check_sqlite_version_less_than_3_7(self):
64426441
# SQLite doesn't enforce foreign key constraints without a pragma.
6443-
dialect = self.engine.url.get_dialect()
6444-
if dialect == sqlite.dialect:
6445-
# We're seeing issues with foreign key support in SQLite 3.6.20
6446-
# SQLAlchemy doesn't support it at all with < SQLite 3.6.19
6447-
# It works fine in SQLite 3.7.
6448-
# So return early to skip this test if running SQLite < 3.7
6449-
import sqlite3
6450-
tup = sqlite3.sqlite_version_info
6451-
if tup[0] < 3 or (tup[0] == 3 and tup[1] < 7):
6452-
self.skipTest(
6453-
'sqlite version too old for reliable SQLA foreign_keys')
6454-
self.conn.execute("PRAGMA foreign_keys = ON")
6442+
self.enforce_fk_constraints(engine=self.engine)
64556443

64566444
def test_archive_deleted_rows_for_migrations(self):
64576445
# migrations.instance_uuid depends on instances.uuid

0 commit comments

Comments
 (0)