hooks/syncschema.py
changeset 10921 977def81780a
parent 10920 4b0443afbb3d
child 11004 14ba505fb652
equal deleted inserted replaced
10920:4b0443afbb3d 10921:977def81780a
   330                     op.add_data(subjtype)
   330                     op.add_data(subjtype)
   331         # update the in-memory schema first
   331         # update the in-memory schema first
   332         self.oldvalues = dict( (attr, getattr(rschema, attr)) for attr in self.values)
   332         self.oldvalues = dict( (attr, getattr(rschema, attr)) for attr in self.values)
   333         self.rschema.__dict__.update(self.values)
   333         self.rschema.__dict__.update(self.values)
   334         # then make necessary changes to the system source database
   334         # then make necessary changes to the system source database
   335         if not 'inlined' in self.values:
   335         if 'inlined' not in self.values:
   336             return # nothing to do
   336             return # nothing to do
   337         inlined = self.values['inlined']
   337         inlined = self.values['inlined']
   338         # check in-lining is possible when inlined
   338         # check in-lining is possible when inlined
   339         if inlined:
   339         if inlined:
   340             self.entity.check_inlined_allowed()
   340             self.entity.check_inlined_allowed()
   606                            {'r': rschema.eid, 'e': rdef.subject.eid})
   606                            {'r': rschema.eid, 'e': rdef.subject.eid})
   607             if rset[0][0] == 0 and not cnx.deleted_in_transaction(rdef.subject.eid):
   607             if rset[0][0] == 0 and not cnx.deleted_in_transaction(rdef.subject.eid):
   608                 ptypes = cnx.transaction_data.setdefault('pendingrtypes', set())
   608                 ptypes = cnx.transaction_data.setdefault('pendingrtypes', set())
   609                 ptypes.add(rschema.type)
   609                 ptypes.add(rschema.type)
   610                 DropColumn.get_instance(cnx).add_data((str(rdef.subject), str(rschema)))
   610                 DropColumn.get_instance(cnx).add_data((str(rdef.subject), str(rschema)))
       
   611             elif rschema.inlined:
       
   612                 cnx.system_sql('UPDATE %s%s SET %s%s=NULL WHERE '
       
   613                                'EXISTS(SELECT 1 FROM entities '
       
   614                                '       WHERE eid=%s%s AND type=%%(to_etype)s)'
       
   615                                % (SQL_PREFIX, rdef.subject, SQL_PREFIX, rdef.rtype,
       
   616                                   SQL_PREFIX, rdef.rtype),
       
   617                                {'to_etype': rdef.object.type})
   611         elif lastrel:
   618         elif lastrel:
   612             DropRelationTable(cnx, str(rschema))
   619             DropRelationTable(cnx, str(rschema))
       
   620         else:
       
   621             cnx.system_sql('DELETE FROM %s_relation WHERE '
       
   622                            'EXISTS(SELECT 1 FROM entities '
       
   623                            '       WHERE eid=eid_from AND type=%%(from_etype)s)'
       
   624                            ' AND EXISTS(SELECT 1 FROM entities '
       
   625                            '       WHERE eid=eid_to AND type=%%(to_etype)s)'
       
   626                            % rschema,
       
   627                            {'from_etype': rdef.subject.type, 'to_etype': rdef.object.type})
   613         # then update the in-memory schema
   628         # then update the in-memory schema
   614         if rdef.subject not in ETYPE_NAME_MAP and rdef.object not in ETYPE_NAME_MAP:
   629         if rdef.subject not in ETYPE_NAME_MAP and rdef.object not in ETYPE_NAME_MAP:
   615             rschema.del_relation_def(rdef.subject, rdef.object)
   630             rschema.del_relation_def(rdef.subject, rdef.object)
   616         # if this is the last relation definition of this type, drop associated
   631         # if this is the last relation definition of this type, drop associated
   617         # relation type
   632         # relation type
   967         name = self.entity.name
   982         name = self.entity.name
   968         if name in CORE_TYPES:
   983         if name in CORE_TYPES:
   969             raise validation_error(self.entity, {None: _("can't be deleted")})
   984             raise validation_error(self.entity, {None: _("can't be deleted")})
   970         # delete every entities of this type
   985         # delete every entities of this type
   971         if name not in ETYPE_NAME_MAP:
   986         if name not in ETYPE_NAME_MAP:
   972             self._cw.execute('DELETE %s X' % name)
       
   973             MemSchemaCWETypeDel(self._cw, etype=name)
   987             MemSchemaCWETypeDel(self._cw, etype=name)
   974         DropTable(self._cw, table=SQL_PREFIX + name)
   988         DropTable(self._cw, table=SQL_PREFIX + name)
   975 
   989 
   976 
   990 
   977 class AfterDelCWETypeHook(DelCWETypeHook):
   991 class AfterDelCWETypeHook(DelCWETypeHook):
  1150             rdeftype = 'CWAttribute'
  1164             rdeftype = 'CWAttribute'
  1151             pendingrdefs.add((subjschema, rschema))
  1165             pendingrdefs.add((subjschema, rschema))
  1152         else:
  1166         else:
  1153             rdeftype = 'CWRelation'
  1167             rdeftype = 'CWRelation'
  1154             pendingrdefs.add((subjschema, rschema, objschema))
  1168             pendingrdefs.add((subjschema, rschema, objschema))
  1155             if not (cnx.deleted_in_transaction(subjschema.eid) or
       
  1156                     cnx.deleted_in_transaction(objschema.eid)):
       
  1157                 cnx.execute('DELETE X %s Y WHERE X is %s, Y is %s'
       
  1158                             % (rschema, subjschema, objschema))
       
  1159         RDefDelOp(cnx, rdef=rdef)
  1169         RDefDelOp(cnx, rdef=rdef)
  1160 
  1170 
  1161 
  1171 
  1162 # CWComputedRType hooks #######################################################
  1172 # CWComputedRType hooks #######################################################
  1163 
  1173