-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Return 409 on creating/importing same name keypair
* Removed the DB look up on creating and importing a keypair. * Added unique constraint on user_id and name columns of key_pairs table. Fixes LP: #1086333. Change-Id: I2fd4697f9269d2be13bd977e65ba4ca4a27b9ac6
- Loading branch information
Rohan Rhishikesh Kanade
committed
Apr 1, 2013
1 parent
a831e9d
commit 0ae598b
Showing
7 changed files
with
175 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
nova/db/sqlalchemy/migrate_repo/versions/173_add_unique_constraint_to_key_pairs.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Copyright 2012 OpenStack LLC. | ||
# 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 Index, MetaData, Table | ||
|
||
from nova.db.sqlalchemy import utils | ||
from nova.openstack.common import log as logging | ||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
OLD_IDX_NAME = 'key_pair_user_id_name_idx' | ||
UC_NAME = 'key_pairs_uniq_name_and_user_id' | ||
TABLE_NAME = 'key_pairs' | ||
UC_COLUMNS = ['user_id', 'name', 'deleted'] | ||
|
||
|
||
def upgrade(migrate_engine): | ||
meta = MetaData(bind=migrate_engine) | ||
key_pairs = Table(TABLE_NAME, meta, autoload=True) | ||
utils.drop_old_duplicate_entries_from_table(migrate_engine, | ||
TABLE_NAME, True, | ||
*UC_COLUMNS) | ||
old_idx = None | ||
#Drop old index because the new UniqueConstraint can be used instead. | ||
for index in key_pairs.indexes: | ||
if index.name == OLD_IDX_NAME: | ||
index.drop() | ||
old_idx = index | ||
|
||
#index.drop() in SQLAlchemy-migrate will issue a DROP INDEX statement to | ||
#the DB but WILL NOT update the table metadata to remove the `Index` | ||
#object. This can cause subsequent calls like drop or create constraint | ||
#on that table to fail.The solution is to update the table metadata to | ||
#reflect the now dropped column. | ||
if old_idx: | ||
key_pairs.indexes.remove(old_idx) | ||
uc = UniqueConstraint(*(UC_COLUMNS), table=key_pairs, name=UC_NAME) | ||
uc.create() | ||
|
||
|
||
def downgrade(migrate_engine): | ||
utils.drop_unique_constraint(migrate_engine, TABLE_NAME, UC_NAME, | ||
*(UC_COLUMNS)) | ||
meta = MetaData(bind=migrate_engine) | ||
key_pairs = Table(TABLE_NAME, meta, autoload=True) | ||
Index(OLD_IDX_NAME, key_pairs.c.user_id, | ||
key_pairs.c.name).create(migrate_engine) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters