]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Catch DBDuplicateEntry instead of IntegrityError
authorIvan Kolodyazhny <e0ne@e0ne.info>
Mon, 4 Aug 2014 15:16:10 +0000 (18:16 +0300)
committerRoman Podoliaka <rpodolyaka@mirantis.com>
Thu, 14 Aug 2014 16:20:01 +0000 (16:20 +0000)
'iscsi_target_create_safe' method didn't catch DB exceptions
correct.

Need to catch both db_exc.DBError and db_exc.DBDuplicateEntry
due to current oslo.db limitations: it doesn't handle correct
DBDuplicateEntry with different SQLite versions

Related-Bug: #1187367

Change-Id: Ie412ecabd3cbcac301340c82b4275adb7a7206c3

cinder/db/sqlalchemy/api.py
cinder/tests/test_db_api.py

index a3244ea09bf883debbfe32e03a7ac52c0c4bad59..9a81acfafc3f19f0fb352e766a441d43ffa31589 100644 (file)
@@ -28,7 +28,6 @@ from oslo.config import cfg
 from oslo.db import exception as db_exc
 from oslo.db import options
 from oslo.db.sqlalchemy import session as db_session
-from sqlalchemy.exc import IntegrityError
 from sqlalchemy import or_
 from sqlalchemy.orm import joinedload, joinedload_all
 from sqlalchemy.orm import RelationshipProperty
@@ -450,12 +449,15 @@ def iscsi_target_create_safe(context, values):
     for (key, value) in values.iteritems():
         iscsi_target_ref[key] = value
     session = get_session()
-    with session.begin():
-        try:
-            iscsi_target_ref.save(session)
+
+    try:
+        with session.begin():
+            session.add(iscsi_target_ref)
             return iscsi_target_ref
-        except IntegrityError:
-            return None
+    # TODO(e0ne): Remove check on db_exc.DBError, when
+    #             Cinder will use oslo.db 0.4.0 or higher.
+    except (db_exc.DBError, db_exc.DBDuplicateEntry):
+        return None
 
 
 ###################
index 61a46c0f539d0542215091251ce68b1cecf7b9f4..35594a88b31e704f75396caf8478440aa12bb692 100644 (file)
@@ -1124,11 +1124,11 @@ class DBAPIIscsiTargetTestCase(BaseTest):
         self.assertEqual(db.iscsi_target_count_by_host(self.ctxt, 'fake_host'),
                          3)
 
-    @test.testtools.skip("bug 1187367")
     def test_integrity_error(self):
-        db.iscsi_target_create_safe(self.ctxt, self._get_base_values())
-        self.assertFalse(db.iscsi_target_create_safe(self.ctxt,
-                                                     self._get_base_values()))
+        values = self._get_base_values()
+        values['id'] = 1
+        db.iscsi_target_create_safe(self.ctxt, values)
+        self.assertFalse(db.iscsi_target_create_safe(self.ctxt, values))
 
 
 class DBAPIBackupTestCase(BaseTest):