Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check schema when dropping constraints.
MySQL constraints are not named like others. Previous to this patch, we were looking up the names in MySQL's INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS table, but were neglecting to also check the Schema. The same table could be in multiple schemas; as an example a unit test run on the same machine as devstack in two different schemas but the same RDBMS. This patch uses a better approach. Sqlalchemy knows about the name of the constraint, it just doesn't use it in the drop statement by default. The Constraint objects from the main sqlalchemy package don't do drops, only those out of the migrate package. This patch finds the name of the constraint in the constraint bound to the table and passes it to the migrate ForeignKeyConstraint to use in the drop statement Bug 1186353 Change-Id: Ida2184021de9dd220a36507a8a625cf4210d17f7
- Loading branch information
Adam Young
committed
Jun 5, 2013
1 parent
99717a8
commit e97262d
Showing
2 changed files
with
75 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# vim: tabstop=4 shiftwidth=4 softtabstop=4 | ||
|
||
# Copyright 2013 OpenStack LLC | ||
# Copyright 2013 Red Hat, 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. | ||
import migrate | ||
import sqlalchemy | ||
|
||
|
||
# Different RDBMSs use different schemes for naming the Foreign Key | ||
# 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 | ||
if (column_name in fk.columns and | ||
isinstance(fk, sqlalchemy.ForeignKeyConstraint))] | ||
constraint_name = fkeys[0].name | ||
return constraint_name | ||
|
||
|
||
# remove_constraints and add_constraints both accept a list of dictionaries | ||
# that contain: | ||
# {'table': a sqlalchemy table. The constraint is added to to dropped from | ||
# this table. | ||
# 'fk_column': the name of a column on the above table, The constraint | ||
# is added to or dropped from this column | ||
# 'ref_column':a sqlalchemy column object. This is the reference column | ||
# 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() | ||
|
||
|
||
def add_constraints(constraints): | ||
for constraint_def in constraints: | ||
migrate.ForeignKeyConstraint( | ||
columns=[getattr(constraint_def['table'].c, | ||
constraint_def['fk_column'])], | ||
refcolumns=[constraint_def['ref_column']]).create() |