From f7705f3a7d6acd8319318afc0fd9d1b534285f7d Mon Sep 17 00:00:00 2001 From: Zhi Yan Liu Date: Sat, 1 Feb 2014 06:24:16 +0800 Subject: [PATCH] Make table utf-8 charset checking be optional for DB migration Add an option to the migration db_sync call to allow a project to disable the check for utf8 encoding on all tables. This allows the project to fix problems automatically via a migration, which otherwise would not be possible because the utf8 check would stop them from running migrations. Change-Id: I7f271d846141ac72dde3fb0d12159b125018eb2c Closes-bug: #1279000 Signed-off-by: Zhi Yan Liu --- openstack/common/db/sqlalchemy/migration.py | 7 +++++-- tests/unit/db/sqlalchemy/test_migration_common.py | 12 ++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/openstack/common/db/sqlalchemy/migration.py b/openstack/common/db/sqlalchemy/migration.py index ab19ce0ef..bc5a71602 100644 --- a/openstack/common/db/sqlalchemy/migration.py +++ b/openstack/common/db/sqlalchemy/migration.py @@ -168,7 +168,7 @@ def patch_migrate(): sqlite.SQLiteConstraintGenerator) -def db_sync(engine, abs_path, version=None, init_version=0): +def db_sync(engine, abs_path, version=None, init_version=0, sanity_check=True): """Upgrade or downgrade a database. Function runs the upgrade() or downgrade() functions in change scripts. @@ -179,7 +179,9 @@ def db_sync(engine, abs_path, version=None, init_version=0): If None - database will update to the latest available version. :param init_version: Initial database version + :param sanity_check: Require schema sanity checking for all tables """ + if version is not None: try: version = int(version) @@ -189,7 +191,8 @@ def db_sync(engine, abs_path, version=None, init_version=0): current_version = db_version(engine, abs_path, init_version) repository = _find_migrate_repo(abs_path) - _db_schema_sanity_check(engine) + if sanity_check: + _db_schema_sanity_check(engine) if version is None or version > current_version: return versioning_api.upgrade(engine, repository, version) else: diff --git a/tests/unit/db/sqlalchemy/test_migration_common.py b/tests/unit/db/sqlalchemy/test_migration_common.py index eae24bdd7..91d746bf7 100644 --- a/tests/unit/db/sqlalchemy/test_migration_common.py +++ b/tests/unit/db/sqlalchemy/test_migration_common.py @@ -176,6 +176,18 @@ class TestMigrationCommon(test_base.DbTestCase): mock_sanity.assert_called_once() + def test_db_sync_sanity_skipped(self): + with contextlib.nested( + mock.patch.object(migration, '_find_migrate_repo'), + mock.patch.object(migration, '_db_schema_sanity_check'), + mock.patch.object(versioning_api, 'downgrade') + ) as (mock_find_repo, mock_sanity, mock_downgrade): + + mock_find_repo.return_value = self.return_value + migration.db_sync(self.engine, self.path, self.test_version, False) + + mock_sanity.assert_not_called() + def test_db_sanity_table_not_utf8(self): with mock.patch.object(self, 'engine') as mock_eng: type(mock_eng).name = mock.PropertyMock(return_value='mysql')