Added alembic DB migration framework

This patch adds alembic DB migration framework required by
neutron-dynamic-routing repository for defining new data
models to realize BGP dynamic routing functionality in neutron.

Below changes are done as part of this patch-set:
 - Added new alembic migration framework.
 - PEP8 changes for alembic migration check.

Change-Id: I395ca9541bceda40bbc0c0870e58c18231ba44e9
Implements: blueprint bgp-spinout
Partial-Bug: #1560003
This commit is contained in:
vikram.choudhary 2016-04-28 17:10:36 +05:30
parent 4ba80f3f1c
commit 61a933f105
15 changed files with 258 additions and 1 deletions

View File

View File

@ -0,0 +1,2 @@
For details refer to:
http://docs.openstack.org/developer/neutron-dynamic-routing/alembic_migration.html

View File

@ -0,0 +1,88 @@
# Copyright 2016 Huawei Technologies India Pvt Limited.
#
# 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 neutron.db import model_base
from oslo_config import cfg
from oslo_db.sqlalchemy import session
import sqlalchemy as sa
from sqlalchemy import event
from neutron_dynamic_routing.db.migration.models import head # noqa
MYSQL_ENGINE = None
DR_VERSION_TABLE = 'alembic_version_dr'
config = context.config
neutron_config = config.neutron_config
logging_config.fileConfig(config.config_file_name)
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 run_migrations_offline():
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['version_table'] = DR_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():
set_mysql_engine()
engine = session.create_engine(neutron_config.database.connection)
connection = engine.connect()
context.configure(
connection=connection,
target_metadata=target_metadata,
version_table=DR_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()

View File

@ -0,0 +1,36 @@
# Copyright 2016 Huawei Technologies India Pvt Limited.
#
# 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.
#
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision}
Create Date: ${create_date}
"""
# revision identifiers, used by Alembic.
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
% if branch_labels:
branch_labels = ${repr(branch_labels)}
%endif
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
def upgrade():
${upgrades if upgrades else "pass"}

View File

@ -0,0 +1 @@
61cc795e43e8

View File

@ -0,0 +1 @@
f399fa0f5f25

View File

@ -0,0 +1,37 @@
# Copyright 2016 Huawei Technologies India Pvt Limited.
#
# 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.
#
"""initial
Revision ID: 61cc795e43e8
Revises: start_neutron_dynamic_routing
Create Date: 2016-05-03 08:30:18.421995
"""
from neutron.db import migration
from neutron.db.migration import cli
# revision identifiers, used by Alembic.
revision = '61cc795e43e8'
down_revision = 'start_neutron_dynamic_routing'
branch_labels = (cli.CONTRACT_BRANCH,)
# milestone identifier, used by neutron-db-manage
neutron_milestone = [migration.NEWTON]
def upgrade():
pass

View File

@ -0,0 +1,37 @@
# Copyright 2016 Huawei Technologies India Pvt Limited.
#
# 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.
#
"""initial
Revision ID: f399fa0f5f25
Revises: None
Create Date: 2016-05-03 08:30:18.421995
"""
from neutron.db import migration
from neutron.db.migration import cli
# revision identifiers, used by Alembic.
revision = 'f399fa0f5f25'
down_revision = 'start_neutron_dynamic_routing'
branch_labels = (cli.EXPAND_BRANCH,)
# milestone identifier, used by neutron-db-manage
neutron_milestone = [migration.NEWTON]
def upgrade():
pass

View File

@ -0,0 +1,30 @@
# Copyright 2016 Huawei Technologies India Pvt Limited.
#
# 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.
#
"""start neutron-dynamic-routing chain
Revision ID: start_neutron_dynamic_routing
Revises: None
Create Date: 2016-04-26 18:42:08.262632
"""
# revision identifiers, used by Alembic.
revision = 'start_neutron_dynamic_routing'
down_revision = None
def upgrade():
pass

View File

@ -0,0 +1,19 @@
# Copyright 2016 Huawei Technologies India Pvt Limited.
#
# 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 neutron.db import model_base
def get_metadata():
return model_base.BASEV2.metadata

View File

@ -24,6 +24,10 @@ packages =
setup-hooks =
pbr.hooks.setup_hook
[entry_points]
neutron.db.alembic_migrations =
neutron-dynamic-routing = neutron_dynamic_routing.db.migration:alembic_migrations
[build_sphinx]
all_files = 1
build-dir = doc/build

View File

@ -24,7 +24,9 @@ install_command = {toxinidir}/tools/tox_install.sh unconstrained {opts} {package
commands = sphinx-build -a -E -W -d releasenotes/build/doctrees -b html releasenotes/source releasenotes/build/html
[testenv:pep8]
commands = flake8
commands =
flake8
neutron-db-manage --subproject neutron-dynamic-routing --database-connection sqlite:// check_migration
[testenv:i18n]
commands = python ./tools/check_i18n.py ./neutron-dynamic-routing ./tools/i18n_cfg.py