From 345714551267186eaf5cfa8808cc6d7a58feb4e9 Mon Sep 17 00:00:00 2001 From: Yipei Niu Date: Fri, 6 Jan 2017 14:28:53 +0800 Subject: [PATCH] DB migration version 1. What is the problem Based on the bug report https://bugs.launchpad.net/tricircle/+bug/1652708, currently the version migrated to is fixed to 2 in cmd/manage.py. 2. What is the solution to the problem We should use the version specified in the CLI, if no target version is specified, then migrate the db to the latest version. 3. What the features need to be implemented to the Tricircle No new features Change-Id: I00703578ecd2017836be9ac6e93a4bfd784e650e --- devstack/plugin.sh | 2 +- tricircle/cmd/manage.py | 47 ++++++++++++++++++++++++++----- tricircle/db/migration_helpers.py | 7 +++++ 3 files changed, 48 insertions(+), 8 deletions(-) diff --git a/devstack/plugin.sh b/devstack/plugin.sh index 5c6deb0b..11953848 100644 --- a/devstack/plugin.sh +++ b/devstack/plugin.sh @@ -153,7 +153,7 @@ if [[ "$Q_ENABLE_TRICIRCLE" == "True" ]]; then setup_package $TRICIRCLE_DIR -e recreate_database tricircle - tricircle-db-manage "$TRICIRCLE_API_CONF" + tricircle-db-manage --config-file="$TRICIRCLE_API_CONF" db_sync if is_service_enabled q-svc ; then start_central_neutron_server $CENTRAL_REGION_NAME $TRICIRCLE_NEUTRON_PORT diff --git a/tricircle/cmd/manage.py b/tricircle/cmd/manage.py index 969bfa4c..16eedb54 100644 --- a/tricircle/cmd/manage.py +++ b/tricircle/cmd/manage.py @@ -17,19 +17,52 @@ import sys from oslo_config import cfg +from oslo_log import log as logging from tricircle.db import core from tricircle.db import migration_helpers +import pbr.version + +CONF = cfg.CONF + + +def do_db_version(): + print(migration_helpers.db_version()) + + +def do_db_sync(): + migration_helpers.sync_repo(CONF.command.version) + + +def add_command_parsers(subparsers): + parser = subparsers.add_parser('db_version') + parser.set_defaults(func=do_db_version) + + parser = subparsers.add_parser('db_sync') + parser.set_defaults(func=do_db_sync) + parser.add_argument('version', nargs='?') + +command_opt = cfg.SubCommandOpt('command', + title='Commands', + help='Show available commands.', + handler=add_command_parsers) + def main(): core.initialize() - cfg.CONF(args=sys.argv[2:], - project='tricircle', - default_config_files=[sys.argv[1]]) - migration_helpers.find_migrate_repo() - migration_helpers.sync_repo(2) + logging.register_options(CONF) + logging.setup(CONF, 'tricircle-db-manage') + CONF.register_cli_opt(command_opt) + version_info = pbr.version.VersionInfo('tricircle') + try: + CONF(sys.argv[1:], project='tricircle', prog='tricircle-db-manage', + version=version_info.version_string()) + except RuntimeError as e: + sys.exit("ERROR: %s" % e) -if __name__ == '__main__': - main() + try: + CONF.command.func() + except Exception as e: + sys.exit("ERROR: %s" % e) diff --git a/tricircle/db/migration_helpers.py b/tricircle/db/migration_helpers.py index f40976e4..7fdbd30f 100644 --- a/tricircle/db/migration_helpers.py +++ b/tricircle/db/migration_helpers.py @@ -23,6 +23,13 @@ from tricircle.db import core from tricircle.db import migrate_repo +def db_version(): + engine = core.get_engine() + repo_abs_path = find_migrate_repo() + init_version = migrate_repo.DB_INIT_VERSION + return migration.db_version(engine, repo_abs_path, init_version) + + def find_migrate_repo(package=None, repo_name='migrate_repo'): package = package or db path = os.path.abspath(os.path.join(