diff --git a/lib/active_graph/node/query/query_proxy_methods_of_mass_updating.rb b/lib/active_graph/node/query/query_proxy_methods_of_mass_updating.rb index e3d1ae71a..14065a73f 100644 --- a/lib/active_graph/node/query/query_proxy_methods_of_mass_updating.rb +++ b/lib/active_graph/node/query/query_proxy_methods_of_mass_updating.rb @@ -50,6 +50,7 @@ def delete_all_rels # Executed in the database, callbacks will not be run. def replace_with(node_or_nodes) node_or_nodes = Array(node_or_nodes).map { |arg| arg.is_a?(ActiveGraph::Node) ? arg : @model.find(arg) } + ActiveGraph::Base.lock_node(start_object) unless start_object.new_record? original_ids = self.pluck(:id) delete_rels_for_nodes(original_ids, node_or_nodes.collect(&:id)) add_rels(node_or_nodes, original_ids) @@ -62,12 +63,13 @@ def add_rels(node_or_nodes, original_ids) end def delete_rels_for_nodes(original_ids, new_ids) - ids = original_ids.select { |id| !new_ids.include?(id) } - return unless ids.present? + ids_to_be_removed = original_ids - new_ids + return unless ids_to_be_removed.present? + if association.dependent - start_object.public_send("dependent_#{association.dependent}_callback", association, ids) + start_object.public_send("dependent_#{association.dependent}_callback", association, ids_to_be_removed) else - self.where(id: ids).delete_all_rels + self.where(id: ids_to_be_removed).delete_all_rels end end diff --git a/lib/active_graph/transactions.rb b/lib/active_graph/transactions.rb index 44f01cf2e..3ba5e888f 100644 --- a/lib/active_graph/transactions.rb +++ b/lib/active_graph/transactions.rb @@ -28,6 +28,10 @@ def read_transaction(**config, &block) alias transaction write_transaction + def lock_node(node) + node.as(:n).query.remove('n._AGLOCK_').exec if tx&.open? + end + private def send_transaction(method, **config, &block)