summaryrefslogtreecommitdiffstats
path: root/keystone/common/sql/migration_helpers.py
diff options
context:
space:
mode:
authorAdam Young <ayoung@redhat.com>2013-06-04 10:26:03 -0400
committerAdam Young <ayoung@redhat.com>2013-06-04 21:10:19 -0400
commite97262d21d3fce44201732eb409d24caaa11e026 (patch)
treed3563bfb16f12f039e3d624d5a46f90ae56719af /keystone/common/sql/migration_helpers.py
parent99717a8fc8f5dc0f5cc310a8113ade5536657cfa (diff)
downloadkeystone-e97262d21d3fce44201732eb409d24caaa11e026.tar.gz
keystone-e97262d21d3fce44201732eb409d24caaa11e026.tar.xz
keystone-e97262d21d3fce44201732eb409d24caaa11e026.zip
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
Diffstat (limited to 'keystone/common/sql/migration_helpers.py')
-rw-r--r--keystone/common/sql/migration_helpers.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/keystone/common/sql/migration_helpers.py b/keystone/common/sql/migration_helpers.py
new file mode 100644
index 00000000..6d735f5e
--- /dev/null
+++ b/keystone/common/sql/migration_helpers.py
@@ -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()