From 9054c32d838b2b8e56d9c02f16311affb35f5735 Mon Sep 17 00:00:00 2001 From: Abhishek Raut Date: Fri, 14 Aug 2015 03:34:28 -0700 Subject: [PATCH] NSX: Register migrations at install time This allows neutron-db-manage to find alembic migrations for vmware-nsx repo. Partial-Bug: #1470625 Change-Id: Icf2d68002ec79c75b4897f6b0bee5180570abe67 --- tox.ini | 1 + vmware_nsx/neutron/db/__init__.py | 0 vmware_nsx/neutron/db/migration/__init__.py | 0 .../migration/alembic_migrations/__init__.py | 14 ++ .../db/migration/alembic_migrations/env.py | 124 ++++++++++++++++++ .../alembic_migrations/versions/HEADS | 1 - .../versions/kilo_release.py | 31 +++++ ...6_initial_liberty_no_op_contract_script.py | 10 +- ...95e_initial_liberty_no_op_expand_script.py | 10 +- 9 files changed, 182 insertions(+), 9 deletions(-) create mode 100644 vmware_nsx/neutron/db/__init__.py create mode 100644 vmware_nsx/neutron/db/migration/__init__.py create mode 100644 vmware_nsx/neutron/db/migration/alembic_migrations/__init__.py create mode 100644 vmware_nsx/neutron/db/migration/alembic_migrations/env.py create mode 100644 vmware_nsx/neutron/db/migration/alembic_migrations/versions/kilo_release.py diff --git a/tox.ini b/tox.ini index 7c7d287c0a..12be0c7eee 100644 --- a/tox.ini +++ b/tox.ini @@ -42,6 +42,7 @@ downloadcache = ~/cache/pip commands = sh ./tools/check_bash.sh flake8 + neutron-db-manage --subproject vmware-nsx check_migration whitelist_externals = sh [testenv:i18n] diff --git a/vmware_nsx/neutron/db/__init__.py b/vmware_nsx/neutron/db/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/vmware_nsx/neutron/db/migration/__init__.py b/vmware_nsx/neutron/db/migration/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/vmware_nsx/neutron/db/migration/alembic_migrations/__init__.py b/vmware_nsx/neutron/db/migration/alembic_migrations/__init__.py new file mode 100644 index 0000000000..c11ccd4914 --- /dev/null +++ b/vmware_nsx/neutron/db/migration/alembic_migrations/__init__.py @@ -0,0 +1,14 @@ +# Copyright 2015 VMware, Inc +# +# 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. +VERSION_TABLE = 'vmware_alembic_version' diff --git a/vmware_nsx/neutron/db/migration/alembic_migrations/env.py b/vmware_nsx/neutron/db/migration/alembic_migrations/env.py new file mode 100644 index 0000000000..34ead54e4d --- /dev/null +++ b/vmware_nsx/neutron/db/migration/alembic_migrations/env.py @@ -0,0 +1,124 @@ +# Copyright 2015 VMware, Inc +# +# 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 logging import config as logging_config + +from alembic import context +from oslo_config import cfg +from oslo_db.sqlalchemy import session +import sqlalchemy as sa +from sqlalchemy import event + +from neutron.db.migration.alembic_migrations import external +from neutron.db.migration.models import head # noqa +from neutron.db import model_base + +from vmware_nsx.neutron.db.migration import alembic_migrations + + +MYSQL_ENGINE = None + +# this is the Alembic Config object, which provides +# access to the values within the .ini file in use. +config = context.config +neutron_config = config.neutron_config + +# Interpret the config file for Python logging. +# This line sets up loggers basically. +logging_config.fileConfig(config.config_file_name) + +# set the target for 'autogenerate' support +target_metadata = model_base.BASEV2.metadata + + +def set_mysql_engine(): + try: + mysql_engine = neutron_config.command.mysql_engine + except cfg.NoSuchOptError: + mysql_engine = None + + global MYSQL_ENGINE + MYSQL_ENGINE = (mysql_engine or + model_base.BASEV2.__table_args__['mysql_engine']) + + +def include_object(object, name, type_, reflected, compare_to): + if (type_ == 'table' and + name in set(external.TABLES) - set(external.REPO_VMWARE_TABLES)): + return False + else: + return True + + +def run_migrations_offline(): + """Run migrations in 'offline' mode. + + This configures the context with either a URL + or an Engine. + + Calls to context.execute() here emit the given string to the + script output. + + """ + set_mysql_engine() + + kwargs = dict() + if neutron_config.database.connection: + kwargs['url'] = neutron_config.database.connection + else: + kwargs['dialect_name'] = neutron_config.database.engine + kwargs['include_object'] = include_object + kwargs['version_table'] = alembic_migrations.VERSION_TABLE + context.configure(**kwargs) + + with context.begin_transaction(): + context.run_migrations() + + +@event.listens_for(sa.Table, 'after_parent_attach') +def set_storage_engine(target, parent): + if MYSQL_ENGINE: + target.kwargs['mysql_engine'] = MYSQL_ENGINE + + +def run_migrations_online(): + """Run migrations in 'online' mode. + + In this scenario we need to create an Engine + and associate a connection with the context. + + """ + set_mysql_engine() + engine = session.create_engine(neutron_config.database.connection) + + connection = engine.connect() + context.configure( + connection=connection, + target_metadata=target_metadata, + include_object=include_object, + version_table=alembic_migrations.VERSION_TABLE + ) + + try: + with context.begin_transaction(): + context.run_migrations() + finally: + connection.close() + engine.dispose() + + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() diff --git a/vmware_nsx/neutron/db/migration/alembic_migrations/versions/HEADS b/vmware_nsx/neutron/db/migration/alembic_migrations/versions/HEADS index 731eec7e70..da6f10e20f 100644 --- a/vmware_nsx/neutron/db/migration/alembic_migrations/versions/HEADS +++ b/vmware_nsx/neutron/db/migration/alembic_migrations/versions/HEADS @@ -1,3 +1,2 @@ 393bf843b96 53a3254aa95e -kilo diff --git a/vmware_nsx/neutron/db/migration/alembic_migrations/versions/kilo_release.py b/vmware_nsx/neutron/db/migration/alembic_migrations/versions/kilo_release.py new file mode 100644 index 0000000000..8079c76283 --- /dev/null +++ b/vmware_nsx/neutron/db/migration/alembic_migrations/versions/kilo_release.py @@ -0,0 +1,31 @@ +# Copyright 2015 VMware, Inc +# +# 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. +# + +"""kilo + +Revision ID: kilo +Revises: None +Create Date: 2015-04-16 00:00:00.000000 + +""" + +# revision identifiers, used by Alembic. +revision = 'kilo' +down_revision = None + + +def upgrade(): + """A no-op migration for marking the Kilo release.""" + pass diff --git a/vmware_nsx/neutron/db/migration/alembic_migrations/versions/liberty/contract/393bf843b96_initial_liberty_no_op_contract_script.py b/vmware_nsx/neutron/db/migration/alembic_migrations/versions/liberty/contract/393bf843b96_initial_liberty_no_op_contract_script.py index 4d0709dbb2..ed2947f4e2 100644 --- a/vmware_nsx/neutron/db/migration/alembic_migrations/versions/liberty/contract/393bf843b96_initial_liberty_no_op_contract_script.py +++ b/vmware_nsx/neutron/db/migration/alembic_migrations/versions/liberty/contract/393bf843b96_initial_liberty_no_op_contract_script.py @@ -16,16 +16,18 @@ """Initial Liberty no-op contract script. Revision ID: 393bf843b96 -Revises: None +Revises: kilo Create Date: 2015-08-13 07:26:21.891165 """ +from neutron.db.migration import cli + + # revision identifiers, used by Alembic. revision = '393bf843b96' -down_revision = None -branch_labels = ('liberty_contract',) -depends_on = ('kilo',) +down_revision = 'kilo' +branch_labels = (cli.CONTRACT_BRANCH,) def upgrade(): diff --git a/vmware_nsx/neutron/db/migration/alembic_migrations/versions/liberty/expand/53a3254aa95e_initial_liberty_no_op_expand_script.py b/vmware_nsx/neutron/db/migration/alembic_migrations/versions/liberty/expand/53a3254aa95e_initial_liberty_no_op_expand_script.py index 8a942e6ba1..1017882823 100644 --- a/vmware_nsx/neutron/db/migration/alembic_migrations/versions/liberty/expand/53a3254aa95e_initial_liberty_no_op_expand_script.py +++ b/vmware_nsx/neutron/db/migration/alembic_migrations/versions/liberty/expand/53a3254aa95e_initial_liberty_no_op_expand_script.py @@ -16,16 +16,18 @@ """Initial Liberty no-op expand script. Revision ID: 53a3254aa95e -Revises: None +Revises: kilo Create Date: 2015-08-13 06:34:29.842396 """ +from neutron.db.migration import cli + + # revision identifiers, used by Alembic. revision = '53a3254aa95e' -down_revision = None -branch_labels = ('liberty_expand',) -depends_on = ('kilo',) +down_revision = 'kilo' +branch_labels = (cli.EXPAND_BRANCH,) def upgrade():