From f2a7b0829a0ef79567b0b4efcc935000a36a84bc Mon Sep 17 00:00:00 2001 From: Ofer Ben-Yacov Date: Wed, 4 Jan 2017 15:28:20 +0200 Subject: [PATCH] Alembic scripts --- .../migration/alembic_migrations/__init__.py | 0 .../db/migration/alembic_migrations/env.py | 99 +++++++++++++++++++ .../alembic_migrations/script.py.mako | 36 +++++++ .../alembic_migrations/versions/__init__.py | 0 .../versions/ocata/__init__.py | 0 .../ocata/contract/45194b1d3492_first_rev.py | 63 ++++++++++++ .../versions/ocata/contract/__init__.py | 0 wan_qos/db/models/wan_tc.py | 7 +- 8 files changed, 200 insertions(+), 5 deletions(-) create mode 100644 wan_qos/db/migration/alembic_migrations/__init__.py create mode 100644 wan_qos/db/migration/alembic_migrations/env.py create mode 100644 wan_qos/db/migration/alembic_migrations/script.py.mako create mode 100644 wan_qos/db/migration/alembic_migrations/versions/__init__.py create mode 100644 wan_qos/db/migration/alembic_migrations/versions/ocata/__init__.py create mode 100644 wan_qos/db/migration/alembic_migrations/versions/ocata/contract/45194b1d3492_first_rev.py create mode 100644 wan_qos/db/migration/alembic_migrations/versions/ocata/contract/__init__.py diff --git a/wan_qos/db/migration/alembic_migrations/__init__.py b/wan_qos/db/migration/alembic_migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/wan_qos/db/migration/alembic_migrations/env.py b/wan_qos/db/migration/alembic_migrations/env.py new file mode 100644 index 0000000..7ec51e4 --- /dev/null +++ b/wan_qos/db/migration/alembic_migrations/env.py @@ -0,0 +1,99 @@ +# Copyright 2015 OpenStack Foundation +# +# 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_lib.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.db.migration.alembic_migrations import external +from neutron.db.migration.models import head # noqa + +MYSQL_ENGINE = None +WANTC_VERSION_TABLE = 'wantc_alembic_version' +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 include_object(object, name, type_, reflected, compare_to): + if type_ == 'table' and name in external.TABLES: + return False + else: + return True + + +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['include_object'] = include_object + kwargs['version_table'] = WANTC_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, + include_object=include_object, + version_table=WANTC_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/wan_qos/db/migration/alembic_migrations/script.py.mako b/wan_qos/db/migration/alembic_migrations/script.py.mako new file mode 100644 index 0000000..9e0b2ce --- /dev/null +++ b/wan_qos/db/migration/alembic_migrations/script.py.mako @@ -0,0 +1,36 @@ +# Copyright ${create_date.year} +# +# 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"} diff --git a/wan_qos/db/migration/alembic_migrations/versions/__init__.py b/wan_qos/db/migration/alembic_migrations/versions/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/wan_qos/db/migration/alembic_migrations/versions/ocata/__init__.py b/wan_qos/db/migration/alembic_migrations/versions/ocata/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/wan_qos/db/migration/alembic_migrations/versions/ocata/contract/45194b1d3492_first_rev.py b/wan_qos/db/migration/alembic_migrations/versions/ocata/contract/45194b1d3492_first_rev.py new file mode 100644 index 0000000..15b4d29 --- /dev/null +++ b/wan_qos/db/migration/alembic_migrations/versions/ocata/contract/45194b1d3492_first_rev.py @@ -0,0 +1,63 @@ +# Copyright 2017 +# +# 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. +# + +"""first rev + +Revision ID: 45194b1d3492 +Revises: None +Create Date: 2017-01-04 13:46:13.433909 + +""" + +# revision identifiers, used by Alembic. +revision = '45194b1d3492' +down_revision = None + +from alembic import op +import sqlalchemy as sa + + +def upgrade(): + op.create_table('wan_tc_class', + sa.Column('id', sa.String(length=36), nullable=False), + sa.Column('networks_id', sa.String(length=36), + nullable=False), + sa.Column('class_ext_id', sa.Integer()), + sa.Column('min_rate', + sa.String(length=15), nullable=False), + sa.Column('min_rate', sa.String(length=15)), + sa.PrimaryKeyConstraint('id') + ) + + op.create_foreign_key( + 'fk_wan_tc_class_networks', + 'wan_tc_class', 'networks', + ['networks_id'], ['id'], + ) + + op.create_table('wan_tc_selector', + sa.Column('id', sa.String(length=36), nullable=False), + sa.Column('class_id', sa.String(length=36), + nullable=False), + sa.Column('protocol', sa.String(length=15)), + sa.Column('match', sa.String(length=15)), + sa.PrimaryKeyConstraint('id') + ) + + op.create_foreign_key( + 'fk_wan_tc__selector_class', + 'wan_tc_selector', 'wan_tc_class', + ['class_id'], ['id'], + ) diff --git a/wan_qos/db/migration/alembic_migrations/versions/ocata/contract/__init__.py b/wan_qos/db/migration/alembic_migrations/versions/ocata/contract/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/wan_qos/db/models/wan_tc.py b/wan_qos/db/models/wan_tc.py index 8393707..422116f 100644 --- a/wan_qos/db/models/wan_tc.py +++ b/wan_qos/db/models/wan_tc.py @@ -28,9 +28,7 @@ class WanTcClass(model_base.BASEV2, network_id = sa.Column(sa.String(36), sa.ForeignKey('networks.id', ondelete='CASCADE'), - nullable=False, - unique=True, - primary_key=True) + nullable=False) min_rate = sa.Column(sa.String(15), nullable=False) max_rate = sa.Column(sa.String(15)) @@ -41,8 +39,7 @@ class WanTcSelector(model_base.BASEV2, class_id = sa.Column(sa.String(36), sa.ForeignKey('wan_tc_class.id', ondelete='CASCADE'), - nullable=False, - primary_key=True) + nullable=False) protocol = sa.Column(sa.String(15)) match = sa.Column(sa.String(15))