Add missing unique constraint

There is missing unique constraint in chassis table.
This patch adds missing UC in separate migration.

Change-Id: If4640d09a19bbd1548375fa6817e9d926c805c7c
This commit is contained in:
Yuriy Zveryanskyy 2013-08-29 17:14:59 +03:00
parent b3577bb7d1
commit d5602a87ef
3 changed files with 41 additions and 0 deletions

View File

@ -0,0 +1,30 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# -*- encoding: utf-8 -*-
#
# 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 migrate.changeset import UniqueConstraint
from sqlalchemy import MetaData, Table
def upgrade(migrate_engine):
meta = MetaData(bind=migrate_engine)
t = Table('chassis', meta, autoload=True)
# NOTE: new name convention for UC
uc = UniqueConstraint('uuid', table=t, name='uniq_chassis0uuid')
uc.create()
def downgrade(migrate_engine):
raise NotImplementedError('Downgrade from version 011 is unsupported.')

View File

@ -84,6 +84,9 @@ class Chassis(Base):
"""Represents a hardware chassis.""" """Represents a hardware chassis."""
__tablename__ = 'chassis' __tablename__ = 'chassis'
__table_args__ = (
schema.UniqueConstraint('uuid', name='uniq_chassis0uuid'),
)
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
uuid = Column(String(36)) uuid = Column(String(36))
extra = Column(JSONEncodedDict) extra = Column(JSONEncodedDict)

View File

@ -680,3 +680,11 @@ class TestMigrations(BaseMigrationTestCase, WalkVersionsMixin):
self.assertEqual(f_key['referred_table'], 'chassis') self.assertEqual(f_key['referred_table'], 'chassis')
self.assertEqual(f_key['referred_columns'], ['id']) self.assertEqual(f_key['referred_columns'], ['id'])
self.assertEqual(f_key['constrained_columns'], ['chassis_id']) self.assertEqual(f_key['constrained_columns'], ['chassis_id'])
def _check_011(self, engine, data):
chassis = db_utils.get_table(engine, 'chassis')
chassis_data = {'uuid': 'uuu-111-222', 'extra': 'extra1'}
chassis.insert().values(chassis_data).execute()
self.assertRaises(sqlalchemy.exc.IntegrityError,
chassis.insert().execute,
{'uuid': 'uuu-111-222', 'extra': 'extra2'})