implements selecting version in db sync

Fixes Bug #993284

db_sync compares the current version with the specified version
and upgrades or downgrades accordingly.

Change-Id: If0e0608ac04c264df7964f8cf6a46e9b156eee49
This commit is contained in:
Sathish Nagappan 2012-10-04 02:27:20 -07:00 committed by sathish-nagappan
parent a1c9364e1d
commit a88b8d8dcb
2 changed files with 21 additions and 5 deletions

View File

@ -119,12 +119,14 @@ def db_sync(version=None, current_version=None):
try:
_version_control(current_version or 0)
except versioning_exceptions.DatabaseAlreadyControlledError, e:
if current_version is not None:
msg = (_("database '%(sql_connection)s' is already under "
"migration control") % locals())
raise exception.DatabaseMigrationError(msg)
pass
upgrade(version=version)
if current_version is None:
current_version = int(db_version())
if version is not None and int(version) < current_version:
downgrade(version=version)
elif version is None or int(version) > current_version:
upgrade(version=version)
def get_migrate_repo_path():

View File

@ -211,6 +211,20 @@ class TestMigrations(utils.BaseTestCase):
cur_version = migration_api.db_version()
self.assertEqual(cur_version, version)
def test_db_sync(self):
initial_version = 0
migration_api.db_sync(initial_version)
cur_version = migration_api.db_version()
self.assertEqual(cur_version, initial_version)
migration_api.db_sync(TestMigrations.REPOSITORY.latest)
cur_version = migration_api.db_version()
self.assertEqual(cur_version, TestMigrations.REPOSITORY.latest)
migration_api.db_sync(initial_version)
cur_version = migration_api.db_version()
self.assertEqual(cur_version, initial_version)
def test_no_data_loss_2_to_3_to_2(self):
"""
Here, we test that in the case when we moved a column "type" from the