From 3c6edad94444675999ac82b1fda8342af4bbd833 Mon Sep 17 00:00:00 2001 From: Ilya Pekelny Date: Wed, 16 Apr 2014 18:48:02 +0300 Subject: [PATCH] Migration DB_INIT_VERSION in common place Init version value exists only in migration_helpers. migration_helpers shouldn't know nothing about migrate repos, primarily because it is intended for handling of any migrate repo in the project. Change-Id: I8bceea0cf5eaf8e623efbb39e385b1990eca0a6f Closes-Bug: #1308593 --- keystone/common/sql/migrate_repo/__init__.py | 17 +++++++++++++ keystone/common/sql/migration_helpers.py | 4 ++-- keystone/tests/test_sql_upgrade.py | 25 ++++++++++---------- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/keystone/common/sql/migrate_repo/__init__.py b/keystone/common/sql/migrate_repo/__init__.py index e69de29bb2..f9e063bfc2 100644 --- a/keystone/common/sql/migrate_repo/__init__.py +++ b/keystone/common/sql/migrate_repo/__init__.py @@ -0,0 +1,17 @@ +# Copyright 2014 Mirantis.inc +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + +DB_INIT_VERSION = 35 diff --git a/keystone/common/sql/migration_helpers.py b/keystone/common/sql/migration_helpers.py index 63135fdbe6..a0a7767dcf 100644 --- a/keystone/common/sql/migration_helpers.py +++ b/keystone/common/sql/migration_helpers.py @@ -22,6 +22,7 @@ from migrate import exceptions import sqlalchemy from keystone.common import sql +from keystone.common.sql import migrate_repo from keystone import config from keystone import contrib from keystone import exception @@ -31,7 +32,6 @@ from keystone.openstack.common import importutils from keystone.openstack.common import jsonutils -DB_INIT_VERSION = 35 CONF = config.CONF @@ -135,7 +135,7 @@ def find_migrate_repo(package=None, repo_name='migrate_repo'): def sync_database_to_version(extension=None, version=None): if not extension: abs_path = find_migrate_repo() - init_version = DB_INIT_VERSION + init_version = migrate_repo.DB_INIT_VERSION else: init_version = 0 try: diff --git a/keystone/tests/test_sql_upgrade.py b/keystone/tests/test_sql_upgrade.py index 166bf080a3..881c8cfd2b 100644 --- a/keystone/tests/test_sql_upgrade.py +++ b/keystone/tests/test_sql_upgrade.py @@ -39,6 +39,7 @@ import sqlalchemy.exc from keystone.assignment.backends import sql as assignment_sql from keystone.common import sql +from keystone.common.sql import migrate_repo from keystone.common.sql import migration_helpers from keystone import config from keystone.contrib import federation @@ -231,18 +232,18 @@ class SqlMigrateBase(tests.SQLDriverOverrides, tests.TestCase): class SqlUpgradeTests(SqlMigrateBase): - _initial_db_version = migration_helpers.DB_INIT_VERSION + _initial_db_version = migrate_repo.DB_INIT_VERSION def test_blank_db_to_start(self): self.assertTableDoesNotExist('user') def test_start_version_db_init_version(self): version = migration.db_version(sql.get_engine(), self.repo_path, - migration_helpers.DB_INIT_VERSION) + migrate_repo.DB_INIT_VERSION) self.assertEqual( version, - migration_helpers.DB_INIT_VERSION, - 'DB is not at version %s' % migration_helpers.DB_INIT_VERSION) + migrate_repo.DB_INIT_VERSION, + 'DB is not at version %s' % migrate_repo.DB_INIT_VERSION) def test_two_steps_forward_one_step_back(self): """You should be able to cleanly undo and re-apply all upgrades. @@ -250,7 +251,7 @@ class SqlUpgradeTests(SqlMigrateBase): Upgrades are run in the following order:: Starting with the initial version defined at - keystone.common.migration_helpers.DB_INIT_VERSION + keystone.common.migrate_repo.DB_INIT_VERSION INIT +1 -> INIT +2 -> INIT +1 -> INIT +2 -> INIT +3 -> INIT +2 ... ^---------------------^ ^---------------------^ @@ -270,18 +271,18 @@ class SqlUpgradeTests(SqlMigrateBase): applied. """ - for x in range(migration_helpers.DB_INIT_VERSION + 1, + for x in range(migrate_repo.DB_INIT_VERSION + 1, self.max_version + 1): self.upgrade(x) downgrade_ver = x - 1 # Don't actually downgrade to the init version. This will raise # a not-implemented error. - if downgrade_ver != migration_helpers.DB_INIT_VERSION: + if downgrade_ver != migrate_repo.DB_INIT_VERSION: self.downgrade(x - 1) self.upgrade(x) def test_upgrade_add_initial_tables(self): - self.upgrade(migration_helpers.DB_INIT_VERSION + 1) + self.upgrade(migrate_repo.DB_INIT_VERSION + 1) self.check_initial_table_structure() def check_initial_table_structure(self): @@ -310,7 +311,7 @@ class SqlUpgradeTests(SqlMigrateBase): if self.engine.name == 'mysql': self._mysql_check_all_tables_innodb() - self.downgrade(migration_helpers.DB_INIT_VERSION + 1) + self.downgrade(migrate_repo.DB_INIT_VERSION + 1) self.check_initial_table_structure() meta = sqlalchemy.MetaData() @@ -328,7 +329,7 @@ class SqlUpgradeTests(SqlMigrateBase): # supported. A NotImplementedError should be raised when attempting to # downgrade. self.assertRaises(NotImplementedError, self.downgrade, - migration_helpers.DB_INIT_VERSION) + migrate_repo.DB_INIT_VERSION) def insert_dict(self, session, table_name, d, table=None): """Naively inserts key-value pairs into a table, given a dictionary.""" @@ -1326,12 +1327,12 @@ class SqlUpgradeTests(SqlMigrateBase): class VersionTests(SqlMigrateBase): - _initial_db_version = migration_helpers.DB_INIT_VERSION + _initial_db_version = migrate_repo.DB_INIT_VERSION def test_core_initial(self): """Get the version before migrated, it's the initial DB version.""" version = migration_helpers.get_db_version() - self.assertEqual(migration_helpers.DB_INIT_VERSION, version) + self.assertEqual(migrate_repo.DB_INIT_VERSION, version) def test_core_max(self): """When get the version after upgrading, it's the new version."""