]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Add unique constraints in IPAvailabilityRange
authorrossella <rsblendido@suse.com>
Tue, 23 Sep 2014 16:09:09 +0000 (16:09 +0000)
committerrossella <rsblendido@suse.com>
Mon, 13 Oct 2014 19:41:37 +0000 (21:41 +0200)
first_ip, allocation_pool_id and last_ip, allocation_pool_id
should be unique in the table.
These constraints are essential to detect concurrent modifications
of the IpAvailabilityRange table if the SELECT ... FOR UPDATE
lock is removed

Change-Id: Iaf2288c0b6bf27e93c03691073d7f505ef24fdd3
Closes-bug: #1373015

neutron/db/migration/alembic_migrations/versions/44621190bc02_add_uniqueconstraint_ipavailability_ranges.py [new file with mode: 0644]
neutron/db/migration/alembic_migrations/versions/HEAD
neutron/db/models_v2.py

diff --git a/neutron/db/migration/alembic_migrations/versions/44621190bc02_add_uniqueconstraint_ipavailability_ranges.py b/neutron/db/migration/alembic_migrations/versions/44621190bc02_add_uniqueconstraint_ipavailability_ranges.py
new file mode 100644 (file)
index 0000000..66b57c1
--- /dev/null
@@ -0,0 +1,61 @@
+# Copyright 2014 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.
+#
+
+"""add_uniqueconstraint_ipavailability_ranges
+
+Revision ID: 44621190bc02
+Revises: juno
+Create Date: 2014-09-23 15:14:15.051921
+
+"""
+
+# revision identifiers, used by Alembic.
+revision = '44621190bc02'
+down_revision = 'juno'
+
+from alembic import op
+
+
+TABLE_NAME = 'ipavailabilityranges'
+UC_1_NAME = 'uniq_ipavailabilityranges0first_ip0allocation_pool_id'
+UC_2_NAME = 'uniq_ipavailabilityranges0last_ip0allocation_pool_id'
+
+
+def upgrade():
+    op.create_unique_constraint(
+        name=UC_1_NAME,
+        source=TABLE_NAME,
+        local_cols=['first_ip', 'allocation_pool_id']
+    )
+
+    op.create_unique_constraint(
+        name=UC_2_NAME,
+        source=TABLE_NAME,
+        local_cols=['last_ip', 'allocation_pool_id']
+    )
+
+
+def downgrade():
+    op.drop_constraint(
+        name=UC_1_NAME,
+        table_name=TABLE_NAME,
+        type_='unique'
+    )
+
+    op.drop_constraint(
+        name=UC_2_NAME,
+        table_name=TABLE_NAME,
+        type_='unique'
+    )
index 7a30775e93c3e196758deeb138f41a6b1fdea7f1..8c20ac433be0bab21486aeb4e49977ea37aad2a6 100644 (file)
@@ -1 +1 @@
-juno
+44621190bc02
index 53efc66926d366701cf45bfc8459e50048294188..d27c4750a3d60d1280a98b9c390da47c2cbcff7d 100644 (file)
@@ -63,6 +63,13 @@ class IPAvailabilityRange(model_base.BASEV2):
                                    primary_key=True)
     first_ip = sa.Column(sa.String(64), nullable=False, primary_key=True)
     last_ip = sa.Column(sa.String(64), nullable=False, primary_key=True)
+    __table_args__ = (
+        sa.UniqueConstraint(
+            first_ip, allocation_pool_id,
+            name='uniq_ipavailabilityranges0first_ip0allocation_pool_id'),
+        sa.UniqueConstraint(
+            last_ip, allocation_pool_id,
+            name='uniq_ipavailabilityranges0last_ip0allocation_pool_id'))
 
     def __repr__(self):
         return "%s - %s" % (self.first_ip, self.last_ip)