1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Ensure that preventing writes is invoked before materialize_transactions consistently

This commit is contained in:
Ryuta Kamizono 2018-12-12 23:12:51 +09:00
parent 4dd06452ef
commit 7d83ef4f7b
2 changed files with 23 additions and 13 deletions

View file

@ -40,8 +40,6 @@ module ActiveRecord
end
def exec_query(sql, name = "SQL", binds = [], prepare: false)
materialize_transactions
if without_prepared_statement?(binds)
execute_and_free(sql, name) do |result|
if result
@ -62,8 +60,6 @@ module ActiveRecord
end
def exec_delete(sql, name = nil, binds = [])
materialize_transactions
if without_prepared_statement?(binds)
execute_and_free(sql, name) { @connection.affected_rows }
else
@ -126,6 +122,8 @@ module ActiveRecord
raise ActiveRecord::ReadOnlyError, "Write query attempted while in readonly mode: #{sql}"
end
materialize_transactions
# make sure we carry over any changes to ActiveRecord::Base.default_timezone that have been
# made since we established the connection
@connection.query_options[:database_timezone] = ActiveRecord::Base.default_timezone

View file

@ -166,9 +166,13 @@ module ActiveRecord
end
def test_errors_when_an_insert_query_is_called_while_preventing_writes
assert_raises(ActiveRecord::ReadOnlyError) do
@connection.while_preventing_writes do
@connection.insert("INSERT INTO subscribers(nick) VALUES ('138853948594')")
assert_no_queries do
assert_raises(ActiveRecord::ReadOnlyError) do
@connection.while_preventing_writes do
@connection.transaction do
@connection.insert("INSERT INTO subscribers(nick) VALUES ('138853948594')", nil, false)
end
end
end
end
end
@ -176,9 +180,13 @@ module ActiveRecord
def test_errors_when_an_update_query_is_called_while_preventing_writes
@connection.insert("INSERT INTO subscribers(nick) VALUES ('138853948594')")
assert_raises(ActiveRecord::ReadOnlyError) do
@connection.while_preventing_writes do
@connection.update("UPDATE subscribers SET nick = '9989' WHERE nick = '138853948594'")
assert_no_queries do
assert_raises(ActiveRecord::ReadOnlyError) do
@connection.while_preventing_writes do
@connection.transaction do
@connection.update("UPDATE subscribers SET nick = '9989' WHERE nick = '138853948594'")
end
end
end
end
end
@ -186,9 +194,13 @@ module ActiveRecord
def test_errors_when_a_delete_query_is_called_while_preventing_writes
@connection.insert("INSERT INTO subscribers(nick) VALUES ('138853948594')")
assert_raises(ActiveRecord::ReadOnlyError) do
@connection.while_preventing_writes do
@connection.delete("DELETE FROM subscribers WHERE nick = '138853948594'")
assert_no_queries do
assert_raises(ActiveRecord::ReadOnlyError) do
@connection.while_preventing_writes do
@connection.transaction do
@connection.delete("DELETE FROM subscribers WHERE nick = '138853948594'")
end
end
end
end
end