Skip to content
This repository was archived by the owner on Oct 31, 2018. It is now read-only.
This repository was archived by the owner on Oct 31, 2018. It is now read-only.

Doesn't work for nested transactions #10

@mrkamel

Description

@mrkamel

after_commit_queue doesn't seem to work for nested transactions, as it stores its queue within the model object and ActiveRecord only runs the after_commit hook once for each uniq record. See https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb#L80-L89
Thus, if an individual record is updated twice within a single transaction, the run_after_commit runs only once. Compare this overly simplified example:

class MyModel ...
  after_save do
    run_after_commit { puts "run after commit called" }
  end
end

MyModel.transaction do
  model1 = MyModel.find(1)
  model1.title = "1"
  model1.save!

  model2 = MyModel.find(1) # same "record"
  model2.title = "2"
  model2.save!
end

Output is only a single:

run after commit called

If we'd instead save the queue within the transaction object.

module AfterCommitQueue
  # ...

  def after_commit_queue
    self.class.connection.current_transaction.instance_variable_get("@run_after_commit_queue") || self.class.connection.current_transaction.instance_variable_set("@run_after_commit_queue", []) 
  end 

  def clear_after_commit_queue
    self.class.connection.current_transaction.instance_variable_set "@run_after_commit_queue", []
  end
end

It would work as IMO expected. What do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions