[constraint] more robust unicity constraint failures reporting for end-users stable
authorAurelien Campeas <aurelien.campeas@logilab.fr>
Wed, 03 Jul 2013 14:33:27 +0200
branchstable
changeset 9130 0f1504a9fb51
parent 9129 6c4ae3a06619
child 9131 b3ad80aa645f
[constraint] more robust unicity constraint failures reporting for end-users Postgres or Sqlserver have limits on the index names (around resp. 64 and 128 characters). Because `logilab.database` encodes the `unique together` constraint rtypes in the index names, we sometimes get truncated index names, from which it is impossible to retrieve all rtypes. In the long run, the way such index are named should be changed. In the short term, we try to reduce the end-user confusion resulting from this design flaw: * in source/native, the regex filtering ``IntegrityError`` message does not impose an `_idx` suffix, which indeed may be absent (the result being an UI message that resembles a catastrophic failure), * also we avoid including a trailing " (double quote) from the error message * in entities/adapters, the well-named ``IUserFriendly`` adapter is made a bit smarter about how to handle missing rtypes. * the adapter also always produces a global message explaining the issue (and the fact that sometimes, the user is not shown all the relevant info) * i18n is updated Closes #2793789
entities/adapters.py
i18n/de.po
i18n/en.po
i18n/es.po
i18n/fr.po
server/sources/native.py
server/test/unittest_repository.py
--- a/entities/adapters.py	Wed Jul 03 14:16:21 2013 +0200
+++ b/entities/adapters.py	Wed Jul 03 14:33:27 2013 +0200
@@ -379,6 +379,7 @@
 class IUserFriendlyError(view.EntityAdapter):
     __regid__ = 'IUserFriendlyError'
     __abstract__ = True
+
     def __init__(self, *args, **kwargs):
         self.exc = kwargs.pop('exc')
         super(IUserFriendlyError, self).__init__(*args, **kwargs)
@@ -386,11 +387,27 @@
 
 class IUserFriendlyUniqueTogether(IUserFriendlyError):
     __select__ = match_exception(UniqueTogetherError)
+
     def raise_user_exception(self):
         etype, rtypes = self.exc.args
-        msg = self._cw._('violates unique_together constraints (%s)') % (
-            ', '.join([self._cw._(rtype) for rtype in rtypes]))
-        raise ValidationError(self.entity.eid, dict((col, msg) for col in rtypes))
+        # Because of index name size limits (e.g: postgres around 64,
+        # sqlserver around 128), we cannot be sure of what we got,
+        # especially for the rtypes part.
+        # Hence we will try to validate them, and handle invalid ones
+        # in the most user-friendly manner ...
+        _ = self._cw._
+        schema = self.entity._cw.vreg.schema
+        rtypes_msg = {}
+        for rtype in rtypes:
+            if rtype in schema:
+                rtypes_msg[rtype] = _('%s is part of violated unicity constraint') % rtype
+        globalmsg = _('some relations %sviolate a unicity constraint')
+        if len(rtypes) != len(rtypes_msg): # we got mangled/missing rtypes
+            globalmsg = globalmsg % _('(not all shown here) ')
+        else:
+            globalmsg = globalmsg % ''
+        rtypes_msg['unicity constraint'] = globalmsg
+        raise ValidationError(self.entity.eid, rtypes_msg)
 
 # deprecated ###################################################################
 
--- a/i18n/de.po	Wed Jul 03 14:16:21 2013 +0200
+++ b/i18n/de.po	Wed Jul 03 14:33:27 2013 +0200
@@ -114,6 +114,10 @@
 msgstr "%s Fehlerbericht"
 
 #, python-format
+msgid "%s is part of violated unicity constraint"
+msgstr ""
+
+#, python-format
 msgid "%s not estimated"
 msgstr "%s unbekannt(e)"
 
@@ -145,6 +149,9 @@
 msgid "(UNEXISTANT EID)"
 msgstr "(EID nicht gefunden)"
 
+msgid "(not all shown here) "
+msgstr ""
+
 #, python-format
 msgid "(suppressed) entity #%d"
 msgstr ""
@@ -3861,6 +3868,10 @@
 "Eine oder mehrere frühere Transaktion(en) betreffen die Tntität. Machen Sie "
 "sie zuerst rückgängig."
 
+#, python-format
+msgid "some relations %sviolate a unicity constraint"
+msgstr ""
+
 msgid "sorry, the server is unable to handle this query"
 msgstr "Der Server kann diese Anfrage leider nicht bearbeiten."
 
--- a/i18n/en.po	Wed Jul 03 14:16:21 2013 +0200
+++ b/i18n/en.po	Wed Jul 03 14:33:27 2013 +0200
@@ -106,6 +106,10 @@
 msgstr ""
 
 #, python-format
+msgid "%s is part of violated unicity constraint"
+msgstr ""
+
+#, python-format
 msgid "%s not estimated"
 msgstr ""
 
@@ -137,6 +141,9 @@
 msgid "(UNEXISTANT EID)"
 msgstr ""
 
+msgid "(not all shown here) "
+msgstr ""
+
 #, python-format
 msgid "(suppressed) entity #%d"
 msgstr ""
@@ -3766,6 +3773,10 @@
 msgid "some later transaction(s) touch entity, undo them first"
 msgstr ""
 
+#, python-format
+msgid "some relations %sviolate a unicity constraint"
+msgstr ""
+
 msgid "sorry, the server is unable to handle this query"
 msgstr ""
 
--- a/i18n/es.po	Wed Jul 03 14:16:21 2013 +0200
+++ b/i18n/es.po	Wed Jul 03 14:33:27 2013 +0200
@@ -115,6 +115,10 @@
 msgstr "%s reporte de errores"
 
 #, python-format
+msgid "%s is part of violated unicity constraint"
+msgstr ""
+
+#, python-format
 msgid "%s not estimated"
 msgstr "%s no estimado(s)"
 
@@ -146,6 +150,9 @@
 msgid "(UNEXISTANT EID)"
 msgstr "(EID INEXISTENTE"
 
+msgid "(not all shown here) "
+msgstr ""
+
 #, python-format
 msgid "(suppressed) entity #%d"
 msgstr ""
@@ -3909,6 +3916,10 @@
 msgstr ""
 "Las transacciones más recientes modificaron esta entidad, anúlelas primero"
 
+#, python-format
+msgid "some relations %sviolate a unicity constraint"
+msgstr ""
+
 msgid "sorry, the server is unable to handle this query"
 msgstr "Lo sentimos, el servidor no puede manejar esta consulta"
 
--- a/i18n/fr.po	Wed Jul 03 14:16:21 2013 +0200
+++ b/i18n/fr.po	Wed Jul 03 14:33:27 2013 +0200
@@ -115,6 +115,10 @@
 msgstr "%s rapport d'erreur"
 
 #, python-format
+msgid "%s is part of violated unicity constraint"
+msgstr "%s appartient à une contrainte d'unicité transgressée"
+
+#, python-format
 msgid "%s not estimated"
 msgstr "%s non estimé(s)"
 
@@ -148,6 +152,9 @@
 msgid "(UNEXISTANT EID)"
 msgstr "(EID INTROUVABLE)"
 
+msgid "(not all shown here) "
+msgstr "(toutes ne sont pas montrées)"
+
 #, python-format
 msgid "(suppressed) entity #%d"
 msgstr "entité #%d (supprimée)"
@@ -3922,6 +3929,10 @@
 msgstr ""
 "des transactions plus récentes modifient cette entité, annulez les d'abord"
 
+#, python-format
+msgid "some relations %sviolate a unicity constraint"
+msgstr "certaines relations %stransgressent une contrainte d'unicité"
+
 msgid "sorry, the server is unable to handle this query"
 msgstr "désolé, le serveur ne peut traiter cette requête"
 
--- a/server/sources/native.py	Wed Jul 03 14:16:21 2013 +0200
+++ b/server/sources/native.py	Wed Jul 03 14:33:27 2013 +0200
@@ -757,15 +757,15 @@
             if ex.__class__.__name__ == 'IntegrityError':
                 # need string comparison because of various backends
                 for arg in ex.args:
-                    mo = re.search('unique_cw_[^ ]+_idx', arg)
+                    # postgres and sqlserver
+                    mo = re.search('"unique_cw_[^ ]+"', arg)
                     if mo is not None:
-                        index_name = mo.group(0)
-                        # right-chop '_idx' postfix
-                        # (garanteed to be there, see regexp above)
-                        elements = index_name[:-4].split('_cw_')[1:]
+                        index_name = mo.group(0)[1:-1] # eat the surrounding " pair
+                        elements = index_name.split('_cw_')[1:]
                         etype = elements[0]
                         rtypes = elements[1:]
                         raise UniqueTogetherError(etype, rtypes)
+                    # sqlite
                     mo = re.search('columns (.*) are not unique', arg)
                     if mo is not None: # sqlite in use
                         # we left chop the 'cw_' prefix of attribute names
--- a/server/test/unittest_repository.py	Wed Jul 03 14:16:21 2013 +0200
+++ b/server/test/unittest_repository.py	Wed Jul 03 14:33:27 2013 +0200
@@ -52,16 +52,18 @@
     and relation
     """
 
-    def test_uniquetogether(self):
+    def test_unique_together_constraint(self):
         self.execute('INSERT Societe S: S nom "Logilab", S type "SSLL", S cp "75013"')
         with self.assertRaises(ValidationError) as wraperr:
             self.execute('INSERT Societe S: S nom "Logilab", S type "SSLL", S cp "75013"')
-        self.assertEqual({'nom': u'violates unique_together constraints (cp, nom, type)',
-                          'cp': u'violates unique_together constraints (cp, nom, type)',
-                          'type': u'violates unique_together constraints (cp, nom, type)'},
-                     wraperr.exception.args[1])
+        self.assertEqual(
+            {'cp': u'cp is part of violated unicity constraint',
+             'nom': u'nom is part of violated unicity constraint',
+             'type': u'type is part of violated unicity constraint',
+             'unicity constraint': u'some relations violate a unicity constraint'},
+            wraperr.exception.args[1])
 
-    def test_unique_together(self):
+    def test_unique_together_schema(self):
         person = self.repo.schema.eschema('Personne')
         self.assertEqual(len(person._unique_together), 1)
         self.assertItemsEqual(person._unique_together[0],