summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Danjou <julien@danjou.info>2016-08-30 17:45:14 +0200
committerIhar Hrachyshka <ihrachys@redhat.com>2016-08-31 17:24:00 +0000
commit363e710295fad49f9c8996ae6efb9e1d9791628f (patch)
tree8aa4a65dbeeb86753157d7ac5474a42ba0e60899
parent87fb9cc84128fb5dceac483f892dba131c340d28 (diff)
downloadoslo-db-363e710295fad49f9c8996ae6efb9e1d9791628f.tar.gz
Fix DBReferenceError and DBNonExistentTable with new PyMySQL version
PyMySQL 0.7.7 changed the string format of the error raised, making oslo.db unable to catch the error: (pymysql.err.IntegrityError) (1452, u'23000Cannot add or update a child row: a foreign key constraint fails (`vaceciqnzs`.`resource_entity`, CONSTRAINT `foo_fkey` FOREIGN KEY (`foo_id`) REFERENCES `resource_foo` (`id`))') [SQL: u'INSERT INTO resource_entity (id, foo_id) VALUES (%(id)s, %(foo_id)s)'] [parameters: {'foo_id': 2, 'id': 1}] Change-Id: Ice7d7767842225951eaf48b71cedb2aabe5a84e4 (cherry picked from commit 1f3d509cab8c5d31f2ecd11561e1117d8c58316e)
-rw-r--r--oslo_db/sqlalchemy/exc_filters.py4
-rw-r--r--oslo_db/tests/sqlalchemy/test_exc_filters.py53
2 files changed, 17 insertions, 40 deletions
diff --git a/oslo_db/sqlalchemy/exc_filters.py b/oslo_db/sqlalchemy/exc_filters.py
index 051d1f8..9e5a2e5 100644
--- a/oslo_db/sqlalchemy/exc_filters.py
+++ b/oslo_db/sqlalchemy/exc_filters.py
@@ -197,7 +197,7 @@ def _sqlite_dupe_key_error(integrity_error, match, engine_name, is_disconnect):
"is (not present in|still referenced from) table "
"\"(?P<key_table>[^\"]+)\".")
@filters("mysql", sqla_exc.IntegrityError,
- r".* u?'Cannot (add|delete) or update a (child|parent) row: "
+ r".*Cannot (add|delete) or update a (child|parent) row: "
'a foreign key constraint fails \([`"].+[`"]\.[`"](?P<table>.+)[`"], '
'CONSTRAINT [`"](?P<constraint>.+)[`"] FOREIGN KEY '
'\([`"](?P<key>.+)[`"]\) REFERENCES [`"](?P<key_table>.+)[`"] ')
@@ -276,7 +276,7 @@ def _check_constraint_non_existing(
@filters("sqlite", sqla_exc.OperationalError,
r".* no such table: (?P<table>.+)")
@filters("mysql", sqla_exc.InternalError,
- r".*1051,.*\"Unknown table '(.+\.)?(?P<table>.+)'\"")
+ r".*1051,.*Unknown table '(.+\.)?(?P<table>.+)'\"")
@filters("postgresql", sqla_exc.ProgrammingError,
r".* table \"(?P<table>.+)\" does not exist")
def _check_table_non_existing(
diff --git a/oslo_db/tests/sqlalchemy/test_exc_filters.py b/oslo_db/tests/sqlalchemy/test_exc_filters.py
index 17153a7..e31f8dd 100644
--- a/oslo_db/tests/sqlalchemy/test_exc_filters.py
+++ b/oslo_db/tests/sqlalchemy/test_exc_filters.py
@@ -512,16 +512,11 @@ class TestReferenceErrorMySQL(TestReferenceErrorSQLite,
self.table_2.insert({'id': 1, 'foo_id': 2})
)
- self.assertInnerException(
- matched,
- "IntegrityError",
- (1452, "Cannot add or update a child row: a "
- "foreign key constraint fails (`{0}`.`resource_entity`, "
- "CONSTRAINT `foo_fkey` FOREIGN KEY (`foo_id`) REFERENCES "
- "`resource_foo` (`id`))".format(self.engine.url.database)),
- "INSERT INTO resource_entity (id, foo_id) VALUES (%s, %s)",
- (1, 2)
- )
+ # NOTE(jd) Cannot check precisely with assertInnerException since MySQL
+ # error are not the same depending on its version…
+ self.assertIsInstance(matched.inner_exception,
+ sqlalchemy.exc.IntegrityError)
+ self.assertEqual(matched.inner_exception.orig.args[0], 1452)
self.assertEqual("resource_entity", matched.table)
self.assertEqual("foo_fkey", matched.constraint)
self.assertEqual("foo_id", matched.key)
@@ -540,19 +535,11 @@ class TestReferenceErrorMySQL(TestReferenceErrorSQLite,
self.table_2.insert({'id': 1, 'foo_id': 2})
)
- self.assertInnerException(
- matched,
- "IntegrityError",
- (
- 1452,
- 'Cannot add or update a child row: a '
- 'foreign key constraint fails ("{0}"."resource_entity", '
- 'CONSTRAINT "foo_fkey" FOREIGN KEY ("foo_id") REFERENCES '
- '"resource_foo" ("id"))'.format(self.engine.url.database)
- ),
- "INSERT INTO resource_entity (id, foo_id) VALUES (%s, %s)",
- (1, 2)
- )
+ # NOTE(jd) Cannot check precisely with assertInnerException since MySQL
+ # error are not the same depending on its version…
+ self.assertIsInstance(matched.inner_exception,
+ sqlalchemy.exc.IntegrityError)
+ self.assertEqual(matched.inner_exception.orig.args[0], 1452)
self.assertEqual("resource_entity", matched.table)
self.assertEqual("foo_fkey", matched.constraint)
self.assertEqual("foo_id", matched.key)
@@ -567,21 +554,11 @@ class TestReferenceErrorMySQL(TestReferenceErrorSQLite,
self.engine.execute,
self.table_1.delete()
)
- self.assertInnerException(
- matched,
- "IntegrityError",
- (
- 1451,
- "Cannot delete or update a parent row: a foreign key "
- "constraint fails (`{0}`.`resource_entity`, "
- "constraint `foo_fkey` "
- "foreign key (`foo_id`) references "
- "`resource_foo` (`id`))".format(self.engine.url.database)
- ),
- "DELETE FROM resource_foo",
- (),
- )
-
+ # NOTE(jd) Cannot check precisely with assertInnerException since MySQL
+ # error are not the same depending on its version…
+ self.assertIsInstance(matched.inner_exception,
+ sqlalchemy.exc.IntegrityError)
+ self.assertEqual(1451, matched.inner_exception.orig.args[0])
self.assertEqual("resource_entity", matched.table)
self.assertEqual("foo_fkey", matched.constraint)
self.assertEqual("foo_id", matched.key)