Unify using of migration tools

Currently magnum has two type of migration tools, one is oslo.db
migration_cli, other is calling alembic migration tool directly.
This fixes to get more consistency.

Closes-Bug: #1487248
Change-Id: I705dd2cc65c1f879ce1e2ebaaf2015dc6dc24c64
This commit is contained in:
OTSUKA, Yuanying 2015-08-21 09:44:17 +09:00
parent 327d2eb852
commit 45a2c4d1ff
6 changed files with 70 additions and 112 deletions

View File

@ -13,35 +13,31 @@
"""Starter script for magnum-db-manage."""
import os
from oslo_config import cfg
from oslo_db import options
from oslo_db.sqlalchemy.migration_cli import manager
from oslo_log import log as logging
from magnum.i18n import _
from magnum.db import migration
LOG = logging.getLogger(__name__)
CONF = cfg.CONF
def do_version(mgr):
print('Current DB revision is %s' % mgr.version())
def do_version():
print('Current DB revision is %s' % migration.version())
def do_upgrade(mgr):
mgr.upgrade(CONF.command.revision)
def do_upgrade():
migration.upgrade(CONF.command.revision)
def do_stamp(mgr):
mgr.stamp(CONF.command.revision)
def do_stamp():
migration.stamp(CONF.command.revision)
def do_revision(mgr):
mgr.revision(message=CONF.command.message,
autogenerate=CONF.command.autogenerate)
def do_revision():
migration.revision(message=CONF.command.message,
autogenerate=CONF.command.autogenerate)
def add_command_parsers(subparsers):
@ -62,23 +58,6 @@ def add_command_parsers(subparsers):
parser.set_defaults(func=do_revision)
def get_manager():
if cfg.CONF.database.connection is None:
raise ValueError(
_('Could not find parameter database.connection in config file'))
alembic_path = os.path.abspath(
os.path.join(os.path.dirname(__file__),
'..', 'db', 'sqlalchemy', 'alembic.ini'))
migrate_path = os.path.abspath(
os.path.join(os.path.dirname(__file__),
'..', 'db', 'sqlalchemy', 'alembic'))
migration_config = {'alembic_ini_path': alembic_path,
'alembic_repo_path': migrate_path,
'db_url': CONF.database.connection}
return manager.MigrationManager(migration_config)
def main():
command_opt = cfg.SubCommandOpt('command',
title='Command',
@ -86,8 +65,5 @@ def main():
handler=add_command_parsers)
CONF.register_cli_opt(command_opt)
# set_defaults() is called to register the db options.
options.set_defaults(CONF)
CONF(project='magnum')
CONF.command.func(get_manager())
CONF.command.func()

View File

@ -0,0 +1,31 @@
# Copyright 2015 NEC Corporation. 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.
from oslo_config import cfg
from oslo_db import options
from magnum.common import paths
sql_opts = [
cfg.StrOpt('mysql_engine',
default='InnoDB',
help='MySQL engine to use.')
]
_DEFAULT_SQL_CONNECTION = 'sqlite:///' + paths.state_path_def('magnum.sqlite')
cfg.CONF.register_opts(sql_opts, 'database')
options.set_defaults(cfg.CONF, _DEFAULT_SQL_CONNECTION, 'magnum.sqlite')

View File

@ -46,7 +46,3 @@ def stamp(version):
def revision(message, autogenerate):
return get_backend().revision(message, autogenerate)
def create_schema():
return get_backend().create_schema()

View File

@ -16,77 +16,48 @@
import os
import alembic
from alembic import config as alembic_config
import alembic.migration as alembic_migration
from oslo_db import exception as db_exc
from oslo_config import cfg
from oslo_db.sqlalchemy.migration_cli import manager
from magnum.db.sqlalchemy import api as sqla_api
from magnum.db.sqlalchemy import models
_MANAGER = None
def _alembic_config():
path = os.path.join(os.path.dirname(__file__), 'alembic.ini')
config = alembic_config.Config(path)
return config
def get_manager():
global _MANAGER
if not _MANAGER:
alembic_path = os.path.abspath(
os.path.join(os.path.dirname(__file__), 'alembic.ini'))
migrate_path = os.path.abspath(
os.path.join(os.path.dirname(__file__), 'alembic'))
migration_config = {'alembic_ini_path': alembic_path,
'alembic_repo_path': migrate_path,
'db_url': cfg.CONF.database.connection}
_MANAGER = manager.MigrationManager(migration_config)
return _MANAGER
def version(config=None, engine=None):
def version():
"""Current database version.
:returns: Database version
:rtype: string
"""
if engine is None:
engine = sqla_api.get_engine()
with engine.connect() as conn:
context = alembic_migration.MigrationContext.configure(conn)
return context.get_current_revision()
return get_manager().version()
def upgrade(revision, config=None):
def upgrade(version):
"""Used for upgrading database.
:param version: Desired database version
:type version: string
"""
revision = revision or 'head'
config = config or _alembic_config()
version = version or 'head'
alembic.command.upgrade(config, revision or 'head')
get_manager().upgrade(version)
def create_schema(config=None, engine=None):
"""Create database schema from models description.
Can be used for initial installation instead of upgrade('head').
"""
if engine is None:
engine = sqla_api.get_engine()
# NOTE(viktors): If we will use metadata.create_all() for non empty db
# schema, it will only add the new tables, but leave
# existing as is. So we should avoid of this situation.
if version(engine=engine) is not None:
raise db_exc.DbMigrationError("DB schema is already under version"
" control. Use upgrade() instead")
models.Base.metadata.create_all(engine)
stamp('head', config=config)
def downgrade(revision, config=None):
"""Used for downgrading database.
:param version: Desired database version
:type version: string
"""
revision = revision or 'base'
config = config or _alembic_config()
return alembic.command.downgrade(config, revision)
def stamp(revision, config=None):
def stamp(revision):
"""Stamps database with provided revision.
Don't run any migrations.
@ -95,11 +66,10 @@ def stamp(revision, config=None):
database with most recent revision
:type revision: string
"""
config = config or _alembic_config()
return alembic.command.stamp(config, revision=revision)
get_manager().stamp(revision)
def revision(message=None, autogenerate=False, config=None):
def revision(message=None, autogenerate=False):
"""Creates template for migration.
:param message: Text that will be used for migration title
@ -108,6 +78,4 @@ def revision(message=None, autogenerate=False, config=None):
state
:type autogenerate: bool
"""
config = config or _alembic_config()
return alembic.command.revision(config, message=message,
autogenerate=autogenerate)
return get_manager().revision(message=message, autogenerate=autogenerate)

View File

@ -19,7 +19,6 @@ SQLAlchemy models for container service
import json
from oslo_config import cfg
from oslo_db import options as db_options
from oslo_db.sqlalchemy import models
import six.moves.urllib.parse as urlparse
from sqlalchemy import Column
@ -30,21 +29,6 @@ from sqlalchemy import String
from sqlalchemy import Text
from sqlalchemy.types import TypeDecorator, TEXT
from magnum.common import paths
sql_opts = [
cfg.StrOpt('mysql_engine',
default='InnoDB',
help='MySQL engine to use.')
]
_DEFAULT_SQL_CONNECTION = 'sqlite:///' + paths.state_path_def('magnum.sqlite')
cfg.CONF.register_opts(sql_opts, 'database')
db_options.set_defaults(cfg.CONF, _DEFAULT_SQL_CONNECTION, 'magnum.sqlite')
def table_args():
engine_name = urlparse.urlparse(cfg.CONF.database.connection).scheme

View File

@ -59,5 +59,8 @@ magnum.template_definitions =
magnum_vm_atomic_swarm = magnum.conductor.template_definition:AtomicSwarmTemplateDefinition
magnum_vm_ubuntu_mesos = magnum.conductor.template_definition:UbuntuMesosTemplateDefinition
magnum.database.migration_backend =
sqlalchemy = magnum.db.sqlalchemy.migration
[wheel]
universal = 1