Use common db migrations module from Oslo

Changes due to use common migration module instead of Glance
implementation.
Removed glance.db.sqlalchemy.migration module and related code.

blueprint db-use-oslo-common-code

Change-Id: Ib7e2e478a84a90cc9c773f2733dc60b88834fd23
This commit is contained in:
Victor Sergeyev 2013-11-22 18:20:08 +02:00
parent 446736e020
commit 99fb66ca80
6 changed files with 31 additions and 144 deletions

View File

@ -42,7 +42,8 @@ from oslo.config import cfg
from glance.common import config from glance.common import config
from glance.common import exception from glance.common import exception
from glance.db.sqlalchemy import migration from glance.db import migration as db_migration
from glance.openstack.common.db.sqlalchemy import migration
from glance.openstack.common import log from glance.openstack.common import log
CONF = cfg.CONF CONF = cfg.CONF
@ -65,22 +66,23 @@ class DbCommands(object):
def version(self): def version(self):
"""Print database's current migration level""" """Print database's current migration level"""
print(migration.db_version()) print(migration.db_version(db_migration.MIGRATE_REPO_PATH,
db_migration.INIT_VERSION))
@args('--version', metavar='<version>', help='Database version') @args('--version', metavar='<version>', help='Database version')
def upgrade(self, version=None): def upgrade(self, version=None):
"""Upgrade the database's migration level""" """Upgrade the database's migration level"""
migration.upgrade(version) migration.db_sync(db_migration.MIGRATE_REPO_PATH, version)
@args('--version', metavar='<version>', help='Database version') @args('--version', metavar='<version>', help='Database version')
def downgrade(self, version=None): def downgrade(self, version=None):
"""Downgrade the database's migration level""" """Downgrade the database's migration level"""
migration.downgrade(version) migration.db_sync(db_migration.MIGRATE_REPO_PATH, version)
@args('--version', metavar='<version>', help='Database version') @args('--version', metavar='<version>', help='Database version')
def version_control(self, version=None): def version_control(self, version=None):
"""Place a database under migration control""" """Place a database under migration control"""
migration.version_control(version) migration.db_version_control(db_migration.MIGRATE_REPO_PATH, version)
@args('--version', metavar='<version>', help='Database version') @args('--version', metavar='<version>', help='Database version')
@args('--current_version', metavar='<version>', @args('--current_version', metavar='<version>',
@ -90,7 +92,10 @@ class DbCommands(object):
Place a database under migration control and upgrade/downgrade it, Place a database under migration control and upgrade/downgrade it,
creating first if necessary. creating first if necessary.
""" """
migration.db_sync(version, current_version) if current_version is not None:
migration.db_version_control(db_migration.MIGRATE_REPO_PATH,
current_version)
migration.db_sync(db_migration.MIGRATE_REPO_PATH, version)
def add_legacy_command_parsers(command_object, subparsers): def add_legacy_command_parsers(command_object, subparsers):

View File

@ -19,20 +19,31 @@
"""Database setup and migration commands.""" """Database setup and migration commands."""
import os
from glance.common import utils from glance.common import utils
IMPL = utils.LazyPluggable('db_backend', IMPL = utils.LazyPluggable(
sqlalchemy='glance.db.sqlalchemy.migration') 'backend',
config_group='database',
sqlalchemy='glance.openstack.common.db.sqlalchemy.migration')
INIT_VERSION = 000 INIT_VERSION = 000
MIGRATE_REPO_PATH = os.path.join(
os.path.abspath(os.path.dirname(__file__)),
'sqlalchemy',
'migrate_repo',
)
def db_sync(version=None): def db_sync(version=None):
"""Migrate the database to `version` or the most recent version.""" """Migrate the database to `version` or the most recent version."""
return IMPL.db_sync(version=version) return IMPL.db_sync(abs_path=MIGRATE_REPO_PATH, version=version)
def db_version(): def db_version():
"""Display the current database version.""" """Display the current database version."""
return IMPL.db_version() return IMPL.db_version(abs_path=MIGRATE_REPO_PATH,
init_version=INIT_VERSION)

View File

@ -1,129 +0,0 @@
# Copyright 2011 OpenStack Foundation
# 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.
import os
from migrate import exceptions as versioning_exceptions
from migrate.versioning import api as versioning_api
from migrate.versioning import repository as versioning_repository
from oslo.config import cfg
from glance.common import exception
import glance.openstack.common.log as logging
LOG = logging.getLogger(__name__)
CONF = cfg.CONF
def db_version():
"""
Return the database's current migration number
:retval version number
"""
repo_path = get_migrate_repo_path()
sql_connection = CONF.database.connection
try:
return versioning_api.db_version(sql_connection, repo_path)
except versioning_exceptions.DatabaseNotControlledError as e:
msg = (_("database is not under migration control"))
raise exception.DatabaseMigrationError(msg)
def upgrade(version=None):
"""
Upgrade the database's current migration level
:param version: version to upgrade (defaults to latest)
:retval version number
"""
db_version() # Ensure db is under migration control
repo_path = get_migrate_repo_path()
sql_connection = CONF.database.connection
version_str = version or 'latest'
LOG.info(_("Upgrading database to version %s") %
version_str)
return versioning_api.upgrade(sql_connection, repo_path, version)
def downgrade(version):
"""
Downgrade the database's current migration level
:param version: version to downgrade to
:retval version number
"""
db_version() # Ensure db is under migration control
repo_path = get_migrate_repo_path()
sql_connection = CONF.database.connection
LOG.info(_("Downgrading database to version %s") %
version)
return versioning_api.downgrade(sql_connection, repo_path, version)
def version_control(version=None):
"""
Place a database under migration control
"""
try:
_version_control(version)
except versioning_exceptions.DatabaseAlreadyControlledError as e:
msg = (_("database is already under migration control"))
raise exception.DatabaseMigrationError(msg)
def _version_control(version):
"""
Place a database under migration control
This will only set the specific version of a database, it won't
run any migrations.
"""
repo_path = get_migrate_repo_path()
sql_connection = CONF.database.connection
if version is None:
version = versioning_repository.Repository(repo_path).latest
return versioning_api.version_control(sql_connection, repo_path, version)
def db_sync(version=None, current_version=None):
"""
Place a database under migration control and upgrade/downgrade it.
:retval version number
"""
try:
_version_control(current_version or 0)
except versioning_exceptions.DatabaseAlreadyControlledError as e:
pass
if current_version is None:
current_version = int(db_version())
if version is not None and int(version) < current_version:
return downgrade(version=version)
elif version is None or int(version) > current_version:
return upgrade(version=version)
else:
return current_version
def get_migrate_repo_path():
"""Get the path for the migrate repository."""
path = os.path.join(os.path.abspath(os.path.dirname(__file__)),
'migrate_repo')
assert os.path.exists(path)
return path

View File

@ -19,8 +19,8 @@ from oslo.config import cfg
import glance.common.client import glance.common.client
from glance.common import config from glance.common import config
from glance.db import migration
import glance.db.sqlalchemy.api import glance.db.sqlalchemy.api
import glance.db.sqlalchemy.migration
import glance.registry.client.v1.client import glance.registry.client.v1.client
import glance.store import glance.store
from glance import tests as glance_tests from glance import tests as glance_tests
@ -163,7 +163,7 @@ class ApiTest(test_utils.BaseTestCase):
test_utils.execute('cp %s %s/tests.sqlite' test_utils.execute('cp %s %s/tests.sqlite'
% (db_location, self.test_dir)) % (db_location, self.test_dir))
else: else:
glance.db.sqlalchemy.migration.db_sync() migration.db_sync()
# copy the clean db to a temp location so that it # copy the clean db to a temp location so that it
# can be reused for future tests # can be reused for future tests

View File

@ -22,8 +22,8 @@ from oslo.config import cfg
import glance.common.client import glance.common.client
from glance.common import config from glance.common import config
from glance.db import migration
import glance.db.sqlalchemy.api import glance.db.sqlalchemy.api
import glance.db.sqlalchemy.migration
import glance.registry.client.v1.client import glance.registry.client.v1.client
import glance.store import glance.store
from glance import tests as glance_tests from glance import tests as glance_tests
@ -166,7 +166,7 @@ class ApiTest(test_utils.BaseTestCase):
test_utils.execute('cp %s %s/tests.sqlite' test_utils.execute('cp %s %s/tests.sqlite'
% (db_location, self.test_dir)) % (db_location, self.test_dir))
else: else:
glance.db.sqlalchemy.migration.db_sync() migration.db_sync()
# copy the clean db to a temp location so that it # copy the clean db to a temp location so that it
# can be reused for future tests # can be reused for future tests

View File

@ -33,6 +33,7 @@ import subprocess
import urlparse import urlparse
import uuid import uuid
from migrate.versioning import api as migration_api
from migrate.versioning.repository import Repository from migrate.versioning.repository import Repository
from oslo.config import cfg from oslo.config import cfg
from six.moves import xrange from six.moves import xrange
@ -42,7 +43,6 @@ from glance.common import crypt
from glance.common import utils from glance.common import utils
import glance.db.migration as migration import glance.db.migration as migration
import glance.db.sqlalchemy.migrate_repo import glance.db.sqlalchemy.migrate_repo
from glance.db.sqlalchemy.migration import versioning_api as migration_api
from glance.db.sqlalchemy import models from glance.db.sqlalchemy import models
from glance.openstack.common import jsonutils from glance.openstack.common import jsonutils
from glance.openstack.common import log as logging from glance.openstack.common import log as logging