[CWEP002 migration] support drop_relation_type for computed relations
authorLaura Médioni <laura.medioni@logilab.fr>
Thu, 28 Aug 2014 07:55:33 +0200
changeset 9962 64b573d54133
parent 9961 cef58bd36f7b
child 9963 5531f5577b50
[CWEP002 migration] support drop_relation_type for computed relations Related to #3546717.
hooks/syncschema.py
server/migractions.py
server/test/unittest_migractions.py
--- a/hooks/syncschema.py	Thu Aug 28 07:49:31 2014 +0200
+++ b/hooks/syncschema.py	Thu Aug 28 07:55:33 2014 +0200
@@ -1063,6 +1063,24 @@
         RDefDelOp(cnx, rdef=rdef)
 
 
+# CWComputedRType hooks #######################################################
+
+class DelCWComputedRTypeHook(SyncSchemaHook):
+    """before deleting a CWComputedRType entity:
+    * check that we don't remove a core relation type
+    * instantiate an operation to delete the relation type on commit
+    """
+    __regid__ = 'syncdelcwcomputedrtype'
+    __select__ = SyncSchemaHook.__select__ & is_instance('CWComputedRType')
+    events = ('before_delete_entity',)
+
+    def __call__(self):
+        name = self.entity.name
+        if name in CORE_TYPES:
+            raise validation_error(self.entity, {None: _("can't be deleted")})
+        MemSchemaCWRTypeDel(self._cw, rtype=name)
+
+
 # CWAttribute / CWRelation hooks ###############################################
 
 class AfterAddCWAttributeHook(SyncSchemaHook):
--- a/server/migractions.py	Thu Aug 28 07:49:31 2014 +0200
+++ b/server/migractions.py	Thu Aug 28 07:55:33 2014 +0200
@@ -1057,8 +1057,12 @@
 
     def cmd_drop_relation_type(self, rtype, commit=True):
         """unregister an existing relation type"""
-        # unregister the relation from CWRType
-        self.rqlexec('DELETE CWRType X WHERE X name %r' % rtype,
+        rschema = self.repo.schema[rtype]
+        if rschema.rule:
+            etype = 'CWComputedRType'
+        else:
+            etype = 'CWRType'
+        self.rqlexec('DELETE %s X WHERE X name %r' % (etype, rtype),
                      ask_confirm=self.verbosity>=2)
         if commit:
             self.commit()
--- a/server/test/unittest_migractions.py	Thu Aug 28 07:49:31 2014 +0200
+++ b/server/test/unittest_migractions.py	Thu Aug 28 07:55:33 2014 +0200
@@ -703,6 +703,12 @@
                          'Cannot drop a relation definition for a computed '
                          'relation (notes)')
 
+    def test_computed_relation_drop_relation_type(self):
+        self.assertIn('notes', self.schema)
+        with self.mh() as (cnx, mh):
+            mh.cmd_drop_relation_type('notes')
+        self.assertNotIn('notes', self.schema)
+
 
 if __name__ == '__main__':
     unittest_main()