Skip to content

Commit

Permalink
Merge "Add unique constraint to instance_uuid"
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenkins authored and openstack-gerrit committed Jun 13, 2014
2 parents 8b61c44 + 332f562 commit 3a2582a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
@@ -0,0 +1,39 @@
# Copyright 2014 Red Hat, Inc.
# All Rights Reserved.
#
# 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 unique constraint to instance_uuid
Revision ID: 3bea56f25597
Revises: 31baaf680d2b
Create Date: 2014-06-05 11:45:07.046670
"""

# revision identifiers, used by Alembic.
revision = '3bea56f25597'
down_revision = '31baaf680d2b'

from alembic import op


def upgrade():
op.create_unique_constraint("uniq_nodes0instance_uuid", "nodes",
["instance_uuid"])
op.drop_index('node_instance_uuid', 'nodes')


def downgrade():
op.drop_constraint("uniq_nodes0instance_uuid", "nodes")
op.create_index('node_instance_uuid', 'nodes', ['instance_uuid'])
5 changes: 3 additions & 2 deletions ironic/db/sqlalchemy/models.py
Expand Up @@ -24,7 +24,7 @@
import six.moves.urllib.parse as urlparse

from sqlalchemy import Boolean, Column, DateTime
from sqlalchemy import ForeignKey, Integer, Index
from sqlalchemy import ForeignKey, Integer
from sqlalchemy import schema, String, Text
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.types import TypeDecorator, TEXT
Expand Down Expand Up @@ -135,7 +135,8 @@ class Node(Base):
__tablename__ = 'nodes'
__table_args__ = (
schema.UniqueConstraint('uuid', name='uniq_nodes0uuid'),
Index('node_instance_uuid', 'instance_uuid'))
schema.UniqueConstraint('instance_uuid',
name='uniq_nodes0instance_uuid'))
id = Column(Integer, primary_key=True)
uuid = Column(String(36))
# NOTE(deva): we store instance_uuid directly on the node so that we can
Expand Down
12 changes: 12 additions & 0 deletions ironic/tests/db/sqlalchemy/test_migrations.py
Expand Up @@ -50,6 +50,7 @@
import sqlalchemy
import sqlalchemy.exc

from ironic.common import utils
from ironic.db.sqlalchemy import migration
from ironic.openstack.common.db.sqlalchemy import utils as db_utils
from ironic.openstack.common import lockutils
Expand Down Expand Up @@ -521,3 +522,14 @@ def _check_31baaf680d2b(self, engine, data):
self.assertIn('instance_info', col_names)
self.assertIsInstance(nodes.c.instance_info.type,
sqlalchemy.types.TEXT)

def _check_3bea56f25597(self, engine, data):
nodes = db_utils.get_table(engine, 'nodes')
instance_uuid = 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'
data = {'driver': 'fake',
'uuid': utils.generate_uuid(),
'instance_uuid': instance_uuid}
nodes.insert().values(data).execute()
data['uuid'] = utils.generate_uuid()
self.assertRaises(sqlalchemy.exc.IntegrityError,
nodes.insert().execute, data)

0 comments on commit 3a2582a

Please sign in to comment.