From d5602a87efeab67b677cd76c0ff27045868bc40b Mon Sep 17 00:00:00 2001 From: Yuriy Zveryanskyy Date: Thu, 29 Aug 2013 17:14:59 +0300 Subject: [PATCH] Add missing unique constraint There is missing unique constraint in chassis table. This patch adds missing UC in separate migration. Change-Id: If4640d09a19bbd1548375fa6817e9d926c805c7c --- .../versions/011_add_chassis_uc.py | 30 +++++++++++++++++++ ironic/db/sqlalchemy/models.py | 3 ++ ironic/tests/db/sqlalchemy/test_migrations.py | 8 +++++ 3 files changed, 41 insertions(+) create mode 100644 ironic/db/sqlalchemy/migrate_repo/versions/011_add_chassis_uc.py diff --git a/ironic/db/sqlalchemy/migrate_repo/versions/011_add_chassis_uc.py b/ironic/db/sqlalchemy/migrate_repo/versions/011_add_chassis_uc.py new file mode 100644 index 0000000000..9d5d7c5423 --- /dev/null +++ b/ironic/db/sqlalchemy/migrate_repo/versions/011_add_chassis_uc.py @@ -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.') diff --git a/ironic/db/sqlalchemy/models.py b/ironic/db/sqlalchemy/models.py index 745a351ff2..2ccbf4f1ee 100644 --- a/ironic/db/sqlalchemy/models.py +++ b/ironic/db/sqlalchemy/models.py @@ -84,6 +84,9 @@ class Chassis(Base): """Represents a hardware chassis.""" __tablename__ = 'chassis' + __table_args__ = ( + schema.UniqueConstraint('uuid', name='uniq_chassis0uuid'), + ) id = Column(Integer, primary_key=True) uuid = Column(String(36)) extra = Column(JSONEncodedDict) diff --git a/ironic/tests/db/sqlalchemy/test_migrations.py b/ironic/tests/db/sqlalchemy/test_migrations.py index 0facee45da..de79920082 100644 --- a/ironic/tests/db/sqlalchemy/test_migrations.py +++ b/ironic/tests/db/sqlalchemy/test_migrations.py @@ -680,3 +680,11 @@ class TestMigrations(BaseMigrationTestCase, WalkVersionsMixin): self.assertEqual(f_key['referred_table'], 'chassis') self.assertEqual(f_key['referred_columns'], ['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'})