A platform for community discussion. Free, open, simple.
Move into MiniSQLMultisiteConnection, and add test for rollback
David Taylor20a21b1240a1
diff --git a/app/helpers/transaction_helper.rb b/app/helpers/transaction_helper.rbdeleted file mode 100644index efb98d6a53e..00000000000--- a/app/helpers/transaction_helper.rb+++ /dev/null@@ -1,38 +0,0 @@-##-# Allows running arbitrary code after the current transaction has been committed.-# Works even with nested transactions. Useful for scheduling sidekiq jobs.-# Slightly simplified version of https://dev.to/evilmartians/rails-aftercommit-everywhere--4j9g-# Usage:-# Topic.transaction do-# puts "Some work before scheduling"-# TransactionHelper.after_commit do-# puts "Running after commit"-# end-# puts "Some work after scheduling"-# end-#-# Produces:-# > Some work before scheduling-# > Some work after scheduling-# > Running after commit--module TransactionHelper- class AfterCommitWrapper- def initialize- @callback = Proc.new- end-- def committed!(*)- @callback.call- end-- def before_committed!(*); end- def rolledback!(*); end- end-- def self.after_commit(&blk)- ActiveRecord::Base.connection.add_transaction_record(- AfterCommitWrapper.new(&blk)- )- end-enddiff --git a/app/models/topic.rb b/app/models/topic.rbindex 2aa41f6bb6b..94f2a8566fc 100644--- a/app/models/topic.rb+++ b/app/models/topic.rb@@ -686,7 +686,7 @@ class Topic < ActiveRecord::Baseif post = self.ordered_posts.firstnotified_user_ids = [post.user_id, post.last_editor_id].uniq- TransactionHelper.after_commit do+ DB.after_commit doJobs.enqueue(:notify_category_change, post_id: post.id, notified_user_ids: notified_user_ids)endenddiff --git a/lib/mini_sql_multisite_connection.rb b/lib/mini_sql_multisite_connection.rbindex c6842fe19ab..c628ef644b5 100644--- a/lib/mini_sql_multisite_connection.rb+++ b/lib/mini_sql_multisite_connection.rb@@ -23,6 +23,27 @@ class MiniSqlMultisiteConnection < MiniSql::Connectionendend+ class AfterCommitWrapper+ def initialize+ @callback = Proc.new+ end++ def committed!(*)+ @callback.call+ end++ def before_committed!(*); end+ def rolledback!(*); end+ end++ # Allows running arbitrary code after the current transaction has been committed.+ # Works even with nested transactions. Useful for scheduling sidekiq jobs.+ def after_commit(&blk)+ ActiveRecord::Base.connection.add_transaction_record(+ AfterCommitWrapper.new(&blk)+ )+ end+def self.instancenew(nil, param_encoder: ParamEncoder.new)enddiff --git a/spec/helpers/transaction_helper_spec.rb b/spec/helpers/transaction_helper_spec.rbdeleted file mode 100644index 01908b15aaa..00000000000--- a/spec/helpers/transaction_helper_spec.rb+++ /dev/null@@ -1,32 +0,0 @@-require 'rails_helper'--describe TransactionHelper do-- it "runs callbacks after outermost transaction is committed" do- outputString = "1"-- # Main transaction- ActiveRecord::Base.transaction do- outputString += "2"-- # Nested transaction- ActiveRecord::Base.transaction do- outputString += "3"-- TransactionHelper.after_commit do- outputString += "6"- end- outputString += "4"- end-- TransactionHelper.after_commit do- outputString += "7"- end-- outputString += "5"- end-- expect(outputString).to eq("1234567")- end--enddiff --git a/spec/lib/mini_sql_multisite_connection_spec.rb b/spec/lib/mini_sql_multisite_connection_spec.rbnew file mode 100644index 00000000000..c4213d9180d--- /dev/null+++ b/spec/lib/mini_sql_multisite_connection_spec.rb@@ -0,0 +1,52 @@+require 'rails_helper'++describe MiniSqlMultisiteConnection do++ describe "after_commit" do+ it "runs callbacks after outermost transaction is committed" do+ outputString = "1"++ # Main transaction+ ActiveRecord::Base.transaction do+ outputString += "2"++ # Nested transaction+ ActiveRecord::Base.transaction do+ outputString += "3"++ DB.after_commit do+ outputString += "6"+ end+ outputString += "4"+ end++ DB.after_commit do+ outputString += "7"+ end++ outputString += "5"+ end++ expect(outputString).to eq("1234567")+ end++ it "does not run if the transaction is rolled back" do+ outputString = "1"++ ActiveRecord::Base.transaction do+ outputString += "2"++ DB.after_commit do+ outputString += "4"+ end++ outputString += "3"++ raise ActiveRecord::Rollback+ end++ expect(outputString).to eq("123")+ end+ end++end