diff options
| author | Yuriy Zveryanskyy <yzveryanskyy@mirantis.com> | 2013-06-17 13:48:45 +0300 |
|---|---|---|
| committer | Yuriy Zveryanskyy <yzveryanskyy@mirantis.com> | 2013-06-17 13:48:45 +0300 |
| commit | 7fdba82bad3c4e550bda4db03ed9d1ab7ab62934 (patch) | |
| tree | e45ed5de89b0a542f350018168056d625c9ba562 /nova/db | |
| parent | 328b347cd058f1c87d7e32a18d9decc0ba517266 (diff) | |
Add unique constraints to Cell.
In documentation: "name: Name of the current cell. This must be
unique for each cell"
Added unique constraint 'uniq_cell_name0deleted' ('name','deleted')
to Cell model and migrate sripts.
Added new exception `CellsExists`. Updated cell_create() in db.api.
Tests updated respectively.
blueprint db-enforce-unique-keys
Change-Id: Ib76e167089cb4756e4438613b49013ebf0874afe
Diffstat (limited to 'nova/db')
| -rw-r--r-- | nova/db/sqlalchemy/api.py | 5 | ||||
| -rw-r--r-- | nova/db/sqlalchemy/migrate_repo/versions/189_add_cells_uc.py | 40 | ||||
| -rw-r--r-- | nova/db/sqlalchemy/models.py | 4 |
3 files changed, 48 insertions, 1 deletions
diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 664425441..55ef03006 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -4048,7 +4048,10 @@ def instance_type_extra_specs_update_or_create(context, flavor_id, specs): def cell_create(context, values): cell = models.Cell() cell.update(values) - cell.save() + try: + cell.save() + except db_exc.DBDuplicateEntry: + raise exception.CellExists(name=values['name']) return cell diff --git a/nova/db/sqlalchemy/migrate_repo/versions/189_add_cells_uc.py b/nova/db/sqlalchemy/migrate_repo/versions/189_add_cells_uc.py new file mode 100644 index 000000000..d0606e9f9 --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/189_add_cells_uc.py @@ -0,0 +1,40 @@ +# Copyright 2013 Mirantis 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. +# +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +from migrate.changeset import UniqueConstraint +from sqlalchemy import MetaData, Table + +from nova.db.sqlalchemy import utils + + +UC_NAME = 'uniq_cell_name0deleted' +COLUMNS = ('name', 'deleted') +TABLE_NAME = 'cells' + + +def upgrade(migrate_engine): + meta = MetaData(bind=migrate_engine) + t = Table(TABLE_NAME, meta, autoload=True) + + utils.drop_old_duplicate_entries_from_table(migrate_engine, TABLE_NAME, + True, *COLUMNS) + uc = UniqueConstraint(*COLUMNS, table=t, name=UC_NAME) + uc.create() + + +def downgrade(migrate_engine): + utils.drop_unique_constraint(migrate_engine, TABLE_NAME, UC_NAME, *COLUMNS) diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index 815041638..9b6d849d3 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -859,6 +859,10 @@ class Cell(BASE, NovaBase): of entries with is_parent=True or False """ __tablename__ = 'cells' + __table_args__ = (schema.UniqueConstraint( + "name", "deleted", name="uniq_cell_name0deleted" + ), + ) id = Column(Integer, primary_key=True) # Name here is the 'short name' of a cell. For instance: 'child1' name = Column(String(255)) |
