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

Update READ_QUERY regex

* The READ_QUERY regex would consider reads to be writes if they started with
    spaces or parens. For example, a UNION query might have parens around each
    SELECT - (SELECT ...) UNION (SELECT ...).
  * It will now correctly treat these queries as reads.
This commit is contained in:
Ali Ibrahim 2019-02-22 16:30:15 -05:00
parent f4bef91a31
commit b7eeb142af
4 changed files with 29 additions and 1 deletions

View file

@ -102,7 +102,7 @@ module ActiveRecord
end
def self.build_read_query_regexp(*parts) # :nodoc:
parts = parts.map { |part| /\A\s*#{part}/i }
parts = parts.map { |part| /\A[\(\s]*#{part}/i }
Regexp.union(*parts)
end

View file

@ -204,6 +204,14 @@ class Mysql2AdapterTest < ActiveRecord::Mysql2TestCase
end
end
def test_doesnt_error_when_a_read_query_with_leading_chars_is_called_while_preventing_writes
@conn.execute("INSERT INTO `engines` (`car_id`) VALUES ('138853948594')")
@conn.while_preventing_writes do
assert_equal 1, @conn.execute("(\n( SELECT `engines`.* FROM `engines` WHERE `engines`.`car_id` = '138853948594' ) )").entries.count
end
end
private
def with_example_table(definition = "id int auto_increment primary key, number int, data varchar(255)", &block)

View file

@ -432,6 +432,16 @@ module ActiveRecord
end
end
def test_doesnt_error_when_a_read_query_with_leading_chars_is_called_while_preventing_writes
with_example_table do
@connection.execute("INSERT INTO ex (data) VALUES ('138853948594')")
@connection.while_preventing_writes do
assert_equal 1, @connection.execute("(\n( SELECT * FROM ex WHERE data = '138853948594' ) )").entries.count
end
end
end
private
def with_example_table(definition = "id serial primary key, number integer, data character varying(255)", &block)

View file

@ -625,6 +625,16 @@ module ActiveRecord
end
end
def test_doesnt_error_when_a_read_query_with_leading_chars_is_called_while_preventing_writes
with_example_table "id int, data string" do
@conn.execute("INSERT INTO ex (data) VALUES ('138853948594')")
@conn.while_preventing_writes do
assert_equal 1, @conn.execute(" SELECT data from ex WHERE data = '138853948594'").count
end
end
end
private
def assert_logged(logs)