diff --git a/networking_bgpvpn/neutron/db/bgpvpn_db.py b/networking_bgpvpn/neutron/db/bgpvpn_db.py index e51a6353..b557dd12 100644 --- a/networking_bgpvpn/neutron/db/bgpvpn_db.py +++ b/networking_bgpvpn/neutron/db/bgpvpn_db.py @@ -34,16 +34,21 @@ from networking_bgpvpn.neutron.services.common import utils LOG = log.getLogger(__name__) +class HasTenantNotNullable(object): + """Non-nullable Tenant mixin.""" + tenant_id = sa.Column(sa.String(255), index=True, nullable=False) + + class BGPVPNNetAssociation(model_base.BASEV2, models_v2.HasId, - models_v2.HasTenant): + HasTenantNotNullable): """Represents the association between a bgpvpn and a network.""" __tablename__ = 'bgpvpn_network_associations' bgpvpn_id = sa.Column(sa.String(36), - sa.ForeignKey('bgpvpns.id'), + sa.ForeignKey('bgpvpns.id', ondelete='CASCADE'), nullable=False) network_id = sa.Column(sa.String(36), - sa.ForeignKey('networks.id'), + sa.ForeignKey('networks.id', ondelete='CASCADE'), nullable=False) sa.UniqueConstraint(bgpvpn_id, network_id) network = orm.relationship("Network", @@ -53,15 +58,15 @@ class BGPVPNNetAssociation(model_base.BASEV2, models_v2.HasId, class BGPVPNRouterAssociation(model_base.BASEV2, models_v2.HasId, - models_v2.HasTenant): + HasTenantNotNullable): """Represents the association between a bgpvpn and a router.""" __tablename__ = 'bgpvpn_router_associations' bgpvpn_id = sa.Column(sa.String(36), - sa.ForeignKey('bgpvpns.id'), + sa.ForeignKey('bgpvpns.id', ondelete='CASCADE'), nullable=False) router_id = sa.Column(sa.String(36), - sa.ForeignKey('routers.id'), + sa.ForeignKey('routers.id', ondelete='CASCADE'), nullable=False) sa.UniqueConstraint(bgpvpn_id, router_id) router = orm.relationship("Router", @@ -77,9 +82,9 @@ class BGPVPN(model_base.BASEV2, models_v2.HasId, models_v2.HasTenant): name="bgpvpn_type"), nullable=False) route_targets = sa.Column(sa.String(255), nullable=False) - import_targets = sa.Column(sa.String(255), nullable=False) - export_targets = sa.Column(sa.String(255), nullable=False) - route_distinguishers = sa.Column(sa.String(255), nullable=False) + import_targets = sa.Column(sa.String(255), nullable=True) + export_targets = sa.Column(sa.String(255), nullable=True) + route_distinguishers = sa.Column(sa.String(255), nullable=True) network_associations = orm.relationship("BGPVPNNetAssociation", backref="bgpvpn", lazy='joined', diff --git a/networking_bgpvpn/neutron/db/head.py b/networking_bgpvpn/neutron/db/head.py new file mode 100644 index 00000000..4a2799f5 --- /dev/null +++ b/networking_bgpvpn/neutron/db/head.py @@ -0,0 +1,20 @@ +# 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.migration.models import head + +# pylint: disable=unused-import +import networking_bgpvpn.neutron.db.bgpvpn_db # noqa + + +def get_metadata(): + return head.model_base.BASEV2.metadata diff --git a/networking_bgpvpn/neutron/db/migration/alembic_migrations/versions/HEADS b/networking_bgpvpn/neutron/db/migration/alembic_migrations/versions/CONTRACT_HEAD similarity index 52% rename from networking_bgpvpn/neutron/db/migration/alembic_migrations/versions/HEADS rename to networking_bgpvpn/neutron/db/migration/alembic_migrations/versions/CONTRACT_HEAD index 6188906a..e927677f 100644 --- a/networking_bgpvpn/neutron/db/migration/alembic_migrations/versions/HEADS +++ b/networking_bgpvpn/neutron/db/migration/alembic_migrations/versions/CONTRACT_HEAD @@ -1,2 +1 @@ 180baa4183e0 -3600132c6147 \ No newline at end of file diff --git a/networking_bgpvpn/neutron/db/migration/alembic_migrations/versions/EXPAND_HEAD b/networking_bgpvpn/neutron/db/migration/alembic_migrations/versions/EXPAND_HEAD new file mode 100644 index 00000000..1d3fbe30 --- /dev/null +++ b/networking_bgpvpn/neutron/db/migration/alembic_migrations/versions/EXPAND_HEAD @@ -0,0 +1 @@ +0ab4049986b8 diff --git a/networking_bgpvpn/neutron/db/migration/alembic_migrations/versions/__init__.py b/networking_bgpvpn/neutron/db/migration/alembic_migrations/versions/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/networking_bgpvpn/neutron/db/migration/alembic_migrations/versions/liberty/__init__.py b/networking_bgpvpn/neutron/db/migration/alembic_migrations/versions/liberty/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/networking_bgpvpn/neutron/db/migration/alembic_migrations/versions/liberty/contract/__init__.py b/networking_bgpvpn/neutron/db/migration/alembic_migrations/versions/liberty/contract/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/networking_bgpvpn/neutron/db/migration/alembic_migrations/versions/liberty/expand/17d9fd4fddee_initial.py b/networking_bgpvpn/neutron/db/migration/alembic_migrations/versions/liberty/expand/17d9fd4fddee_initial.py index 0dffc199..8f16f46e 100644 --- a/networking_bgpvpn/neutron/db/migration/alembic_migrations/versions/liberty/expand/17d9fd4fddee_initial.py +++ b/networking_bgpvpn/neutron/db/migration/alembic_migrations/versions/liberty/expand/17d9fd4fddee_initial.py @@ -32,7 +32,7 @@ branch_labels = (cli.EXPAND_BRANCH,) vpn_types = sa.Enum("l2", "l3", name="vpn_types") -def upgrade(active_plugins=None, options=None): +def upgrade(): op.create_table( 'bgpvpns', sa.Column('name', sa.String(255), nullable=True), diff --git a/networking_bgpvpn/neutron/db/migration/alembic_migrations/versions/liberty/expand/__init__.py b/networking_bgpvpn/neutron/db/migration/alembic_migrations/versions/liberty/expand/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/networking_bgpvpn/neutron/db/migration/alembic_migrations/versions/newton/expand/0ab4049986b8_add_indexes_to_tenant_id.py b/networking_bgpvpn/neutron/db/migration/alembic_migrations/versions/newton/expand/0ab4049986b8_add_indexes_to_tenant_id.py new file mode 100644 index 00000000..5986b7e8 --- /dev/null +++ b/networking_bgpvpn/neutron/db/migration/alembic_migrations/versions/newton/expand/0ab4049986b8_add_indexes_to_tenant_id.py @@ -0,0 +1,35 @@ +# 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. +# + +"""add indexes to tenant_id + +Revision ID: 0ab4049986b8 +Create Date: 2016年07月22日 14:19:04.888614 + +""" + +from alembic import op + +# revision identifiers, used by Alembic. +revision = '0ab4049986b8' +down_revision = '3600132c6147' + + +def upgrade(): + for table in [ + 'bgpvpns', + 'bgpvpn_network_associations', + 'bgpvpn_router_associations', + ]: + op.create_index(op.f('ix_%s_tenant_id' % table), + table, ['tenant_id'], unique=False) diff --git a/networking_bgpvpn/tests/unit/db/test_migrations.py b/networking_bgpvpn/tests/unit/db/test_migrations.py new file mode 100644 index 00000000..322e95e1 --- /dev/null +++ b/networking_bgpvpn/tests/unit/db/test_migrations.py @@ -0,0 +1,73 @@ +# 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 oslo_config import cfg + +from neutron.db.migration.alembic_migrations import external +from neutron.db.migration import cli as migration +from neutron.tests.functional.db import test_migrations +from neutron.tests.unit import testlib_api + +from networking_bgpvpn.neutron.db import head + + +# Tables from other repos that we depend on but do not manage. + +BAGPIPE_TABLES = { + 'ml2_route_target_allocations', +} + +ODL_TABLES = { + 'odl_alembic_version', + 'opendaylightjournal', + 'opendaylight_maintenance', + +} + +# EXTERNAL_TABLES should contain all names of tables that are not related to +# current repo. +EXTERNAL_TABLES = set(external.TABLES) | BAGPIPE_TABLES | ODL_TABLES +VERSION_TABLE = 'alembic_version_bgpvpn' + + +class _TestModelsMigrationsBGPVPN(test_migrations._TestModelsMigrations): + + def db_sync(self, engine): + cfg.CONF.set_override('connection', engine.url, group='database') + for conf in migration.get_alembic_configs(): + self.alembic_config = conf + self.alembic_config.neutron_config = cfg.CONF + migration.do_alembic_command(conf, 'upgrade', 'heads') + + def get_metadata(self): + return head.get_metadata() + + def include_object(self, object_, name, type_, reflected, compare_to): + if type_ == 'table' and (name.startswith('alembic') or + name == VERSION_TABLE or + name in EXTERNAL_TABLES): + return False + if type_ == 'index' and reflected and name.startswith("idx_autoinc_"): + return False + return True + + +class TestModelsMigrationsMysql(testlib_api.MySQLTestCaseMixin, + _TestModelsMigrationsBGPVPN, + testlib_api.SqlTestCaseLight): + pass + + +class TestModelsMigrationsPostgresql(testlib_api.PostgreSQLTestCaseMixin, + _TestModelsMigrationsBGPVPN, + testlib_api.SqlTestCaseLight): + pass diff --git a/test-requirements.txt b/test-requirements.txt index 35ce1602..3bd633c0 100755 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -10,6 +10,8 @@ discover python-subunit>=0.0.18 sphinx!=1.2.0,!=1.3b1,<1.3,>=1.1.2 oslosphinx!=3.4.0,>=2.5.0 # Apache-2.0 +psycopg2>=2.5 # LGPL/ZPL +PyMySQL>=0.6.2 # MIT License WebOb>=1.2.3 WebTest>=2.0 oslotest>=1.10.0 # Apache-2.0 diff --git a/tox.ini b/tox.ini index 1242c668..004d079a 100755 --- a/tox.ini +++ b/tox.ini @@ -29,6 +29,9 @@ commands = pylint --rcfile=.pylintrc --output-format=colorized doc/source/samples neutron-db-manage --subproject networking-bgpvpn --database-connection sqlite:// check_migration +[testenv:py27] +setenv = OS_FAIL_ON_MISSING_DEPS=1 + [testenv:venv] # TODO(tmorin): remove once infra supports constraints for this target install_command = {toxinidir}/tools/tox_install.sh unconstrained {opts} {packages}

AltStyle によって変換されたページ (->オリジナル) /