hooks/syncschema.py
changeset 9375 8e88576787c3
parent 9365 71c12e778162
child 9402 2c48c091b6a2
child 9588 fe267b7336f3
equal deleted inserted replaced
9374:1236d9058ad3 9375:8e88576787c3
   712             self.unique_changed = True
   712             self.unique_changed = True
   713 
   713 
   714 
   714 
   715 class CWUniqueTogetherConstraintAddOp(MemSchemaOperation):
   715 class CWUniqueTogetherConstraintAddOp(MemSchemaOperation):
   716     entity = None # make pylint happy
   716     entity = None # make pylint happy
       
   717 
   717     def precommit_event(self):
   718     def precommit_event(self):
   718         session = self.session
   719         session = self.session
   719         prefix = SQL_PREFIX
   720         prefix = SQL_PREFIX
   720         table = '%s%s' % (prefix, self.entity.constraint_of[0].name)
   721         entity = self.entity
   721         cols = ['%s%s' % (prefix, r.name) for r in self.entity.relations]
   722         table = '%s%s' % (prefix, entity.constraint_of[0].name)
   722         dbhelper= session.cnxset.source('system').dbhelper
   723         cols = ['%s%s' % (prefix, r.name) for r in entity.relations]
   723         sqls = dbhelper.sqls_create_multicol_unique_index(table, cols)
   724         dbhelper = session.cnxset.source('system').dbhelper
       
   725         sqls = dbhelper.sqls_create_multicol_unique_index(table, cols, entity.name)
   724         for sql in sqls:
   726         for sql in sqls:
   725             session.system_sql(sql)
   727             session.system_sql(sql)
   726 
   728 
   727     # XXX revertprecommit_event
       
   728 
       
   729     def postcommit_event(self):
   729     def postcommit_event(self):
   730         eschema = self.session.vreg.schema.schema_by_eid(self.entity.constraint_of[0].eid)
   730         entity = self.entity
   731         attrs = [r.name for r in self.entity.relations]
   731         eschema = self.session.vreg.schema.schema_by_eid(entity.constraint_of[0].eid)
       
   732         attrs = [r.name for r in entity.relations]
   732         eschema._unique_together.append(attrs)
   733         eschema._unique_together.append(attrs)
   733 
   734 
   734 
   735 
   735 class CWUniqueTogetherConstraintDelOp(MemSchemaOperation):
   736 class CWUniqueTogetherConstraintDelOp(MemSchemaOperation):
   736     entity = oldcstr = None # for pylint
   737     entity = cstrname = None # for pylint
   737     cols = [] # for pylint
   738     cols = () # for pylint
       
   739 
   738     def precommit_event(self):
   740     def precommit_event(self):
   739         session = self.session
   741         session = self.session
   740         prefix = SQL_PREFIX
   742         prefix = SQL_PREFIX
   741         table = '%s%s' % (prefix, self.entity.type)
   743         table = '%s%s' % (prefix, self.entity.type)
   742         dbhelper= session.cnxset.source('system').dbhelper
   744         dbhelper = session.cnxset.source('system').dbhelper
   743         cols = ['%s%s' % (prefix, c) for c in self.cols]
   745         cols = ['%s%s' % (prefix, c) for c in self.cols]
   744         sqls = dbhelper.sqls_drop_multicol_unique_index(table, cols)
   746         sqls = dbhelper.sqls_drop_multicol_unique_index(table, cols, self.cstrname)
   745         for sql in sqls:
   747         for sql in sqls:
   746             try:
   748             session.system_sql(sql)
   747                 session.system_sql(sql)
       
   748             except Exception as exc: # should be ProgrammingError
       
   749                 if sql.startswith('DROP'):
       
   750                     self.error('execute of `%s` failed (cause: %s)', sql, exc)
       
   751                     continue
       
   752                 raise
       
   753 
       
   754     # XXX revertprecommit_event
       
   755 
   749 
   756     def postcommit_event(self):
   750     def postcommit_event(self):
   757         eschema = self.session.vreg.schema.schema_by_eid(self.entity.eid)
   751         eschema = self.session.vreg.schema.schema_by_eid(self.entity.eid)
   758         cols = set(self.cols)
   752         cols = set(self.cols)
   759         unique_together = [ut for ut in eschema._unique_together
   753         unique_together = [ut for ut in eschema._unique_together
  1169         if self._cw.deleted_in_transaction(self.eidto):
  1163         if self._cw.deleted_in_transaction(self.eidto):
  1170             return
  1164             return
  1171         schema = self._cw.vreg.schema
  1165         schema = self._cw.vreg.schema
  1172         cstr = self._cw.entity_from_eid(self.eidfrom)
  1166         cstr = self._cw.entity_from_eid(self.eidfrom)
  1173         entity = schema.schema_by_eid(self.eidto)
  1167         entity = schema.schema_by_eid(self.eidto)
  1174         cols = [r.name for r in cstr.relations]
  1168         cols = tuple(r.name for r in cstr.relations)
  1175         CWUniqueTogetherConstraintDelOp(self._cw, entity=entity,
  1169         CWUniqueTogetherConstraintDelOp(self._cw, entity=entity,
  1176                                         oldcstr=cstr, cols=cols)
  1170                                         cstrname=cstr.name, cols=cols)
  1177 
  1171 
  1178 
  1172 
  1179 # permissions synchronization hooks ############################################
  1173 # permissions synchronization hooks ############################################
  1180 
  1174 
  1181 class AfterAddPermissionHook(SyncSchemaHook):
  1175 class AfterAddPermissionHook(SyncSchemaHook):