def test_update(self): uowcommit, a1, b1, a_mapper, b_mapper = self._fixture() a1.obj().id = 10 a1.commit_all(a1.dict) a1.obj().id = 12 pairs = [(a_mapper.c.id, b_mapper.c.id,)] dest = {} sync.update(a1, a_mapper, dest, "old_", pairs) eq_(dest, {'id': 12, 'old_id': 10})
def process_saves(self, uowcommit, states): secondary_delete = [] secondary_insert = [] secondary_update = [] processed = self._get_reversed_processed_set(uowcommit) tmp = set() for state in states: need_cascade_pks = not self.passive_updates and \ self._pks_changed(uowcommit, state) history = uowcommit.get_attribute_history( state, self.key, passive=not need_cascade_pks) if history: for child in history.added: if child is None or \ (processed is not None and (state, child) in processed): continue associationrow = {} self._synchronize(state, child, associationrow, False, uowcommit) secondary_insert.append(associationrow) for child in history.deleted: if child is None or \ (processed is not None and (state, child) in processed) or \ not uowcommit.session._contains_state(child): continue associationrow = {} self._synchronize(state, child, associationrow, False, uowcommit) secondary_delete.append(associationrow) tmp.update((c, state) for c in history.added + history.deleted) if need_cascade_pks: for child in history.unchanged: associationrow = {} sync.update(state, self.parent, associationrow, "old_", self.prop.synchronize_pairs) sync.update(child, self.mapper, associationrow, "old_", self.prop.secondary_synchronize_pairs) secondary_update.append(associationrow) if processed is not None: processed.update(tmp) self._run_crud(uowcommit, secondary_insert, secondary_update, secondary_delete)
def process_saves(self, uowcommit, states): secondary_delete = [] secondary_insert = [] secondary_update = [] processed = self._get_reversed_processed_set(uowcommit) tmp = set() for state in states: need_cascade_pks = not self.passive_updates and \ self._pks_changed(uowcommit, state) if need_cascade_pks: passive = attributes.PASSIVE_OFF else: passive = attributes.PASSIVE_NO_INITIALIZE history = uowcommit.get_attribute_history(state, self.key, passive) if history: for child in history.added: if child is None or \ (processed is not None and (state, child) in processed): continue associationrow = {} if not self._synchronize(state, child, associationrow, False, uowcommit, "add"): continue secondary_insert.append(associationrow) for child in history.deleted: if child is None or \ (processed is not None and (state, child) in processed): continue associationrow = {} if not self._synchronize(state, child, associationrow, False, uowcommit, "delete"): continue secondary_delete.append(associationrow) tmp.update((c, state) for c in history.added + history.deleted) if need_cascade_pks: for child in history.unchanged: associationrow = {} sync.update(state, self.parent, associationrow, "old_", self.prop.synchronize_pairs) sync.update(child, self.mapper, associationrow, "old_", self.prop.secondary_synchronize_pairs) secondary_update.append(associationrow) if processed is not None: processed.update(tmp) self._run_crud(uowcommit, secondary_insert, secondary_update, secondary_delete)
def process_dependencies(self, task, deplist, uowcommit, delete=False): connection = uowcommit.transaction.connection(self.mapper) secondary_delete = [] secondary_insert = [] secondary_update = [] if delete: for state in deplist: history = uowcommit.get_attribute_history( state, self.key, passive=self.passive_deletes) if history: for child in history.non_added(): if child is None or self._check_reverse_action( uowcommit, child, state, "manytomany"): continue associationrow = {} self._synchronize(state, child, associationrow, False, uowcommit) secondary_delete.append(associationrow) self._performed_action(uowcommit, state, child, "manytomany") else: for state in deplist: history = uowcommit.get_attribute_history(state, self.key) if history: for child in history.added: if child is None or self._check_reverse_action( uowcommit, child, state, "manytomany"): continue associationrow = {} self._synchronize(state, child, associationrow, False, uowcommit) self._performed_action(uowcommit, state, child, "manytomany") secondary_insert.append(associationrow) for child in history.deleted: if child is None or self._check_reverse_action( uowcommit, child, state, "manytomany"): continue associationrow = {} self._synchronize(state, child, associationrow, False, uowcommit) self._performed_action(uowcommit, state, child, "manytomany") secondary_delete.append(associationrow) if not self.passive_updates and self._pks_changed( uowcommit, state): if not history: history = uowcommit.get_attribute_history( state, self.key, passive=False) for child in history.unchanged: associationrow = {} sync.update(state, self.parent, associationrow, "old_", self.prop.synchronize_pairs) sync.update(child, self.mapper, associationrow, "old_", self.prop.secondary_synchronize_pairs) #self.syncrules.update(associationrow, state, child, "old_") secondary_update.append(associationrow) if secondary_delete: statement = self.secondary.delete( sql.and_(*[ c == sql.bindparam(c.key, type_=c.type) for c in self.secondary.c if c.key in associationrow ])) result = connection.execute(statement, secondary_delete) if result.supports_sane_multi_rowcount( ) and result.rowcount != len(secondary_delete): raise exc.ConcurrentModificationError( "Deleted rowcount %d does not match number of " "secondary table rows deleted from table '%s': %d" % (result.rowcount, self.secondary.description, len(secondary_delete))) if secondary_update: statement = self.secondary.update( sql.and_(*[ c == sql.bindparam("old_" + c.key, type_=c.type) for c in self.secondary.c if c.key in associationrow ])) result = connection.execute(statement, secondary_update) if result.supports_sane_multi_rowcount( ) and result.rowcount != len(secondary_update): raise exc.ConcurrentModificationError( "Updated rowcount %d does not match number of " "secondary table rows updated from table '%s': %d" % (result.rowcount, self.secondary.description, len(secondary_update))) if secondary_insert: statement = self.secondary.insert() connection.execute(statement, secondary_insert)
def process_dependencies(self, task, deplist, uowcommit, delete = False): #print self.mapper.mapped_table.name + " " + self.key + " " + repr(len(deplist)) + " process_dep isdelete " + repr(delete) + " direction " + repr(self.direction) connection = uowcommit.transaction.connection(self.mapper) secondary_delete = [] secondary_insert = [] secondary_update = [] if self.prop._reverse_property: reverse_dep = getattr(self.prop._reverse_property, '_dependency_processor', None) else: reverse_dep = None if delete: for state in deplist: history = uowcommit.get_attribute_history(state, self.key, passive=self.passive_deletes) if history: for child in history.non_added(): if child is None or (reverse_dep and (reverse_dep, "manytomany", child, state) in uowcommit.attributes): continue associationrow = {} self._synchronize(state, child, associationrow, False, uowcommit) secondary_delete.append(associationrow) uowcommit.attributes[(self, "manytomany", state, child)] = True else: for state in deplist: history = uowcommit.get_attribute_history(state, self.key) if history: for child in history.added: if child is None or (reverse_dep and (reverse_dep, "manytomany", child, state) in uowcommit.attributes): continue associationrow = {} self._synchronize(state, child, associationrow, False, uowcommit) uowcommit.attributes[(self, "manytomany", state, child)] = True secondary_insert.append(associationrow) for child in history.deleted: if child is None or (reverse_dep and (reverse_dep, "manytomany", child, state) in uowcommit.attributes): continue associationrow = {} self._synchronize(state, child, associationrow, False, uowcommit) uowcommit.attributes[(self, "manytomany", state, child)] = True secondary_delete.append(associationrow) if not self.passive_updates and self._pks_changed(uowcommit, state): if not history: history = uowcommit.get_attribute_history(state, self.key, passive=False) for child in history.unchanged: associationrow = {} sync.update(state, self.parent, associationrow, "old_", self.prop.synchronize_pairs) sync.update(child, self.mapper, associationrow, "old_", self.prop.secondary_synchronize_pairs) #self.syncrules.update(associationrow, state, child, "old_") secondary_update.append(associationrow) if secondary_delete: # TODO: precompile the delete/insert queries? statement = self.secondary.delete(sql.and_(*[c == sql.bindparam(c.key, type_=c.type) for c in self.secondary.c if c.key in associationrow])) result = connection.execute(statement, secondary_delete) if result.supports_sane_multi_rowcount() and result.rowcount != len(secondary_delete): raise exc.ConcurrentModificationError("Deleted rowcount %d does not match number of secondary table rows deleted from table '%s': %d" % (result.rowcount, self.secondary.description, len(secondary_delete))) if secondary_update: statement = self.secondary.update(sql.and_(*[c == sql.bindparam("old_" + c.key, type_=c.type) for c in self.secondary.c if c.key in associationrow])) result = connection.execute(statement, secondary_update) if result.supports_sane_multi_rowcount() and result.rowcount != len(secondary_update): raise exc.ConcurrentModificationError("Updated rowcount %d does not match number of secondary table rows updated from table '%s': %d" % (result.rowcount, self.secondary.description, len(secondary_update))) if secondary_insert: statement = self.secondary.insert() connection.execute(statement, secondary_insert)
def process_dependencies(self, task, deplist, uowcommit, delete = False): connection = uowcommit.transaction.connection(self.mapper) secondary_delete = [] secondary_insert = [] secondary_update = [] if delete: for state in deplist: history = uowcommit.get_attribute_history(state, self.key, passive=self.passive_deletes) if history: for child in history.non_added(): if child is None or self._check_reverse_action(uowcommit, child, state, "manytomany"): continue associationrow = {} self._synchronize(state, child, associationrow, False, uowcommit) secondary_delete.append(associationrow) self._performed_action(uowcommit, state, child, "manytomany") else: for state in deplist: history = uowcommit.get_attribute_history(state, self.key) if history: for child in history.added: if child is None or self._check_reverse_action(uowcommit, child, state, "manytomany"): continue associationrow = {} self._synchronize(state, child, associationrow, False, uowcommit) self._performed_action(uowcommit, state, child, "manytomany") secondary_insert.append(associationrow) for child in history.deleted: if child is None or self._check_reverse_action(uowcommit, child, state, "manytomany"): continue associationrow = {} self._synchronize(state, child, associationrow, False, uowcommit) self._performed_action(uowcommit, state, child, "manytomany") secondary_delete.append(associationrow) if not self.passive_updates and self._pks_changed(uowcommit, state): if not history: history = uowcommit.get_attribute_history(state, self.key, passive=False) for child in history.unchanged: associationrow = {} sync.update(state, self.parent, associationrow, "old_", self.prop.synchronize_pairs) sync.update(child, self.mapper, associationrow, "old_", self.prop.secondary_synchronize_pairs) #self.syncrules.update(associationrow, state, child, "old_") secondary_update.append(associationrow) if secondary_delete: statement = self.secondary.delete(sql.and_(*[ c == sql.bindparam(c.key, type_=c.type) for c in self.secondary.c if c.key in associationrow ])) result = connection.execute(statement, secondary_delete) if result.supports_sane_multi_rowcount() and result.rowcount != len(secondary_delete): raise exc.ConcurrentModificationError("Deleted rowcount %d does not match number of " "secondary table rows deleted from table '%s': %d" % (result.rowcount, self.secondary.description, len(secondary_delete))) if secondary_update: statement = self.secondary.update(sql.and_(*[ c == sql.bindparam("old_" + c.key, type_=c.type) for c in self.secondary.c if c.key in associationrow ])) result = connection.execute(statement, secondary_update) if result.supports_sane_multi_rowcount() and result.rowcount != len(secondary_update): raise exc.ConcurrentModificationError("Updated rowcount %d does not match number of " "secondary table rows updated from table '%s': %d" % (result.rowcount, self.secondary.description, len(secondary_update))) if secondary_insert: statement = self.secondary.insert() connection.execute(statement, secondary_insert)
def process_dependencies(self, task, deplist, uowcommit, delete=False): # print self.mapper.mapped_table.name + " " + self.key + " " + repr(len(deplist)) + " process_dep isdelete " + repr(delete) + " direction " + repr(self.direction) connection = uowcommit.transaction.connection(self.mapper) secondary_delete = [] secondary_insert = [] secondary_update = [] if self.prop._reverse_property: reverse_dep = getattr(self.prop._reverse_property, "_dependency_processor", None) else: reverse_dep = None if delete: for state in deplist: history = uowcommit.get_attribute_history(state, self.key, passive=self.passive_deletes) if history: for child in history.non_added(): if child is None or ( reverse_dep and (reverse_dep, "manytomany", child, state) in uowcommit.attributes ): continue associationrow = {} self._synchronize(state, child, associationrow, False, uowcommit) secondary_delete.append(associationrow) uowcommit.attributes[(self, "manytomany", state, child)] = True else: for state in deplist: history = uowcommit.get_attribute_history(state, self.key) if history: for child in history.added: if child is None or ( reverse_dep and (reverse_dep, "manytomany", child, state) in uowcommit.attributes ): continue associationrow = {} self._synchronize(state, child, associationrow, False, uowcommit) uowcommit.attributes[(self, "manytomany", state, child)] = True secondary_insert.append(associationrow) for child in history.deleted: if child is None or ( reverse_dep and (reverse_dep, "manytomany", child, state) in uowcommit.attributes ): continue associationrow = {} self._synchronize(state, child, associationrow, False, uowcommit) uowcommit.attributes[(self, "manytomany", state, child)] = True secondary_delete.append(associationrow) if not self.passive_updates and self._pks_changed(uowcommit, state): if not history: history = uowcommit.get_attribute_history(state, self.key, passive=False) for child in history.unchanged: associationrow = {} sync.update(state, self.parent, associationrow, "old_", self.prop.synchronize_pairs) sync.update(child, self.mapper, associationrow, "old_", self.prop.secondary_synchronize_pairs) # self.syncrules.update(associationrow, state, child, "old_") secondary_update.append(associationrow) if secondary_delete: # TODO: precompile the delete/insert queries? statement = self.secondary.delete( sql.and_( *[c == sql.bindparam(c.key, type_=c.type) for c in self.secondary.c if c.key in associationrow] ) ) result = connection.execute(statement, secondary_delete) if result.supports_sane_multi_rowcount() and result.rowcount != len(secondary_delete): raise exc.ConcurrentModificationError( "Deleted rowcount %d does not match number of secondary table rows deleted from table '%s': %d" % (result.rowcount, self.secondary.description, len(secondary_delete)) ) if secondary_update: statement = self.secondary.update( sql.and_( *[ c == sql.bindparam("old_" + c.key, type_=c.type) for c in self.secondary.c if c.key in associationrow ] ) ) result = connection.execute(statement, secondary_update) if result.supports_sane_multi_rowcount() and result.rowcount != len(secondary_update): raise exc.ConcurrentModificationError( "Updated rowcount %d does not match number of secondary table rows updated from table '%s': %d" % (result.rowcount, self.secondary.description, len(secondary_update)) ) if secondary_insert: statement = self.secondary.insert() connection.execute(statement, secondary_insert)
def process_saves(self, uowcommit, states): secondary_delete = [] secondary_insert = [] secondary_update = [] processed = self._get_reversed_processed_set(uowcommit) for state in states: history = uowcommit.get_attribute_history(state, self.key) if history: for child in history.added: if child is None or \ (processed is not None and (state, child) in processed): continue associationrow = {} self._synchronize(state, child, associationrow, False, uowcommit) secondary_insert.append(associationrow) for child in history.deleted: if child is None or \ (processed is not None and (state, child) in processed) or \ not uowcommit.session._contains_state(child): continue associationrow = {} self._synchronize(state, child, associationrow, False, uowcommit) secondary_delete.append(associationrow) if processed is not None: processed.update((c, state) for c in history.added + history.deleted) if not self.passive_updates and \ self._pks_changed(uowcommit, state): if not history: history = uowcommit.get_attribute_history( state, self.key, passive=False) for child in history.unchanged: associationrow = {} sync.update(state, self.parent, associationrow, "old_", self.prop.synchronize_pairs) sync.update(child, self.mapper, associationrow, "old_", self.prop.secondary_synchronize_pairs) secondary_update.append(associationrow) self._run_crud(uowcommit, secondary_insert, secondary_update, secondary_delete)