Address db_sync check against new install

This patch fixes a bug and causes a log message along with an exit
code to be returned when a DBMigration error is raised.

Change-Id: Iba7aff606937561ad98e2ef551ca4005bd4f337d
Closes-Bug: #1663627
This commit is contained in:
Richard Avelar 2017-02-10 16:33:33 +00:00
parent d5ce8ea0ed
commit 99db3c83e0
3 changed files with 28 additions and 7 deletions

View File

@ -225,7 +225,7 @@ authenticate requests normally.
the previous release. the previous release.
Using db_sync check Using db_sync check
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~
In order to check the current state of your rolling upgrades, you may run the In order to check the current state of your rolling upgrades, you may run the
command ``keystone-manage db_sync --check``. This will inform you of any command ``keystone-manage db_sync --check``. This will inform you of any
@ -239,8 +239,9 @@ can make from your current version. Here are a list of possible return codes.
database and operator intervention will be required. database and operator intervention will be required.
* A return code of ``2`` means that an upgrade from your current database * A return code of ``2`` means that an upgrade from your current database
version is available and your first step is to run ``keystone-manage version is available, your database is not currently under version control,
db_sync --expand``. or the database is already under control. Your first step is to run
``keystone-manage db_sync --expand``.
* A return code of ``3`` means that the expansion stage is complete, and the * A return code of ``3`` means that the expansion stage is complete, and the
next step is to run ``keystone-manage db_sync --migrate``. next step is to run ``keystone-manage db_sync --migrate``.

View File

@ -21,6 +21,7 @@ import uuid
import migrate import migrate
from oslo_config import cfg from oslo_config import cfg
from oslo_db.sqlalchemy import migration
from oslo_log import log from oslo_log import log
from oslo_log import versionutils from oslo_log import versionutils
from oslo_serialization import jsonutils from oslo_serialization import jsonutils
@ -457,10 +458,17 @@ class DbSync(BaseApp):
@classmethod @classmethod
def check_db_sync_status(self): def check_db_sync_status(self):
status = 0 status = 0
expand_version = upgrades.get_db_version(repo='expand_repo') try:
migrate_version = upgrades.get_db_version( expand_version = upgrades.get_db_version(repo='expand_repo')
repo='data_migration_repo') migrate_version = upgrades.get_db_version(
contract_version = upgrades.get_db_version(repo='contract_repo') repo='data_migration_repo')
contract_version = upgrades.get_db_version(repo='contract_repo')
except migration.exception.DbMigrationError:
LOG.info(_LI('Your database is not currently under version '
'control or the database is already controlled. Your '
'first step is to run `keystone-manage db_sync '
'--expand`.'))
return 2
repo = migrate.versioning.repository.Repository( repo = migrate.versioning.repository.Repository(
upgrades.find_repo('expand_repo')) upgrades.find_repo('expand_repo'))

View File

@ -19,6 +19,7 @@ import uuid
import fixtures import fixtures
import mock import mock
import oslo_config.fixture import oslo_config.fixture
from oslo_db.sqlalchemy import migration
from oslo_log import log from oslo_log import log
from oslotest import mockpatch from oslotest import mockpatch
from six.moves import configparser from six.moves import configparser
@ -680,6 +681,17 @@ class CliDBSyncTestCase(unit.BaseTestCase):
cli.DbSync.main() cli.DbSync.main()
self._assert_correct_call(upgrades.contract_schema) self._assert_correct_call(upgrades.contract_schema)
@mock.patch('keystone.cmd.cli.upgrades.get_db_version')
def test_db_sync_check_when_database_is_empty(self, mocked_get_db_version):
e = migration.exception.DbMigrationError("Invalid version")
mocked_get_db_version.side_effect = e
checker = cli.DbSync()
log_info = self.useFixture(fixtures.FakeLogger(level=log.INFO))
status = checker.check_db_sync_status()
self.assertIn("not currently under version control", log_info.output)
self.assertEqual(status, 2)
class TestMappingPopulate(unit.SQLDriverOverrides, unit.TestCase): class TestMappingPopulate(unit.SQLDriverOverrides, unit.TestCase):