Wrap Mysql count of deleted rows in lock block to avoid conflict in test

This commit is contained in:
Quan Chau 2018-12-27 10:59:18 -08:00
parent 95a1d8721c
commit 66762b81f4
2 changed files with 31 additions and 1 deletions

View File

@ -61,7 +61,9 @@ module ActiveRecord
def exec_delete(sql, name = nil, binds = [])
if without_prepared_statement?(binds)
execute_and_free(sql, name) { @connection.affected_rows }
@lock.synchronize do
execute_and_free(sql, name) { @connection.affected_rows }
end
else
exec_stmt_and_free(sql, name, binds) { |stmt| stmt.affected_rows }
end

View File

@ -0,0 +1,28 @@
# frozen_string_literal: true
require "cases/helper"
require "support/connection_helper"
require "models/author"
require "models/bulb"
module ActiveRecord
class CountDeletedRowsWithLockTest < ActiveRecord::Mysql2TestCase
test "delete and create in different threads synchronize correctly" do
Bulb.unscoped.delete_all
Bulb.create!(name: "Jimmy", color: "blue")
delete_thread = Thread.new do
Bulb.unscoped.delete_all
end
create_thread = Thread.new do
Author.create!(name: "Tommy")
end
delete_thread.join
create_thread.join
assert_equal 1, delete_thread.value
end
end
end