From eb1b5eaac61c8f8199d3d54c300c3730bdb4667a Mon Sep 17 00:00:00 2001 From: Adam Young Date: Mon, 10 Jun 2013 20:39:34 -0400 Subject: check for constraint before dropping Bug 1189633 Change-Id: Ieff57d17f224ec31523bfcce288818a08d64f98a --- keystone/common/sql/migration_helpers.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/keystone/common/sql/migration_helpers.py b/keystone/common/sql/migration_helpers.py index 6d735f5e..6823b6a7 100644 --- a/keystone/common/sql/migration_helpers.py +++ b/keystone/common/sql/migration_helpers.py @@ -23,12 +23,11 @@ import sqlalchemy # Constraints. SQLAlchemy does not yet attempt to determine the name # for the constraint, and instead attempts to deduce it from the column. # This fails on MySQL. -def get_fkey_constraint_name(table, column_name): - fkeys = [fk for fk in table.constraints +def get_constraints_names(table, column_name): + fkeys = [fk.name for fk in table.constraints if (column_name in fk.columns and isinstance(fk, sqlalchemy.ForeignKeyConstraint))] - constraint_name = fkeys[0].name - return constraint_name + return fkeys # remove_constraints and add_constraints both accept a list of dictionaries @@ -41,13 +40,14 @@ def get_fkey_constraint_name(table, column_name): # for the constraint. def remove_constraints(constraints): for constraint_def in constraints: - migrate.ForeignKeyConstraint( - columns=[getattr(constraint_def['table'].c, - constraint_def['fk_column'])], - refcolumns=[constraint_def['ref_column']], - name=(get_fkey_constraint_name - (constraint_def['table'], - constraint_def['fk_column']))).drop() + constraint_names = get_constraints_names(constraint_def['table'], + constraint_def['fk_column']) + for constraint_name in constraint_names: + migrate.ForeignKeyConstraint( + columns=[getattr(constraint_def['table'].c, + constraint_def['fk_column'])], + refcolumns=[constraint_def['ref_column']], + name=constraint_name).drop() def add_constraints(constraints): -- cgit