[migration] fix rename_entity_type to avoid to loose some relations on the way stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 20 Jul 2010 16:45:11 +0200
branchstable
changeset 5999 eaf8219f8b7d
parent 5998 6c148645075d
child 6000 98ca82aae3a1
[migration] fix rename_entity_type to avoid to loose some relations on the way
server/checkintegrity.py
server/migractions.py
--- a/server/checkintegrity.py	Tue Jul 20 14:09:58 2010 +0200
+++ b/server/checkintegrity.py	Tue Jul 20 16:45:11 2010 +0200
@@ -17,8 +17,8 @@
 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
 """Check integrity of a CubicWeb repository. Hum actually only the system database
 is checked.
+"""
 
-"""
 from __future__ import with_statement
 
 __docformat__ = "restructuredtext en"
--- a/server/migractions.py	Tue Jul 20 14:09:58 2010 +0200
+++ b/server/migractions.py	Tue Jul 20 16:45:11 2010 +0200
@@ -865,23 +865,30 @@
                               if (rschema.final or rschema.inlined)
                               and not rschema in PURE_VIRTUAL_RTYPES])
             self.sqlexec('INSERT INTO %s%s(%s) SELECT %s FROM %s%s' % (
-                SQL_PREFIX, newname, attrs, attrs, SQL_PREFIX, oldname))
+                SQL_PREFIX, newname, attrs, attrs, SQL_PREFIX, oldname),
+                         ask_confirm=False)
             # old entity type has not been added to the schema, can't gather it
             new = schema.eschema(newname)
             oldeid = self.rqlexec('CWEType ET WHERE ET name %(on)s', {'on': oldname},
                                   ask_confirm=False)[0][0]
             # backport old type relations to new type
             # XXX workflows, other relations?
-            self.rqlexec('SET X from_entity NET WHERE X from_entity OET, '
-                         'NOT EXISTS(X2 from_entity NET, X relation_type XRT, X2 relation_type XRT, '
-                         'X to_entity XTE, X2 to_entity XTE), '
-                         'OET eid %(o)s, NET eid %(n)s',
-                         {'o': oldeid, 'n': new.eid}, ask_confirm=False)
-            self.rqlexec('SET X to_entity NET WHERE X to_entity OET, '
-                         'NOT EXISTS(X2 to_entity NET, X relation_type XRT, X2 relation_type XRT, '
-                         'X from_entity XTE, X2 from_entity XTE), '
-                         'OET eid %(o)s, NET eid %(n)s',
-                         {'o': oldeid, 'n': new.eid}, ask_confirm=False)
+            for r1, rr1 in [('from_entity', 'to_entity'),
+                            ('to_entity', 'from_entity')]:
+                self.rqlexec('SET X %(r1)s NET WHERE X %(r1)s OET, '
+                             'NOT EXISTS(X2 %(r1)s NET, X relation_type XRT, '
+                             'X2 relation_type XRT, X %(rr1)s XTE, X2 %(rr1)s XTE), '
+                             'OET eid %%(o)s, NET eid %%(n)s' % locals(),
+                             {'o': oldeid, 'n': new.eid}, ask_confirm=False)
+            # backport is / is_instance_of relation to new type
+            for rtype in ('is', 'is_instance_of'):
+                self.sqlexec('UPDATE %s_relation SET eid_to=%s WHERE eid_to=%s'
+                             % (rtype, new.eid, oldeid), ask_confirm=False)
+            # delete relations using SQL to avoid relations content removal
+            # triggered by schema synchronization hooks
+            self.sqlexec('DELETE FROM cw_CWRelation '
+                         'WHERE cw_from_entity=%(eid)s OR cw_to_entity=%(eid)s',
+                         {'eid': oldeid}, ask_confirm=False)
             # remove the old type: use rql to propagate deletion
             self.rqlexec('DELETE CWEType ET WHERE ET name %(on)s', {'on': oldname},
                          ask_confirm=False)