]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Further cleanup of reservations index
authorVishvananda Ishaya <vishvananda@gmail.com>
Mon, 28 Jul 2014 17:39:29 +0000 (10:39 -0700)
committerVishvananda Ishaya <vishvananda@gmail.com>
Mon, 28 Jul 2014 17:40:34 +0000 (10:40 -0700)
It was discovered that the duplicate index checking does not work
on db2, so this fixes it.

Change-Id: I1a80b9c1fb6cfe916a2110de3151a09fa3618e75
Partial-bug: 1348720

cinder/db/sqlalchemy/migrate_repo/versions/023_add_expire_reservations_index.py

index eb9edabcdfc0d7d018cde2d8882f66b59d50f6e8..996ebbdfcba2bb05a20b3d8cfebe4f6d3f4288d9 100644 (file)
@@ -13,7 +13,6 @@
 #    under the License.
 
 from sqlalchemy import Index, MetaData, Table
-from sqlalchemy.exc import OperationalError
 
 from cinder.openstack.common.gettextutils import _
 from cinder.openstack.common import log as logging
@@ -21,21 +20,29 @@ from cinder.openstack.common import log as logging
 LOG = logging.getLogger(__name__)
 
 
+def _get_deleted_expire_index(table):
+    members = sorted(['deleted', 'expire'])
+    for idx in table.indexes:
+        if sorted(idx.columns.keys()) == members:
+            return idx
+
+
 def upgrade(migrate_engine):
     meta = MetaData()
     meta.bind = migrate_engine
 
     reservations = Table('reservations', meta, autoload=True)
+    if _get_deleted_expire_index(reservations):
+        LOG.info(_('Skipped adding reservations_deleted_expire_idx '
+                   'because an equivalent index already exists.'))
+        return
 
     # Based on expire_reservations query
     # from: cinder/db/sqlalchemy/api.py
     index = Index('reservations_deleted_expire_idx',
                   reservations.c.deleted, reservations.c.expire)
-    try:
-        index.create(migrate_engine)
-    except OperationalError:
-        LOG.info(_('Skipped adding reservations_deleted_expire_idx '
-                   'because an equivalent index already exists.'))
+
+    index.create(migrate_engine)
 
 
 def downgrade(migrate_engine):
@@ -44,10 +51,9 @@ def downgrade(migrate_engine):
 
     reservations = Table('reservations', meta, autoload=True)
 
-    index = Index('reservations_deleted_expire_idx',
-                  reservations.c.deleted, reservations.c.expire)
-    try:
+    index = _get_deleted_expire_index(reservations)
+    if index:
         index.drop(migrate_engine)
-    except OperationalError:
+    else:
         LOG.info(_('Skipped removing reservations_deleted_expire_idx '
                    'because index does not exist.'))