[hooks/syncschema] don't set NOT NULL on inlined relation columns
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 24 Nov 2015 16:00:06 +0100
changeset 10917 edea9c324396
parent 10916 8e504786df3c
child 10918 6b1445cfccdf
[hooks/syncschema] don't set NOT NULL on inlined relation columns The NOT NULL constraint was being added on inlined relation cardinality change while it shouldn't. We don't want this, because this forbids to add an entity without the relation set at once (it may be added later in the transaction). This patch fix synchronization hook to stop doing this. Closes #6211101
hooks/syncschema.py
server/test/unittest_migractions.py
--- a/hooks/syncschema.py	Thu Sep 24 12:18:00 2015 +0200
+++ b/hooks/syncschema.py	Tue Nov 24 16:00:06 2015 +0100
@@ -1,4 +1,4 @@
-# copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
 #
 # This file is part of CubicWeb.
@@ -648,8 +648,7 @@
         if 'indexed' in self.values:
             syssource.update_rdef_indexed(cnx, rdef)
             self.indexed_changed = True
-        if 'cardinality' in self.values and (rdef.rtype.final or
-                                             rdef.rtype.inlined) \
+        if 'cardinality' in self.values and rdef.rtype.final \
               and self.values['cardinality'][0] != self.oldvalues['cardinality'][0]:
             syssource.update_rdef_null_allowed(self.cnx, rdef)
             self.null_allowed_changed = True
--- a/server/test/unittest_migractions.py	Thu Sep 24 12:18:00 2015 +0200
+++ b/server/test/unittest_migractions.py	Tue Nov 24 16:00:06 2015 +0100
@@ -22,6 +22,7 @@
 from contextlib import contextmanager
 
 from logilab.common.testlib import unittest_main, Tags, tag
+from logilab.common import tempattr
 
 from yams.constraints import UniqueConstraint
 
@@ -720,6 +721,18 @@
             self.assertEqual(tel, 1.0)
             self.assertIsInstance(tel, float)
 
+    def test_drop_required_inlined_relation(self):
+        with self.mh() as (cnx, mh):
+            bob = mh.cmd_create_entity('Personne', nom=u'bob')
+            note = mh.cmd_create_entity('Note', ecrit_par=bob)
+            mh.commit()
+            rdef = mh.fs_schema.rschema('ecrit_par').rdefs[('Note', 'Personne')]
+            with tempattr(rdef, 'cardinality', '1*'):
+                mh.sync_schema_props_perms('ecrit_par', syncperms=False)
+            mh.cmd_drop_relation_type('ecrit_par')
+            self.assertNotIn('%secrit_par' % SQL_PREFIX,
+                             self.table_schema(mh, '%sPersonne' % SQL_PREFIX))
+
 
 class MigrationCommandsComputedTC(MigrationTC):
     """ Unit tests for computed relations and attributes