Support db-manage command for db migration

blueprint: octavia-db-manage

Change-Id: Ib1c8f47b6af4dd5d86acff68bc56ab660241458a
This commit is contained in:
Fei Long Wang 2016-05-11 05:58:38 +12:00
parent ffe2e4fbb5
commit 9f6c6d058b
5 changed files with 124 additions and 12 deletions

View File

@ -105,8 +105,7 @@ function octavia_configure {
iniset $OCTAVIA_CONF haproxy_amphora key_path ${OCTAVIA_AMP_SSH_KEY_PATH}
recreate_database_mysql octavia
iniset $OCTAVIA_DIR/octavia/db/migration/alembic.ini alembic sqlalchemy.url "mysql+pymysql://${DATABASE_USER}:${DATABASE_PASSWORD}@${DATABASE_HOST}:3306/octavia"
alembic -c $OCTAVIA_DIR/octavia/db/migration/alembic.ini upgrade head
octavia-db-manage upgrade head
if [[ -a $OCTAVIA_CERTS_DIR ]] ; then
rm -rf $OCTAVIA_CERTS_DIR

View File

@ -20,7 +20,7 @@ script_location = %(here)s/alembic_migrations
# versions/ directory
# sourceless = false
sqlalchemy.url = mysql+pymysql://root:password@localhost/octavia
sqlalchemy.url =
# Logging configuration

View File

@ -13,13 +13,15 @@
# under the License.
from __future__ import with_statement
from alembic import context
from sqlalchemy import engine_from_config, pool
from logging import config as logging_config
from alembic import context
from sqlalchemy import create_engine
from sqlalchemy import pool
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
octavia_config = config.octavia_config
# Interpret the config file for Python logging.
# This line sets up loggers basically.
@ -49,8 +51,8 @@ def run_migrations_offline():
script output.
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(url=url, target_metadata=target_metadata)
context.configure(url=octavia_config.database.connection,
target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
@ -63,14 +65,14 @@ def run_migrations_online():
and associate a connection with the context.
"""
engine = engine_from_config(
config.get_section(config.config_ini_section),
prefix='sqlalchemy.',
engine = create_engine(
octavia_config.database.connection,
poolclass=pool.NullPool)
connection = engine.connect()
context.configure(connection=connection,
target_metadata=target_metadata)
context.configure(
connection=connection,
target_metadata=target_metadata)
try:
with context.begin_transaction():

110
octavia/db/migration/cli.py Normal file
View File

@ -0,0 +1,110 @@
# Copyright (c) 2016 Catalyst IT Ltd.
#
# 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 alembic import command as alembic_cmd
from alembic import config as alembic_cfg
from alembic import util as alembic_u
from oslo_config import cfg
from oslo_db import options
CONF = cfg.CONF
options.set_defaults(CONF)
def do_alembic_command(config, cmd, *args, **kwargs):
try:
getattr(alembic_cmd, cmd)(config, *args, **kwargs)
except alembic_u.CommandError as e:
alembic_u.err(str(e))
def do_check_migration(config, _cmd):
do_alembic_command(config, 'branches')
def do_upgrade_downgrade(config, cmd):
if not CONF.command.revision and not CONF.command.delta:
raise SystemExit('You must provide a revision or relative delta')
revision = CONF.command.revision
if CONF.command.delta:
sign = '+' if CONF.command.name == 'upgrade' else '-'
revision = sign + str(CONF.command.delta)
do_alembic_command(config, cmd, revision, sql=CONF.command.sql)
def do_stamp(config, cmd):
do_alembic_command(config, cmd,
CONF.command.revision,
sql=CONF.command.sql)
def do_revision(config, cmd):
do_alembic_command(config, cmd,
message=CONF.command.message,
autogenerate=CONF.command.autogenerate,
sql=CONF.command.sql)
def add_command_parsers(subparsers):
for name in ['current', 'history', 'branches']:
parser = subparsers.add_parser(name)
parser.set_defaults(func=do_alembic_command)
parser = subparsers.add_parser('check_migration')
parser.set_defaults(func=do_check_migration)
for name in ['upgrade', 'downgrade']:
parser = subparsers.add_parser(name)
parser.add_argument('--delta', type=int)
parser.add_argument('--sql', action='store_true')
parser.add_argument('revision', nargs='?')
parser.set_defaults(func=do_upgrade_downgrade)
parser = subparsers.add_parser('stamp')
parser.add_argument('--sql', action='store_true')
parser.add_argument('revision')
parser.set_defaults(func=do_stamp)
parser = subparsers.add_parser('revision')
parser.add_argument('-m', '--message')
parser.add_argument('--autogenerate', action='store_true')
parser.add_argument('--sql', action='store_true')
parser.set_defaults(func=do_revision)
command_opt = cfg.SubCommandOpt('command',
title='Command',
help='Available commands',
handler=add_command_parsers)
CONF.register_cli_opt(command_opt)
def main():
config = alembic_cfg.Config(
os.path.join(os.path.dirname(__file__), 'alembic.ini')
)
config.set_main_option('script_location',
'octavia.db.migration:alembic_migrations')
# attach the octavia conf to the Alembic conf
config.octavia_config = CONF
CONF(project='octavia')
CONF.command.func(config, CONF.command.name)

View File

@ -41,6 +41,7 @@ console_scripts =
octavia-worker = octavia.cmd.octavia_worker:main
octavia-health-manager = octavia.cmd.health_manager:main
octavia-housekeeping = octavia.cmd.house_keeping:main
octavia-db-manage = octavia.db.migration.cli:main
amphora-agent = octavia.cmd.agent:main
haproxy-vrrp-check = octavia.cmd.haproxy_vrrp_check:main
octavia.api.handlers =