2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-07-24 04:41:55 -04:00
|
|
|
describe MiniSqlMultisiteConnection do
|
|
|
|
|
|
|
|
describe "after_commit" do
|
2020-12-07 19:03:31 -05:00
|
|
|
it "works for 'fake' (joinable) transactions" do
|
2018-07-24 04:41:55 -04:00
|
|
|
outputString = "1"
|
|
|
|
|
2018-07-24 05:49:20 -04:00
|
|
|
ActiveRecord::Base.transaction do
|
2018-07-24 04:41:55 -04:00
|
|
|
outputString += "2"
|
2020-12-07 19:03:31 -05:00
|
|
|
DB.exec("SELECT 1")
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
DB.exec("SELECT 2")
|
|
|
|
outputString += "3"
|
|
|
|
DB.after_commit { outputString += "6" }
|
|
|
|
outputString += "4"
|
|
|
|
end
|
|
|
|
DB.after_commit { outputString += "7" }
|
|
|
|
outputString += "5"
|
|
|
|
end
|
2018-07-24 04:41:55 -04:00
|
|
|
|
2020-12-07 19:03:31 -05:00
|
|
|
expect(outputString).to eq("1234567")
|
|
|
|
end
|
2018-07-24 04:41:55 -04:00
|
|
|
|
2020-12-07 19:03:31 -05:00
|
|
|
it "works for real (non-joinable) transactions" do
|
|
|
|
outputString = "1"
|
2018-07-24 04:41:55 -04:00
|
|
|
|
2020-12-07 19:03:31 -05:00
|
|
|
ActiveRecord::Base.transaction(requires_new: true, joinable: false) do
|
|
|
|
outputString += "2"
|
|
|
|
DB.exec("SELECT 1")
|
|
|
|
ActiveRecord::Base.transaction(requires_new: true) do
|
|
|
|
DB.exec("SELECT 2")
|
|
|
|
outputString += "3"
|
|
|
|
DB.after_commit { outputString += "6" }
|
|
|
|
outputString += "4"
|
2018-07-24 04:41:55 -04:00
|
|
|
end
|
2020-12-07 19:03:31 -05:00
|
|
|
DB.after_commit { outputString += "7" }
|
2018-07-24 04:41:55 -04:00
|
|
|
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
|
2020-05-01 11:37:43 -04:00
|
|
|
|
|
|
|
it "runs immediately if there is no transaction" do
|
|
|
|
outputString = "1"
|
|
|
|
|
|
|
|
DB.after_commit do
|
|
|
|
outputString += "2"
|
|
|
|
end
|
|
|
|
|
|
|
|
outputString += "3"
|
|
|
|
|
|
|
|
expect(outputString).to eq("123")
|
|
|
|
end
|
|
|
|
|
2021-03-23 17:48:04 -04:00
|
|
|
it "supports prepared statements" do
|
|
|
|
DB.prepared.query("SELECT ?", 1)
|
|
|
|
DB.prepared.query("SELECT ?", 2)
|
|
|
|
end
|
|
|
|
|
2018-07-24 04:41:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|