提交 82c28ec0 编写于 作者: R Ryuta Kamizono

Merge pull request #36932 from kirs/mysql2-filsort-abort

Improve MySQL error detection
上级 c3841e3e
* Improve detection of ActiveRecord::StatementTimeout with mysql2 adapter in the edge case when the query is terminated during filesort.
*Kir Shatrov*
## Rails 6.0.0 (August 16, 2019) ##
* Preserve user supplied joins order as much as possible.
......
......@@ -581,6 +581,7 @@ def extract_precision(sql_type)
end
# See https://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html
ER_FILSORT_ABORT = 1028
ER_DUP_ENTRY = 1062
ER_NOT_NULL_VIOLATION = 1048
ER_NO_REFERENCED_ROW = 1216
......@@ -622,7 +623,7 @@ def translate_exception(exception, message:, sql:, binds:)
Deadlocked.new(message, sql: sql, binds: binds)
when ER_LOCK_WAIT_TIMEOUT
LockWaitTimeout.new(message, sql: sql, binds: binds)
when ER_QUERY_TIMEOUT
when ER_QUERY_TIMEOUT, ER_FILSORT_ABORT
StatementTimeout.new(message, sql: sql, binds: binds)
when ER_QUERY_INTERRUPTED
QueryCanceled.new(message, sql: sql, binds: binds)
......
......@@ -225,6 +225,21 @@ def test_doesnt_error_when_a_read_query_with_leading_chars_is_called_while_preve
end
end
def test_statement_timeout_error_codes
raw_conn = @conn.raw_connection
assert_raises(ActiveRecord::StatementTimeout) do
raw_conn.stub(:query, ->(_sql) { raise Mysql2::Error.new("fail", 50700, ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter::ER_FILSORT_ABORT) }) {
@conn.execute("SELECT 1")
}
end
assert_raises(ActiveRecord::StatementTimeout) do
raw_conn.stub(:query, ->(_sql) { raise Mysql2::Error.new("fail", 50700, ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter::ER_QUERY_TIMEOUT) }) {
@conn.execute("SELECT 1")
}
end
end
private
def with_example_table(definition = "id int auto_increment primary key, number int, data varchar(255)", &block)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册