hooks/syncschema.py
changeset 3376 f5c69485381f
parent 2968 0e3460341023
child 3400 93fc0070673a
equal deleted inserted replaced
3375:ebb11fa58ab9 3376:f5c69485381f
   761     """before deleting a CWEType entity:
   761     """before deleting a CWEType entity:
   762     * check that we don't remove a core entity type
   762     * check that we don't remove a core entity type
   763     * cascade to delete related CWAttribute and CWRelation entities
   763     * cascade to delete related CWAttribute and CWRelation entities
   764     * instantiate an operation to delete the entity type on commit
   764     * instantiate an operation to delete the entity type on commit
   765     """
   765     """
   766     __id__ = 'syncdelcwetype'
   766     __regid__ = 'syncdelcwetype'
   767     __select__ = SyncSchemaHook.__select__ & entity_implements('CWEType')
   767     __select__ = SyncSchemaHook.__select__ & entity_implements('CWEType')
   768     events = ('before_delete_entity',)
   768     events = ('before_delete_entity',)
   769 
   769 
   770     def __call__(self):
   770     def __call__(self):
   771         # final entities can't be deleted, don't care about that
   771         # final entities can't be deleted, don't care about that
   777         DropTable(self._cw, table=SQL_PREFIX + name)
   777         DropTable(self._cw, table=SQL_PREFIX + name)
   778         MemSchemaCWETypeDel(self._cw, name)
   778         MemSchemaCWETypeDel(self._cw, name)
   779 
   779 
   780 
   780 
   781 class AfterDelCWETypeHook(DelCWETypeHook):
   781 class AfterDelCWETypeHook(DelCWETypeHook):
   782     __id__ = 'wfcleanup'
   782     __regid__ = 'wfcleanup'
   783     events = ('after_delete_entity',)
   783     events = ('after_delete_entity',)
   784 
   784 
   785     def __call__(self):
   785     def __call__(self):
   786         # workflow cleanup
   786         # workflow cleanup
   787         self._cw.execute('DELETE Workflow X WHERE NOT X workflow_of Y')
   787         self._cw.execute('DELETE Workflow X WHERE NOT X workflow_of Y')
   794       CWAttribute entities
   794       CWAttribute entities
   795     * add owned_by relation by creating the necessary CWRelation entity
   795     * add owned_by relation by creating the necessary CWRelation entity
   796     * register an operation to add the entity type to the instance's
   796     * register an operation to add the entity type to the instance's
   797       schema on commit
   797       schema on commit
   798     """
   798     """
   799     __id__ = 'syncaddcwetype'
   799     __regid__ = 'syncaddcwetype'
   800     events = ('before_add_entity',)
   800     events = ('before_add_entity',)
   801 
   801 
   802     def __call__(self):
   802     def __call__(self):
   803         entity = self.entity
   803         entity = self.entity
   804         if entity.get('final'):
   804         if entity.get('final'):
   838             self._cw.execute(rql, kwargs)
   838             self._cw.execute(rql, kwargs)
   839 
   839 
   840 
   840 
   841 class BeforeUpdateCWETypeHook(DelCWETypeHook):
   841 class BeforeUpdateCWETypeHook(DelCWETypeHook):
   842     """check name change, handle final"""
   842     """check name change, handle final"""
   843     __id__ = 'syncupdatecwetype'
   843     __regid__ = 'syncupdatecwetype'
   844     events = ('before_update_entity',)
   844     events = ('before_update_entity',)
   845 
   845 
   846     def __call__(self):
   846     def __call__(self):
   847         entity = self.entity
   847         entity = self.entity
   848         check_valid_changes(self._cw, entity, ro_attrs=('final',))
   848         check_valid_changes(self._cw, entity, ro_attrs=('final',))
   862     """before deleting a CWRType entity:
   862     """before deleting a CWRType entity:
   863     * check that we don't remove a core relation type
   863     * check that we don't remove a core relation type
   864     * cascade to delete related CWAttribute and CWRelation entities
   864     * cascade to delete related CWAttribute and CWRelation entities
   865     * instantiate an operation to delete the relation type on commit
   865     * instantiate an operation to delete the relation type on commit
   866     """
   866     """
   867     __id__ = 'syncdelcwrtype'
   867     __regid__ = 'syncdelcwrtype'
   868     __select__ = SyncSchemaHook.__select__ & entity_implements('CWRType')
   868     __select__ = SyncSchemaHook.__select__ & entity_implements('CWRType')
   869     events = ('before_delete_entity',)
   869     events = ('before_delete_entity',)
   870 
   870 
   871     def __call__(self):
   871     def __call__(self):
   872         name = self.entity.name
   872         name = self.entity.name
   885     * register an operation to add the relation type to the instance's
   885     * register an operation to add the relation type to the instance's
   886       schema on commit
   886       schema on commit
   887 
   887 
   888     We don't know yet this point if a table is necessary
   888     We don't know yet this point if a table is necessary
   889     """
   889     """
   890     __id__ = 'syncaddcwrtype'
   890     __regid__ = 'syncaddcwrtype'
   891     events = ('after_add_entity',)
   891     events = ('after_add_entity',)
   892 
   892 
   893     def __call__(self):
   893     def __call__(self):
   894         entity = self.entity
   894         entity = self.entity
   895         rtype = RelationType(name=entity.name,
   895         rtype = RelationType(name=entity.name,
   901         MemSchemaCWRTypeAdd(self._cw, rtype)
   901         MemSchemaCWRTypeAdd(self._cw, rtype)
   902 
   902 
   903 
   903 
   904 class BeforeUpdateCWRTypeHook(DelCWRTypeHook):
   904 class BeforeUpdateCWRTypeHook(DelCWRTypeHook):
   905     """check name change, handle final"""
   905     """check name change, handle final"""
   906     __id__ = 'checkupdatecwrtype'
   906     __regid__ = 'checkupdatecwrtype'
   907     events = ('before_update_entity',)
   907     events = ('before_update_entity',)
   908 
   908 
   909     def __call__(self):
   909     def __call__(self):
   910         check_valid_changes(self._cw, self.entity)
   910         check_valid_changes(self._cw, self.entity)
   911 
   911 
   912 
   912 
   913 class AfterUpdateCWRTypeHook(DelCWRTypeHook):
   913 class AfterUpdateCWRTypeHook(DelCWRTypeHook):
   914     __id__ = 'syncupdatecwrtype'
   914     __regid__ = 'syncupdatecwrtype'
   915     events = ('after_update_entity',)
   915     events = ('after_update_entity',)
   916 
   916 
   917     def __call__(self):
   917     def __call__(self):
   918         entity = self.entity
   918         entity = self.entity
   919         rschema = self._cw.schema.rschema(entity.name)
   919         rschema = self._cw.schema.rschema(entity.name)
   936       of a non final relation, instantiate an operation to drop necessary
   936       of a non final relation, instantiate an operation to drop necessary
   937       table
   937       table
   938     * instantiate an operation to delete the relation definition on commit
   938     * instantiate an operation to delete the relation definition on commit
   939     * delete the associated relation type when necessary
   939     * delete the associated relation type when necessary
   940     """
   940     """
   941     __id__ = 'syncdelrelationtype'
   941     __regid__ = 'syncdelrelationtype'
   942     __select__ = SyncSchemaHook.__select__ & hook.match_rtype('relation_type')
   942     __select__ = SyncSchemaHook.__select__ & hook.match_rtype('relation_type')
   943     events = ('after_delete_relation',)
   943     events = ('after_delete_relation',)
   944 
   944 
   945     def __call__(self):
   945     def __call__(self):
   946         session = self._cw
   946         session = self._cw
   983 
   983 
   984 
   984 
   985 # CWAttribute / CWRelation hooks ###############################################
   985 # CWAttribute / CWRelation hooks ###############################################
   986 
   986 
   987 class AfterAddCWAttributeHook(SyncSchemaHook):
   987 class AfterAddCWAttributeHook(SyncSchemaHook):
   988     __id__ = 'syncaddcwattribute'
   988     __regid__ = 'syncaddcwattribute'
   989     __select__ = SyncSchemaHook.__select__ & entity_implements('CWAttribute')
   989     __select__ = SyncSchemaHook.__select__ & entity_implements('CWAttribute')
   990     events = ('after_add_entity',)
   990     events = ('after_add_entity',)
   991 
   991 
   992     def __call__(self):
   992     def __call__(self):
   993         SourceDbCWAttributeAdd(self._cw, entity=self.entity)
   993         SourceDbCWAttributeAdd(self._cw, entity=self.entity)
   994 
   994 
   995 
   995 
   996 class AfterAddCWRelationHook(AfterAddCWAttributeHook):
   996 class AfterAddCWRelationHook(AfterAddCWAttributeHook):
   997     __id__ = 'syncaddcwrelation'
   997     __regid__ = 'syncaddcwrelation'
   998     __select__ = SyncSchemaHook.__select__ & entity_implements('CWRelation')
   998     __select__ = SyncSchemaHook.__select__ & entity_implements('CWRelation')
   999 
   999 
  1000     def __call__(self):
  1000     def __call__(self):
  1001         SourceDbCWRelationAdd(self._cw, entity=self.entity)
  1001         SourceDbCWRelationAdd(self._cw, entity=self.entity)
  1002 
  1002 
  1003 
  1003 
  1004 class AfterUpdateCWRDefHook(SyncSchemaHook):
  1004 class AfterUpdateCWRDefHook(SyncSchemaHook):
  1005     __id__ = 'syncaddcwattribute'
  1005     __regid__ = 'syncaddcwattribute'
  1006     __select__ = SyncSchemaHook.__select__ & entity_implements('CWAttribute',
  1006     __select__ = SyncSchemaHook.__select__ & entity_implements('CWAttribute',
  1007                                                                'CWRelation')
  1007                                                                'CWRelation')
  1008     events = ('after_update_entity',)
  1008     events = ('after_update_entity',)
  1009 
  1009 
  1010     def __call__(self):
  1010     def __call__(self):
  1030 
  1030 
  1031 
  1031 
  1032 # constraints synchronization hooks ############################################
  1032 # constraints synchronization hooks ############################################
  1033 
  1033 
  1034 class AfterAddCWConstraintHook(SyncSchemaHook):
  1034 class AfterAddCWConstraintHook(SyncSchemaHook):
  1035     __id__ = 'syncaddcwconstraint'
  1035     __regid__ = 'syncaddcwconstraint'
  1036     __select__ = SyncSchemaHook.__select__ & entity_implements('CWConstraint')
  1036     __select__ = SyncSchemaHook.__select__ & entity_implements('CWConstraint')
  1037     events = ('after_add_entity', 'after_update_entity')
  1037     events = ('after_add_entity', 'after_update_entity')
  1038 
  1038 
  1039     def __call__(self):
  1039     def __call__(self):
  1040         MemSchemaCWConstraintAdd(self._cw, entity=self.entity)
  1040         MemSchemaCWConstraintAdd(self._cw, entity=self.entity)
  1041         SourceDbCWConstraintAdd(self._cw, entity=self.entity)
  1041         SourceDbCWConstraintAdd(self._cw, entity=self.entity)
  1042 
  1042 
  1043 
  1043 
  1044 class AfterAddConstrainedByHook(SyncSchemaHook):
  1044 class AfterAddConstrainedByHook(SyncSchemaHook):
  1045     __id__ = 'syncdelconstrainedby'
  1045     __regid__ = 'syncdelconstrainedby'
  1046     __select__ = SyncSchemaHook.__select__ & hook.match_rtype('constrainted_by')
  1046     __select__ = SyncSchemaHook.__select__ & hook.match_rtype('constrainted_by')
  1047     events = ('after_add_relation',)
  1047     events = ('after_add_relation',)
  1048 
  1048 
  1049     def __call__(self):
  1049     def __call__(self):
  1050         if self._cw.added_in_transaction(self.eidfrom):
  1050         if self._cw.added_in_transaction(self.eidfrom):
  1051             self._cw.transaction_data.setdefault(self.eidfrom, []).append(self.eidto)
  1051             self._cw.transaction_data.setdefault(self.eidfrom, []).append(self.eidto)
  1052 
  1052 
  1053 
  1053 
  1054 class BeforeDeleteConstrainedByHook(AfterAddConstrainedByHook):
  1054 class BeforeDeleteConstrainedByHook(AfterAddConstrainedByHook):
  1055     __id__ = 'syncdelconstrainedby'
  1055     __regid__ = 'syncdelconstrainedby'
  1056     events = ('before_delete_relation',)
  1056     events = ('before_delete_relation',)
  1057 
  1057 
  1058     def __call__(self):
  1058     def __call__(self):
  1059         if self._cw.deleted_in_transaction(self.eidfrom):
  1059         if self._cw.deleted_in_transaction(self.eidfrom):
  1060             return
  1060             return
  1075 
  1075 
  1076 # permissions synchronization hooks ############################################
  1076 # permissions synchronization hooks ############################################
  1077 
  1077 
  1078 class AfterAddPermissionHook(SyncSchemaHook):
  1078 class AfterAddPermissionHook(SyncSchemaHook):
  1079     """added entity/relation *_permission, need to update schema"""
  1079     """added entity/relation *_permission, need to update schema"""
  1080     __id__ = 'syncaddperm'
  1080     __regid__ = 'syncaddperm'
  1081     __select__ = SyncSchemaHook.__select__ & hook.match_rtype(
  1081     __select__ = SyncSchemaHook.__select__ & hook.match_rtype(
  1082         'read_permission', 'add_permission', 'delete_permission',
  1082         'read_permission', 'add_permission', 'delete_permission',
  1083         'update_permission')
  1083         'update_permission')
  1084     events = ('after_add_relation',)
  1084     events = ('after_add_relation',)
  1085 
  1085 
  1095 class BeforeDelPermissionHook(AfterAddPermissionHook):
  1095 class BeforeDelPermissionHook(AfterAddPermissionHook):
  1096     """delete entity/relation *_permission, need to update schema
  1096     """delete entity/relation *_permission, need to update schema
  1097 
  1097 
  1098     skip the operation if the related type is being deleted
  1098     skip the operation if the related type is being deleted
  1099     """
  1099     """
  1100     __id__ = 'syncdelperm'
  1100     __regid__ = 'syncdelperm'
  1101     events = ('before_delete_relation',)
  1101     events = ('before_delete_relation',)
  1102 
  1102 
  1103     def __call__(self):
  1103     def __call__(self):
  1104         if self._cw.deleted_in_transaction(self.eidfrom):
  1104         if self._cw.deleted_in_transaction(self.eidfrom):
  1105             return
  1105             return
  1113 
  1113 
  1114 # specializes synchronization hooks ############################################
  1114 # specializes synchronization hooks ############################################
  1115 
  1115 
  1116 
  1116 
  1117 class AfterAddSpecializesHook(SyncSchemaHook):
  1117 class AfterAddSpecializesHook(SyncSchemaHook):
  1118     __id__ = 'syncaddspecializes'
  1118     __regid__ = 'syncaddspecializes'
  1119     __select__ = SyncSchemaHook.__select__ & hook.match_rtype('specializes')
  1119     __select__ = SyncSchemaHook.__select__ & hook.match_rtype('specializes')
  1120     events = ('after_add_relation',)
  1120     events = ('after_add_relation',)
  1121 
  1121 
  1122     def __call__(self):
  1122     def __call__(self):
  1123         MemSchemaSpecializesAdd(session, etypeeid=self.eidfrom,
  1123         MemSchemaSpecializesAdd(session, etypeeid=self.eidfrom,
  1124                                 parentetypeeid=self.eidto)
  1124                                 parentetypeeid=self.eidto)
  1125 
  1125 
  1126 
  1126 
  1127 class AfterAddSpecializesHook(SyncSchemaHook):
  1127 class AfterAddSpecializesHook(SyncSchemaHook):
  1128     __id__ = 'syncdelspecializes'
  1128     __regid__ = 'syncdelspecializes'
  1129     __select__ = SyncSchemaHook.__select__ & hook.match_rtype('specializes')
  1129     __select__ = SyncSchemaHook.__select__ & hook.match_rtype('specializes')
  1130     events = ('after_delete_relation',)
  1130     events = ('after_delete_relation',)
  1131 
  1131 
  1132     def __call__(self):
  1132     def __call__(self):
  1133         MemSchemaSpecializesDel(session, etypeeid=self.eidfrom,
  1133         MemSchemaSpecializesDel(session, etypeeid=self.eidfrom,