cubicweb/hooks/syncschema.py
changeset 11929 fcbd6b251d81
parent 11904 e760c54490b1
child 12025 ba79f4a4f5d8
equal deleted inserted replaced
11920:f13799fbcfea 11929:fcbd6b251d81
     1 # copyright 2003-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     1 # copyright 2003-2017 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     3 #
     3 #
     4 # This file is part of CubicWeb.
     4 # This file is part of CubicWeb.
     5 #
     5 #
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
   776             syssource.update_rdef_column(self.cnx, self.rdef)
   776             syssource.update_rdef_column(self.cnx, self.rdef)
   777         if self.unique_changed:
   777         if self.unique_changed:
   778             syssource.update_rdef_unique(self.cnx, self.rdef)
   778             syssource.update_rdef_unique(self.cnx, self.rdef)
   779 
   779 
   780 
   780 
   781 class CWConstraintAddOp(CWConstraintDelOp):
   781 class CWConstraintAddOp(hook.LateOperation, CWConstraintDelOp):
   782     """actually update constraint of a relation definition"""
   782     """actually update constraint of a relation definition"""
   783     entity = None  # make pylint happy
   783     entity = None  # make pylint happy
   784 
   784 
   785     def precommit_event(self):
   785     def precommit_event(self):
   786         cnx = self.cnx
   786         cnx = self.cnx
   884         # del_entity_type also removes entity's relations
   884         # del_entity_type also removes entity's relations
   885         self.cnx.vreg.schema.del_entity_type(self.etype)
   885         self.cnx.vreg.schema.del_entity_type(self.etype)
   886 
   886 
   887 
   887 
   888 class MemSchemaCWRTypeAdd(MemSchemaOperation):
   888 class MemSchemaCWRTypeAdd(MemSchemaOperation):
   889     """actually add the relation type to the instance's schema"""
   889     """Revert addition of the relation type from the instance's schema if something goes wrong.
       
   890     """
   890     rtypedef = None  # make pylint happy
   891     rtypedef = None  # make pylint happy
   891 
       
   892     def precommit_event(self):
       
   893         self.cnx.vreg.schema.add_relation_type(self.rtypedef)
       
   894 
   892 
   895     def revertprecommit_event(self):
   893     def revertprecommit_event(self):
   896         self.cnx.vreg.schema.del_relation_type(self.rtypedef.name)
   894         self.cnx.vreg.schema.del_relation_type(self.rtypedef.name)
   897 
   895 
   898 
   896 
  1086         self._cw.execute('DELETE CWRelation X WHERE X relation_type Y, Y eid %(x)s',
  1084         self._cw.execute('DELETE CWRelation X WHERE X relation_type Y, Y eid %(x)s',
  1087                          {'x': self.entity.eid})
  1085                          {'x': self.entity.eid})
  1088         MemSchemaCWRTypeDel(self._cw, rtype=name)
  1086         MemSchemaCWRTypeDel(self._cw, rtype=name)
  1089 
  1087 
  1090 
  1088 
  1091 class AfterAddCWComputedRTypeHook(SyncSchemaHook):
       
  1092     """after a CWComputedRType entity has been added:
       
  1093     * register an operation to add the relation type to the instance's
       
  1094       schema on commit
       
  1095 
       
  1096     We don't know yet this point if a table is necessary
       
  1097     """
       
  1098     __regid__ = 'syncaddcwcomputedrtype'
       
  1099     __select__ = SyncSchemaHook.__select__ & is_instance('CWComputedRType')
       
  1100     events = ('after_add_entity',)
       
  1101 
       
  1102     def __call__(self):
       
  1103         entity = self.entity
       
  1104         rtypedef = ybo.ComputedRelation(name=entity.name,
       
  1105                                         eid=entity.eid,
       
  1106                                         rule=entity.rule)
       
  1107         MemSchemaCWRTypeAdd(self._cw, rtypedef=rtypedef)
       
  1108 
       
  1109 
       
  1110 class AfterAddCWRTypeHook(SyncSchemaHook):
  1089 class AfterAddCWRTypeHook(SyncSchemaHook):
  1111     """after a CWRType entity has been added:
  1090     """After a CWRType entity has been added, register an operation to add the
  1112     * register an operation to add the relation type to the instance's
  1091     relation type to the instance's schema on commit.
  1113       schema on commit
  1092 
  1114 
  1093     We don't know yet at this point if a table is necessary, it will depend on
  1115     We don't know yet this point if a table is necessary
  1094     further addition of relation definitions.
  1116     """
  1095     """
  1117     __regid__ = 'syncaddcwrtype'
  1096     __regid__ = 'syncaddcwrtype'
  1118     __select__ = SyncSchemaHook.__select__ & is_instance('CWRType')
  1097     __select__ = SyncSchemaHook.__select__ & is_instance('CWRType')
  1119     events = ('after_add_entity',)
  1098     events = ('after_add_entity',)
  1120 
  1099 
  1121     def __call__(self):
  1100     def __call__(self):
       
  1101         rtypedef = self.rtype_def()
       
  1102         # modify the instance's schema now since we'll usually need the type definition to do
       
  1103         # further thing (e.g. add relation def of this type) but register and operation to revert
       
  1104         # this if necessary
       
  1105         self._cw.vreg.schema.add_relation_type(rtypedef)
       
  1106         MemSchemaCWRTypeAdd(self._cw, rtypedef=rtypedef)
       
  1107 
       
  1108     def rtype_def(self):
  1122         entity = self.entity
  1109         entity = self.entity
  1123         rtypedef = ybo.RelationType(name=entity.name,
  1110         return ybo.RelationType(name=entity.name,
  1124                                     description=entity.description,
  1111                                 description=entity.description,
  1125                                     inlined=entity.cw_edited.get('inlined', False),
  1112                                 inlined=entity.cw_edited.get('inlined', False),
  1126                                     symmetric=entity.cw_edited.get('symmetric', False),
  1113                                 symmetric=entity.cw_edited.get('symmetric', False),
  1127                                     eid=entity.eid)
  1114                                 eid=entity.eid)
  1128         MemSchemaCWRTypeAdd(self._cw, rtypedef=rtypedef)
  1115 
       
  1116 
       
  1117 class AfterAddCWComputedRTypeHook(AfterAddCWRTypeHook):
       
  1118     """After a CWComputedRType entity has been added, register an operation to
       
  1119     add the relation type to the instance's schema on commit.
       
  1120     """
       
  1121     __select__ = SyncSchemaHook.__select__ & is_instance('CWComputedRType')
       
  1122 
       
  1123     def rtype_def(self):
       
  1124         entity = self.entity
       
  1125         return ybo.ComputedRelation(name=entity.name,
       
  1126                                     eid=entity.eid,
       
  1127                                     rule=entity.rule)
  1129 
  1128 
  1130 
  1129 
  1131 class BeforeUpdateCWRTypeHook(SyncSchemaHook):
  1130 class BeforeUpdateCWRTypeHook(SyncSchemaHook):
  1132     """check name change, handle final"""
  1131     """check name change, handle final"""
  1133     __regid__ = 'syncupdatecwrtype'
  1132     __regid__ = 'syncupdatecwrtype'