merge 3.16.x fix in 3.17.x
authorPierre-Yves David <pierre-yves.david@logilab.fr>
Thu, 28 Mar 2013 12:41:14 +0100
changeset 8822 064231ce93d5
parent 8819 d95a79c2687c (current diff)
parent 8821 c4aa23af0baa (diff)
child 8823 3f590510f72e
merge 3.16.x fix in 3.17.x
--- a/hooks/syncschema.py	Thu Mar 28 11:40:19 2013 +0100
+++ b/hooks/syncschema.py	Thu Mar 28 12:41:14 2013 +0100
@@ -507,6 +507,8 @@
 
     def revertprecommit_event(self):
         # revert changes on in memory schema
+        if getattr(self, 'rdefdef', None) is None:
+            return
         self.session.vreg.schema.del_relation_def(
             self.rdefdef.subject, self.rdefdef.name, self.rdefdef.object)
         # XXX revert changes on database
--- a/server/sources/native.py	Thu Mar 28 11:40:19 2013 +0100
+++ b/server/sources/native.py	Thu Mar 28 12:41:14 2013 +0100
@@ -756,13 +756,17 @@
                     mo = re.search('unique_cw_[^ ]+_idx', arg)
                     if mo is not None:
                         index_name = mo.group(0)
-                        elements = index_name.rstrip('_idx').split('_cw_')[1:]
+                        # right-chop '_idx' postfix
+                        # (garanteed to be there, see regexp above)
+                        elements = index_name[:-4].split('_cw_')[1:]
                         etype = elements[0]
                         rtypes = elements[1:]
                         raise UniqueTogetherError(etype, rtypes)
                     mo = re.search('columns (.*) are not unique', arg)
                     if mo is not None: # sqlite in use
-                        rtypes = [c.strip().lstrip('cw_') for c in mo.group(1).split(',')]
+                        # we left chop the 'cw_' prefix of attribute names
+                        rtypes = [c.strip()[3:]
+                                  for c in mo.group(1).split(',')]
                         etype = '???'
                         raise UniqueTogetherError(etype, rtypes)
             raise
--- a/server/test/data/schema.py	Thu Mar 28 11:40:19 2013 +0100
+++ b/server/test/data/schema.py	Thu Mar 28 12:41:14 2013 +0100
@@ -52,6 +52,7 @@
 
 
 class Societe(EntityType):
+    __unique_together__ = [('nom', 'type', 'cp')]
     __permissions__ = {
         'read': ('managers', 'users', 'guests'),
         'update': ('managers', 'owners', ERQLExpression('U login L, X nom L')),
--- a/server/test/unittest_repository.py	Thu Mar 28 11:40:19 2013 +0100
+++ b/server/test/unittest_repository.py	Thu Mar 28 12:41:14 2013 +0100
@@ -51,6 +51,15 @@
     and relation
     """
 
+    def test_uniquetogether(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_fill_schema(self):
         origshema = self.repo.schema
         try: