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