hooks/syncschema.py
changeset 4075 e710f4052bd6
parent 4054 03c9a539d282
child 4181 c79135c217df
equal deleted inserted replaced
4074:9cce43f52c61 4075:e710f4052bd6
    17 from yams.schema2sql import eschema2sql, rschema2sql, type_from_constraints
    17 from yams.schema2sql import eschema2sql, rschema2sql, type_from_constraints
    18 
    18 
    19 from logilab.common.decorators import clear_cache
    19 from logilab.common.decorators import clear_cache
    20 
    20 
    21 from cubicweb import ValidationError, RepositoryError
    21 from cubicweb import ValidationError, RepositoryError
    22 from cubicweb.selectors import entity_implements
    22 from cubicweb.selectors import implements
    23 from cubicweb.schema import META_RTYPES, VIRTUAL_RTYPES, CONSTRAINTS
    23 from cubicweb.schema import META_RTYPES, VIRTUAL_RTYPES, CONSTRAINTS
    24 from cubicweb.server import hook, schemaserial as ss
    24 from cubicweb.server import hook, schemaserial as ss
    25 from cubicweb.server.sqlutils import SQL_PREFIX
    25 from cubicweb.server.sqlutils import SQL_PREFIX
    26 
    26 
    27 
    27 
   763     * check that we don't remove a core entity type
   763     * check that we don't remove a core entity type
   764     * cascade to delete related CWAttribute and CWRelation entities
   764     * cascade to delete related CWAttribute and CWRelation entities
   765     * instantiate an operation to delete the entity type on commit
   765     * instantiate an operation to delete the entity type on commit
   766     """
   766     """
   767     __regid__ = 'syncdelcwetype'
   767     __regid__ = 'syncdelcwetype'
   768     __select__ = SyncSchemaHook.__select__ & entity_implements('CWEType')
   768     __select__ = SyncSchemaHook.__select__ & implements('CWEType')
   769     events = ('before_delete_entity',)
   769     events = ('before_delete_entity',)
   770 
   770 
   771     def __call__(self):
   771     def __call__(self):
   772         # final entities can't be deleted, don't care about that
   772         # final entities can't be deleted, don't care about that
   773         name = self.entity.name
   773         name = self.entity.name
   864     * check that we don't remove a core relation type
   864     * check that we don't remove a core relation type
   865     * cascade to delete related CWAttribute and CWRelation entities
   865     * cascade to delete related CWAttribute and CWRelation entities
   866     * instantiate an operation to delete the relation type on commit
   866     * instantiate an operation to delete the relation type on commit
   867     """
   867     """
   868     __regid__ = 'syncdelcwrtype'
   868     __regid__ = 'syncdelcwrtype'
   869     __select__ = SyncSchemaHook.__select__ & entity_implements('CWRType')
   869     __select__ = SyncSchemaHook.__select__ & implements('CWRType')
   870     events = ('before_delete_entity',)
   870     events = ('before_delete_entity',)
   871 
   871 
   872     def __call__(self):
   872     def __call__(self):
   873         name = self.entity.name
   873         name = self.entity.name
   874         if name in CORE_RTYPES:
   874         if name in CORE_RTYPES:
   997 
   997 
   998 # CWAttribute / CWRelation hooks ###############################################
   998 # CWAttribute / CWRelation hooks ###############################################
   999 
   999 
  1000 class AfterAddCWAttributeHook(SyncSchemaHook):
  1000 class AfterAddCWAttributeHook(SyncSchemaHook):
  1001     __regid__ = 'syncaddcwattribute'
  1001     __regid__ = 'syncaddcwattribute'
  1002     __select__ = SyncSchemaHook.__select__ & entity_implements('CWAttribute')
  1002     __select__ = SyncSchemaHook.__select__ & implements('CWAttribute')
  1003     events = ('after_add_entity',)
  1003     events = ('after_add_entity',)
  1004 
  1004 
  1005     def __call__(self):
  1005     def __call__(self):
  1006         SourceDbCWAttributeAdd(self._cw, entity=self.entity)
  1006         SourceDbCWAttributeAdd(self._cw, entity=self.entity)
  1007 
  1007 
  1008 
  1008 
  1009 class AfterAddCWRelationHook(AfterAddCWAttributeHook):
  1009 class AfterAddCWRelationHook(AfterAddCWAttributeHook):
  1010     __regid__ = 'syncaddcwrelation'
  1010     __regid__ = 'syncaddcwrelation'
  1011     __select__ = SyncSchemaHook.__select__ & entity_implements('CWRelation')
  1011     __select__ = SyncSchemaHook.__select__ & implements('CWRelation')
  1012 
  1012 
  1013     def __call__(self):
  1013     def __call__(self):
  1014         SourceDbCWRelationAdd(self._cw, entity=self.entity)
  1014         SourceDbCWRelationAdd(self._cw, entity=self.entity)
  1015 
  1015 
  1016 
  1016 
  1017 class AfterUpdateCWRDefHook(SyncSchemaHook):
  1017 class AfterUpdateCWRDefHook(SyncSchemaHook):
  1018     __regid__ = 'syncaddcwattribute'
  1018     __regid__ = 'syncaddcwattribute'
  1019     __select__ = SyncSchemaHook.__select__ & entity_implements('CWAttribute',
  1019     __select__ = SyncSchemaHook.__select__ & implements('CWAttribute',
  1020                                                                'CWRelation')
  1020                                                                'CWRelation')
  1021     events = ('after_update_entity',)
  1021     events = ('after_update_entity',)
  1022 
  1022 
  1023     def __call__(self):
  1023     def __call__(self):
  1024         entity = self.entity
  1024         entity = self.entity
  1044 
  1044 
  1045 # constraints synchronization hooks ############################################
  1045 # constraints synchronization hooks ############################################
  1046 
  1046 
  1047 class AfterAddCWConstraintHook(SyncSchemaHook):
  1047 class AfterAddCWConstraintHook(SyncSchemaHook):
  1048     __regid__ = 'syncaddcwconstraint'
  1048     __regid__ = 'syncaddcwconstraint'
  1049     __select__ = SyncSchemaHook.__select__ & entity_implements('CWConstraint')
  1049     __select__ = SyncSchemaHook.__select__ & implements('CWConstraint')
  1050     events = ('after_add_entity', 'after_update_entity')
  1050     events = ('after_add_entity', 'after_update_entity')
  1051 
  1051 
  1052     def __call__(self):
  1052     def __call__(self):
  1053         MemSchemaCWConstraintAdd(self._cw, entity=self.entity)
  1053         MemSchemaCWConstraintAdd(self._cw, entity=self.entity)
  1054         SourceDbCWConstraintAdd(self._cw, entity=self.entity)
  1054         SourceDbCWConstraintAdd(self._cw, entity=self.entity)