Merge "Add UniqueConstraint for pool_id + hostname" into stable/liberty

This commit is contained in:
Jenkins 2016-01-11 15:44:25 +00:00 committed by Gerrit Code Review
commit 6c6a5e7c8b
5 changed files with 81 additions and 31 deletions

View File

@ -1,30 +0,0 @@
# Copyright 2015 Hewlett-Packard Development Company, L.P.
#
# Author: Kiall Mac Innes <kiall@hp.com>
#
# 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.
#
# This is a placeholder for Kilo backports.
# Do not use this number for new Liberty work. New Liberty work starts after
# all the placeholders.
#
# See https://blueprints.launchpad.net/nova/+spec/backportable-db-migrations
# http://lists.openstack.org/pipermail/openstack-dev/2013-March/006827.html
def upgrade(migrate_engine):
pass
def downgrade(migration_engine):
pass

View File

@ -0,0 +1,49 @@
# Copyright 2015 Hewlett-Packard Development Company, L.P.
#
# Author: Endre Karlson <endre.karlson@hp.com>
#
# 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.
import sys
from migrate.changeset.constraint import UniqueConstraint
from oslo_log import log as logging
from sqlalchemy.schema import MetaData, Table
from sqlalchemy import exc
LOG = logging.getLogger()
meta = MetaData()
CONSTRAINT_NAME = "unique_ns_name"
explanation = """
You need to manually remove duplicate entries from the database.
The error message was:
%s
"""
def upgrade(migrate_engine):
meta.bind = migrate_engine
pool_ns_records_table = Table('pool_ns_records', meta, autoload=True)
constraint = UniqueConstraint('pool_id', 'hostname',
name=CONSTRAINT_NAME,
table=pool_ns_records_table)
try:
constraint.create()
except exc.IntegrityError as e:
LOG.error(explanation, e)
# Use sys.exit so we dont blow up with a huge trace
sys.exit(1)

View File

@ -272,6 +272,7 @@ pool_ns_records = Table('pool_ns_records', metadata,
Column('hostname', String(255), nullable=False),
ForeignKeyConstraint(['pool_id'], ['pools.id'], ondelete='CASCADE'),
UniqueConstraint('pool_id', 'hostname', name='unique_ns_name'),
mysql_engine='INNODB',
mysql_charset='utf8')

View File

@ -62,7 +62,7 @@ class ApiV1ServersTest(ApiV1Test):
def test_create_server(self):
# Create a server
fixture = self.get_server_fixture(0)
fixture = self.get_server_fixture(1)
response = self.post('servers', data=fixture)

View File

@ -1971,6 +1971,36 @@ class StorageTestCase(object):
uuid = '203ca44f-c7e7-4337-9a02-0d735833e6aa'
self.storage.delete_pool(self.admin_context, uuid)
def test_create_pool_ns_record_duplicate(self):
# Create a pool
pool = self.create_pool(name='test1')
ns = objects.PoolNsRecord(priority=1, hostname="ns.example.io.")
self.storage.create_pool_ns_record(
self.admin_context, pool.id, ns)
ns2 = objects.PoolNsRecord(priority=2, hostname="ns.example.io.")
with testtools.ExpectedException(exceptions.DuplicatePoolNsRecord):
self.storage.create_pool_ns_record(
self.admin_context, pool.id, ns2)
def test_update_pool_ns_record_duplicate(self):
# Create a pool
pool = self.create_pool(name='test1')
ns1 = objects.PoolNsRecord(priority=1, hostname="ns1.example.io.")
self.storage.create_pool_ns_record(
self.admin_context, pool.id, ns1)
ns2 = objects.PoolNsRecord(priority=2, hostname="ns2.example.io.")
self.storage.create_pool_ns_record(
self.admin_context, pool.id, ns2)
with testtools.ExpectedException(exceptions.DuplicatePoolNsRecord):
ns2.hostname = ns1.hostname
self.storage.update_pool_ns_record(
self.admin_context, ns2)
def test_create_zone_transfer_request(self):
domain = self.create_domain()