Commit 3314d816 authored by Aurelien Campeas's avatar Aurelien Campeas
Browse files

[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

--HG--
branch : stable
parent 6c4ae3a06619
......@@ -379,6 +379,7 @@ from cubicweb import UniqueTogetherError
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 IUserFriendlyError(view.EntityAdapter):
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 ###################################################################
......
......@@ -113,6 +113,10 @@ msgstr ""
msgid "%s error report"
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 @@ msgstr ""
msgid "(UNEXISTANT EID)"
msgstr "(EID nicht gefunden)"
msgid "(not all shown here) "
msgstr ""
#, python-format
msgid "(suppressed) entity #%d"
msgstr ""
......@@ -3861,6 +3868,10 @@ msgstr ""
"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."
......
......@@ -105,6 +105,10 @@ msgstr ""
msgid "%s error report"
msgstr ""
#, python-format
msgid "%s is part of violated unicity constraint"
msgstr ""
#, python-format
msgid "%s not estimated"
msgstr ""
......@@ -137,6 +141,9 @@ msgstr ""
msgid "(UNEXISTANT EID)"
msgstr ""
msgid "(not all shown here) "
msgstr ""
#, python-format
msgid "(suppressed) entity #%d"
msgstr ""
......@@ -3766,6 +3773,10 @@ msgstr ""
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 ""
......
......@@ -114,6 +114,10 @@ msgstr "%s podría ser mantenido"
msgid "%s error report"
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 @@ msgstr "la acción '%s' requiere una opción 'linkattr'"
msgid "(UNEXISTANT EID)"
msgstr "(EID INEXISTENTE"
msgid "(not all shown here) "
msgstr ""
#, python-format
msgid "(suppressed) entity #%d"
msgstr ""
......@@ -3909,6 +3916,10 @@ msgid "some later transaction(s) touch entity, undo them first"
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"
......
......@@ -114,6 +114,10 @@ msgstr "%s pourrait être supporté"
msgid "%s error report"
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 @@ msgstr "l'action '%s' nécessite une option 'linkattr'"
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 @@ msgid "some later transaction(s) touch entity, undo them first"
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"
......
......@@ -757,15 +757,15 @@ class NativeSQLSource(SQLAdapterMixIn, AbstractSource):
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
......
......@@ -52,16 +52,18 @@ class RepositoryTC(CubicWebTC):
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])
def test_unique_together(self):
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_schema(self):
person = self.repo.schema.eschema('Personne')
self.assertEqual(len(person._unique_together), 1)
self.assertItemsEqual(person._unique_together[0],
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment