From a6172924d5fce8aca4a2a718e2a1f45df6f81372 Mon Sep 17 00:00:00 2001 From: dineshbhor Date: Fri, 11 Mar 2016 07:00:30 +0000 Subject: [PATCH] Raise only DbMigrationError from migrate 'upgrade' method All the exceptions which are raised from sqlalchemy-migrate upgrade method are not more user friendly to the operator. This patch proposes to convert all the exceptions which will be raised by 'upgrade' method to an oslo_db specific 'DbMigrationError' exception. Related-Bug: #1546441 Change-Id: I28533bedba358281ce302b9b021744387a76c04a --- oslo_db/sqlalchemy/migration.py | 5 ++++- .../tests/sqlalchemy/test_migration_common.py | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/oslo_db/sqlalchemy/migration.py b/oslo_db/sqlalchemy/migration.py index f6a4d555..457dd359 100644 --- a/oslo_db/sqlalchemy/migration.py +++ b/oslo_db/sqlalchemy/migration.py @@ -75,7 +75,10 @@ def db_sync(engine, abs_path, version=None, init_version=0, sanity_check=True): if sanity_check: _db_schema_sanity_check(engine) if version is None or version > current_version: - migration = versioning_api.upgrade(engine, repository, version) + try: + migration = versioning_api.upgrade(engine, repository, version) + except Exception as ex: + raise exception.DbMigrationError(ex) else: migration = versioning_api.downgrade(engine, repository, version) diff --git a/oslo_db/tests/sqlalchemy/test_migration_common.py b/oslo_db/tests/sqlalchemy/test_migration_common.py index 95d031e6..870f2082 100644 --- a/oslo_db/tests/sqlalchemy/test_migration_common.py +++ b/oslo_db/tests/sqlalchemy/test_migration_common.py @@ -192,6 +192,22 @@ class TestMigrationCommon(test_base.DbTestCase): self.assertRaises(db_exception.DBMigrationError, migration.db_sync, self.engine, self.path, 'foo') + @mock.patch.object(versioning_api, 'upgrade') + def test_db_sync_script_not_present(self, upgrade): + # For non existent migration script file sqlalchemy-migrate will raise + # VersionNotFoundError which will be wrapped in DbMigrationError. + upgrade.side_effect = migrate_exception.VersionNotFoundError + self.assertRaises(db_exception.DbMigrationError, + migration.db_sync, self.engine, self.path, + self.test_version + 1) + + @mock.patch.object(versioning_api, 'upgrade') + def test_db_sync_known_error_raised(self, upgrade): + upgrade.side_effect = migrate_exception.KnownError + self.assertRaises(db_exception.DbMigrationError, + migration.db_sync, self.engine, self.path, + self.test_version + 1) + def test_db_sync_upgrade(self): init_ver = 55 with test_utils.nested(