Adds Not Null constraint to KeyPair name

Since the API enforces that name is not null, the database schema and
model are now updated to enforce this.

Change-Id: I1088cc79f4998d3b6609891adbb0479381297ec4
Related-Bug: #1403544
Closes-Bug: #1189981
This commit is contained in:
Kent Wang 2015-03-10 08:44:32 -07:00
parent 37a6c601f3
commit dd52617232
5 changed files with 70 additions and 6 deletions

View File

@ -0,0 +1,53 @@
# Copyright 2015 Intel Corporation
# 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.
from migrate.changeset import UniqueConstraint
from sqlalchemy import MetaData, Table
def upgrade(migrate_engine):
"""Function enforces non-null value for keypairs name field."""
meta = MetaData(bind=migrate_engine)
key_pairs = Table('key_pairs', meta, autoload=True)
# Note: Since we are altering name field, this constraint on name needs to
# first be dropped before we can alter name. We then re-create the same
# constraint. It was first added in 216_havana.py so no need to remove
# constraint on downgrade.
UniqueConstraint('user_id', 'name', 'deleted', table=key_pairs,
name='uniq_key_pairs0user_id0name0deleted').drop()
key_pairs.c.name.alter(nullable=False)
UniqueConstraint('user_id', 'name', 'deleted', table=key_pairs,
name='uniq_key_pairs0user_id0name0deleted').create()
def downgrade(migrate_engine):
"""Function allows null value for keypairs name field."""
meta = MetaData(bind=migrate_engine)
key_pairs = Table('key_pairs', meta, autoload=True)
# Note: Since we are altering name field, this constraint on name needs to
# first be dropped before we can alter name. We then re-create the same
# constraint. It was first added in 216_havana.py so no need to remove
# constraint on downgrade.
UniqueConstraint('user_id', 'name', 'deleted', table=key_pairs,
name='uniq_key_pairs0user_id0name0deleted').drop()
key_pairs.c.name.alter(nullable=True)
UniqueConstraint('user_id', 'name', 'deleted', table=key_pairs,
name='uniq_key_pairs0user_id0name0deleted').create()

View File

@ -759,7 +759,7 @@ class KeyPair(BASE, NovaBase):
)
id = Column(Integer, primary_key=True, nullable=False)
name = Column(String(255))
name = Column(String(255), nullable=False)
user_id = Column(String(255))

View File

@ -29,11 +29,12 @@ class KeyPair(base.NovaPersistentObject, base.NovaObject,
# Version 1.0: Initial version
# Version 1.1: String attributes updated to support unicode
# Version 1.2: Added keypair type
VERSION = '1.2'
# Version 1.3: Name field is non-null
VERSION = '1.3'
fields = {
'id': fields.IntegerField(),
'name': fields.StringField(nullable=True),
'name': fields.StringField(nullable=False),
'user_id': fields.StringField(nullable=True),
'fingerprint': fields.StringField(nullable=True),
'public_key': fields.StringField(nullable=True),
@ -81,7 +82,8 @@ class KeyPairList(base.ObjectListBase, base.NovaObject):
# Version 1.0: Initial version
# KeyPair <= version 1.1
# Version 1.1: KeyPair <= version 1.2
VERSION = '1.1'
# Version 1.2: KeyPair <= version 1.3
VERSION = '1.2'
fields = {
'objects': fields.ListOfObjectsField('KeyPair'),
@ -90,6 +92,7 @@ class KeyPairList(base.ObjectListBase, base.NovaObject):
'1.0': '1.1',
# NOTE(danms): KeyPair was at 1.1 before we added this
'1.1': '1.2',
'1.2': '1.3',
}
@base.remotable_classmethod

View File

@ -871,6 +871,14 @@ class NovaMigrationsCheckers(test_migrations.ModelsMigrationsSync,
self.assertIn('uniq_compute_nodes0host0hypervisor_hostname',
constraint_names)
def _check_280(self, engine, data):
key_pairs = oslodbutils.get_table(engine, 'key_pairs')
self.assertFalse(key_pairs.c.name.nullable)
def _post_downgrade_280(self, engine):
key_pairs = oslodbutils.get_table(engine, 'key_pairs')
self.assertTrue(key_pairs.c.name.nullable)
class TestNovaMigrationsSQLite(NovaMigrationsCheckers,
test_base.DbTestCase,

View File

@ -1216,8 +1216,8 @@ object_data = {
'InstanceNUMATopology': '1.1-b6fab68a3f0f1dfab4c98a236d29839a',
'InstancePCIRequest': '1.1-e082d174f4643e5756ba098c47c1510f',
'InstancePCIRequests': '1.1-4825b599f000538991fdc9972a92c2c6',
'KeyPair': '1.2-b476e480f85d307711c89d0aa78b3a3f',
'KeyPairList': '1.1-152dc1efcc46014cc10656a0d0ac5bb0',
'KeyPair': '1.3-2d7c9ccade5532f7cd185110a9367e6a',
'KeyPairList': '1.2-1f84680a0a533374db8a9fac7bd7bbc7',
'Migration': '1.1-dc2db9e6e625bd3444a5a114438b298d',
'MigrationList': '1.1-8c5f678edc72a592d591a13b35e54353',
'MyObj': '1.6-fce707f79d6fee00f0ebbac98816a380',