From: Alan Meadows <alan.meadows@gmail.com>
Date: Thu, 26 Dec 2013 19:12:37 +0000 (-0800)
Subject: Bugfix missing foreign key removal for mysql
X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=8328fc46d783f4ec9286eededafa91afae89cba0;p=openstack-build%2Fcinder-build.git

Bugfix missing foreign key removal for mysql

Downgrading the cinder schema fails when running 018_add_qos_specs.py
under MySQL. The upgrade path of this schema patch adds the foreign
key volume_types_ibfk_1 on table volume_types, and the downgrade does
not correspondingly remove it before attempting to drop the
qos_specs_id column.  This update removes the foreign key when
the engine is mysql prior to dropping the column.

Change-Id: Ibd3b35ad3b0bd41ad04ab7aeeb28c3ba7e5d255d
Closes-Bug: #1264360
---

diff --git a/cinder/db/sqlalchemy/migrate_repo/versions/018_add_qos_specs.py b/cinder/db/sqlalchemy/migrate_repo/versions/018_add_qos_specs.py
index 76868bf6c..c95d25e37 100644
--- a/cinder/db/sqlalchemy/migrate_repo/versions/018_add_qos_specs.py
+++ b/cinder/db/sqlalchemy/migrate_repo/versions/018_add_qos_specs.py
@@ -18,6 +18,7 @@
 
 from sqlalchemy import Boolean, Column, DateTime
 from sqlalchemy import ForeignKey, MetaData, String, Table
+from migrate import ForeignKeyConstraint
 
 from cinder.openstack.common import log as logging
 
@@ -69,17 +70,34 @@ def downgrade(migrate_engine):
 
     qos_specs = Table('quality_of_service_specs', meta, autoload=True)
 
-    try:
-        qos_specs.drop()
-
-    except Exception:
-        LOG.error(_("Dropping quality_of_service_specs table failed."))
-        raise
+    if migrate_engine.name == 'mysql':
+        # NOTE(alanmeadows): MySQL Cannot drop column qos_specs_id
+        # until the foreign key volumes_types_ibfk_1 is removed.  We
+        # remove the foreign key first, and then we drop the column.
+        table = Table('volume_types', meta, autoload=True)
+        ref_table = Table('volume_types', meta, autoload=True)
+        params = {'columns': [table.c['qos_specs_id']],
+                  'refcolumns': [ref_table.c['id']],
+                  'name': 'volume_types_ibfk_1'}
+
+        try:
+            fkey = ForeignKeyConstraint(**params)
+            fkey.drop()
+        except Exception:
+            LOG.error(_("Dropping foreign key volume_types_ibfk_1 failed"))
 
     volume_types = Table('volume_types', meta, autoload=True)
     qos_specs_id = Column('qos_specs_id', String(36))
+
     try:
         volume_types.drop_column(qos_specs_id)
     except Exception:
         LOG.error(_("Dropping qos_specs_id column failed."))
         raise
+
+    try:
+        qos_specs.drop()
+
+    except Exception:
+        LOG.error(_("Dropping quality_of_service_specs table failed."))
+        raise