hooks/syncschema.py
branchstable
changeset 4839 f482dbdf2f8c
parent 4828 3e173d598cad
child 4845 dc351b96f596
child 4927 19fd1952ad63
equal deleted inserted replaced
4838:d4187a08ccdf 4839:f482dbdf2f8c
   224                 (self.newname, self.oldname))
   224                 (self.newname, self.oldname))
   225 
   225 
   226 
   226 
   227 class SourceDbCWRTypeUpdate(hook.Operation):
   227 class SourceDbCWRTypeUpdate(hook.Operation):
   228     """actually update some properties of a relation definition"""
   228     """actually update some properties of a relation definition"""
   229     rschema = entity = None # make pylint happy
   229     rschema = entity = values = None # make pylint happy
   230 
   230 
   231     def precommit_event(self):
   231     def precommit_event(self):
       
   232         rschema = self.rschema
       
   233         if rschema.final:
       
   234             return
   232         session = self.session
   235         session = self.session
   233         rschema = self.rschema
   236         if 'fulltext_container' in self.values:
   234         entity = self.entity
       
   235         if 'fulltext_container' in entity.edited_attributes:
       
   236             ftiupdates = session.transaction_data.setdefault(
   237             ftiupdates = session.transaction_data.setdefault(
   237                 'fti_update_etypes', set())
   238                 'fti_update_etypes', set())
   238             for subjtype, objtype in rschema.rdefs:
   239             for subjtype, objtype in rschema.rdefs:
   239                 ftiupdates.add(subjtype)
   240                 ftiupdates.add(subjtype)
   240                 ftiupdates.add(objtype)
   241                 ftiupdates.add(objtype)
   241             UpdateFTIndexOp(session)
   242             UpdateFTIndexOp(session)
   242         if rschema.final or not 'inlined' in entity.edited_attributes:
   243         if not 'inlined' in self.values:
   243             return # nothing to do
   244             return # nothing to do
   244         inlined = entity.inlined
   245         inlined = self.values['inlined']
   245         # check in-lining is necessary / possible
   246         # check in-lining is necessary / possible
   246         if not entity.inlined_changed(inlined):
   247         if inlined:
   247             return # nothing to do
   248             self.entity.check_inlined_allowed()
   248         # inlined changed, make necessary physical changes!
   249         # inlined changed, make necessary physical changes!
   249         sqlexec = self.session.system_sql
   250         sqlexec = self.session.system_sql
   250         rtype = rschema.type
   251         rtype = rschema.type
   251         eidcolumn = SQL_PREFIX + 'eid'
   252         eidcolumn = SQL_PREFIX + 'eid'
   252         if not inlined:
   253         if not inlined:
   923         MemSchemaCWRTypeAdd(self._cw, rtype)
   924         MemSchemaCWRTypeAdd(self._cw, rtype)
   924 
   925 
   925 
   926 
   926 class BeforeUpdateCWRTypeHook(DelCWRTypeHook):
   927 class BeforeUpdateCWRTypeHook(DelCWRTypeHook):
   927     """check name change, handle final"""
   928     """check name change, handle final"""
   928     __regid__ = 'checkupdatecwrtype'
   929     __regid__ = 'syncupdatecwrtype'
   929     events = ('before_update_entity',)
   930     events = ('before_update_entity',)
   930 
   931 
   931     def __call__(self):
   932     def __call__(self):
   932         check_valid_changes(self._cw, self.entity)
       
   933 
       
   934 
       
   935 class AfterUpdateCWRTypeHook(DelCWRTypeHook):
       
   936     __regid__ = 'syncupdatecwrtype'
       
   937     events = ('after_update_entity',)
       
   938 
       
   939     def __call__(self):
       
   940         entity = self.entity
   933         entity = self.entity
       
   934         check_valid_changes(self._cw, entity)
   941         newvalues = {}
   935         newvalues = {}
   942         for prop in ('symmetric', 'inlined', 'fulltext_container'):
   936         for prop in ('symmetric', 'inlined', 'fulltext_container'):
   943             if prop in entity.edited_attributes:
   937             if prop in entity.edited_attributes:
   944                 newvalues[prop] = entity[prop]
   938                 old, new = hook.entity_oldnewvalue(entity, prop)
       
   939                 if old != new:
       
   940                     newvalues[prop] = entity[prop]
   945         if newvalues:
   941         if newvalues:
   946             rschema = self._cw.vreg.schema.rschema(entity.name)
   942             rschema = self._cw.vreg.schema.rschema(entity.name)
   947             SourceDbCWRTypeUpdate(self._cw, rschema=rschema, entity=entity)
   943             SourceDbCWRTypeUpdate(self._cw, rschema=rschema, entity=entity,
       
   944                                   values=newvalues)
   948             MemSchemaCWRTypeUpdate(self._cw, rschema=rschema, values=newvalues)
   945             MemSchemaCWRTypeUpdate(self._cw, rschema=rschema, values=newvalues)
   949 
   946 
   950 
   947 
   951 class AfterDelRelationTypeHook(SyncSchemaHook):
   948 class AfterDelRelationTypeHook(SyncSchemaHook):
   952     """before deleting a CWAttribute or CWRelation entity:
   949     """before deleting a CWAttribute or CWRelation entity:
  1022 
  1019 
  1023 
  1020 
  1024 class AfterUpdateCWRDefHook(SyncSchemaHook):
  1021 class AfterUpdateCWRDefHook(SyncSchemaHook):
  1025     __regid__ = 'syncaddcwattribute'
  1022     __regid__ = 'syncaddcwattribute'
  1026     __select__ = SyncSchemaHook.__select__ & implements('CWAttribute',
  1023     __select__ = SyncSchemaHook.__select__ & implements('CWAttribute',
  1027                                                                'CWRelation')
  1024                                                         'CWRelation')
  1028     events = ('after_update_entity',)
  1025     events = ('before_update_entity',)
  1029 
  1026 
  1030     def __call__(self):
  1027     def __call__(self):
  1031         entity = self.entity
  1028         entity = self.entity
  1032         if self._cw.deleted_in_transaction(entity.eid):
  1029         if self._cw.deleted_in_transaction(entity.eid):
  1033             return
  1030             return
  1038             if prop == 'constraints':
  1035             if prop == 'constraints':
  1039                 continue
  1036                 continue
  1040             if prop == 'order':
  1037             if prop == 'order':
  1041                 prop = 'ordernum'
  1038                 prop = 'ordernum'
  1042             if prop in entity.edited_attributes:
  1039             if prop in entity.edited_attributes:
  1043                 newvalues[prop] = entity[prop]
  1040                 old, new = hook.entity_oldnewvalue(entity, prop)
       
  1041                 if old != new:
       
  1042                     newvalues[prop] = entity[prop]
  1044         if newvalues:
  1043         if newvalues:
  1045             subjtype = entity.stype.name
  1044             subjtype = entity.stype.name
  1046             MemSchemaRDefUpdate(self._cw, kobj=(subjtype, desttype),
  1045             MemSchemaRDefUpdate(self._cw, kobj=(subjtype, desttype),
  1047                                 rschema=rschema, values=newvalues)
  1046                                 rschema=rschema, values=newvalues)
  1048             SourceDbRDefUpdate(self._cw, kobj=(subjtype, desttype),
  1047             SourceDbRDefUpdate(self._cw, kobj=(subjtype, desttype),