mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
parent
bad1041b82
commit
bde898c1df
3 changed files with 46 additions and 1 deletions
|
@ -609,6 +609,10 @@ module ActiveRecord
|
|||
FEATURE_NOT_SUPPORTED = "0A000" #:nodoc:
|
||||
|
||||
def execute_and_clear(sql, name, binds, prepare: false)
|
||||
if preventing_writes? && write_query?(sql)
|
||||
raise ActiveRecord::ReadOnlyError, "Write query attempted while in readonly mode: #{sql}"
|
||||
end
|
||||
|
||||
if without_prepared_statement?(binds)
|
||||
result = exec_no_cache(sql, name, [])
|
||||
elsif !prepare
|
||||
|
|
|
@ -209,7 +209,7 @@ module ActiveRecord
|
|||
# DATABASE STATEMENTS ======================================
|
||||
#++
|
||||
|
||||
READ_QUERY = ActiveRecord::ConnectionAdapters::AbstractAdapter.build_read_query_regexp(:select) # :nodoc:
|
||||
READ_QUERY = ActiveRecord::ConnectionAdapters::AbstractAdapter.build_read_query_regexp(:select, :pragma) # :nodoc:
|
||||
private_constant :READ_QUERY
|
||||
|
||||
def write_query?(sql) # :nodoc:
|
||||
|
@ -222,6 +222,10 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def exec_query(sql, name = nil, binds = [], prepare: false)
|
||||
if preventing_writes? && write_query?(sql)
|
||||
raise ActiveRecord::ReadOnlyError, "Write query attempted while in readonly mode: #{sql}"
|
||||
end
|
||||
|
||||
materialize_transactions
|
||||
|
||||
type_casted_binds = type_casted_binds(binds)
|
||||
|
|
|
@ -165,6 +165,43 @@ module ActiveRecord
|
|||
end
|
||||
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')")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
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'")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
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'")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_doesnt_error_when_a_select_query_is_called_while_preventing_writes
|
||||
@connection.insert("INSERT INTO subscribers(nick) VALUES ('138853948594')")
|
||||
|
||||
@connection.while_preventing_writes do
|
||||
result = @connection.select_all("SELECT subscribers.* FROM subscribers WHERE nick = '138853948594'")
|
||||
assert_equal 1, result.length
|
||||
end
|
||||
end
|
||||
|
||||
def test_uniqueness_violations_are_translated_to_specific_exception
|
||||
@connection.execute "INSERT INTO subscribers(nick) VALUES('me')"
|
||||
error = assert_raises(ActiveRecord::RecordNotUnique) do
|
||||
|
|
Loading…
Reference in a new issue