From 2a2f8535e2858400d68af74a8d813ca278f7a1b6 Mon Sep 17 00:00:00 2001 From: Lance Bragstad Date: Mon, 19 Jun 2017 17:41:13 +0000 Subject: [PATCH] Improve handling of database migration checks The `--check` subcommand is suppose to provide useful information and status codes depending on the state of the keystone database. Operators and automation use this information to determine what their next step is in a rolling upgrade. The current logic is broken becuase it doesn't account for new installations that might be relying on this information. This change breaks that case into multiple try/except statements and handles each appropriately so that the status code and logging information is accurate for operators and automation using this information for upgrading a new keystone database. Change-Id: I331fa663a99f79ea9a79a75e4ae07c45278556bf Closes-Bug: 1698900 --- keystone/cmd/cli.py | 12 +++++++++--- keystone/tests/unit/test_sql_upgrade.py | 7 +++++++ releasenotes/notes/bug_1698900-f195125bf341d887.yaml | 6 ++++++ 3 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 releasenotes/notes/bug_1698900-f195125bf341d887.yaml diff --git a/keystone/cmd/cli.py b/keystone/cmd/cli.py index cc12fa5cfc..57a0be33e7 100644 --- a/keystone/cmd/cli.py +++ b/keystone/cmd/cli.py @@ -459,15 +459,21 @@ class DbSync(BaseApp): status = 0 try: expand_version = upgrades.get_db_version(repo='expand_repo') - migrate_version = upgrades.get_db_version( - repo='data_migration_repo') - contract_version = upgrades.get_db_version(repo='contract_repo') except migration.exception.DbMigrationError: LOG.info('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 + try: + migrate_version = upgrades.get_db_version( + repo='data_migration_repo') + except migration.exception.DbMigrationError: + migrate_version = 0 + try: + contract_version = upgrades.get_db_version(repo='contract_repo') + except migration.exception.DbMigrationError: + contract_version = 0 repo = migrate.versioning.repository.Repository( upgrades.find_repo('expand_repo')) diff --git a/keystone/tests/unit/test_sql_upgrade.py b/keystone/tests/unit/test_sql_upgrade.py index 61d0364958..40a50572c9 100644 --- a/keystone/tests/unit/test_sql_upgrade.py +++ b/keystone/tests/unit/test_sql_upgrade.py @@ -1710,6 +1710,13 @@ class FullMigration(SqlMigrateBase, unit.TestCase): checker = cli.DbSync() latest_version = self.repos[EXPAND_REPO].max_version + # If the expand repository doesn't exist yet, then we need to make sure + # we advertise that `--expand` must be run first. + log_info = self.useFixture(fixtures.FakeLogger(level=log.INFO)) + status = checker.check_db_sync_status() + self.assertIn("keystone-manage db_sync --expand", log_info.output) + self.assertEqual(status, 2) + # Assert the correct message is printed when expand is the first step # that needs to run self.expand(1) diff --git a/releasenotes/notes/bug_1698900-f195125bf341d887.yaml b/releasenotes/notes/bug_1698900-f195125bf341d887.yaml new file mode 100644 index 0000000000..252625b459 --- /dev/null +++ b/releasenotes/notes/bug_1698900-f195125bf341d887.yaml @@ -0,0 +1,6 @@ +fixes: + - | + The implementation for checking database state during an upgrade with the + use of `keystone-manage db_sync --check` has been corrected. This allows + users and automation to determine what step is next in a rolling upgrade + based on logging and command status codes.