Skip to content

Commit 95fee94

Browse files
authored
Merge pull request rails#34800 from mqchau/mysqlCountDeleteRowInLock
Wrap Mysql count of deleted rows in lock block to avoid conflict in test
2 parents a497ece + 66762b8 commit 95fee94

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ def exec_query(sql, name = "SQL", binds = [], prepare: false)
6161

6262
def exec_delete(sql, name = nil, binds = [])
6363
if without_prepared_statement?(binds)
64-
execute_and_free(sql, name) { @connection.affected_rows }
64+
@lock.synchronize do
65+
execute_and_free(sql, name) { @connection.affected_rows }
66+
end
6567
else
6668
exec_stmt_and_free(sql, name, binds) { |stmt| stmt.affected_rows }
6769
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
require "cases/helper"
4+
require "support/connection_helper"
5+
require "models/author"
6+
require "models/bulb"
7+
8+
module ActiveRecord
9+
class CountDeletedRowsWithLockTest < ActiveRecord::Mysql2TestCase
10+
test "delete and create in different threads synchronize correctly" do
11+
Bulb.unscoped.delete_all
12+
Bulb.create!(name: "Jimmy", color: "blue")
13+
14+
delete_thread = Thread.new do
15+
Bulb.unscoped.delete_all
16+
end
17+
18+
create_thread = Thread.new do
19+
Author.create!(name: "Tommy")
20+
end
21+
22+
delete_thread.join
23+
create_thread.join
24+
25+
assert_equal 1, delete_thread.value
26+
end
27+
end
28+
end

0 commit comments

Comments
 (0)