Skip to content

Commit

Permalink
Revert "Raise ConnectionNotEstablished rather than StatementInvalid i…
Browse files Browse the repository at this point in the history
…n Mysql2Adapter#quote_string"
  • Loading branch information
kamipo committed Sep 2, 2020
1 parent b9571ae commit 6f92c40
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 20 deletions.
Expand Up @@ -650,12 +650,6 @@ def extract_precision(sql_type)

def translate_exception(exception, message:, sql:, binds:)
case error_number(exception)
when nil
if exception.message.match?(/MySQL client is not connected/i)
ConnectionNotEstablished.new(exception)
else
super
end
when ER_DB_CREATE_EXISTS
DatabaseAlreadyExists.new(message, sql: sql, binds: binds)
when ER_DUP_ENTRY
Expand Down
Expand Up @@ -478,12 +478,6 @@ def translate_exception(exception, message:, sql:, binds:)
return exception unless exception.respond_to?(:result)

case exception.result.try(:error_field, PG::PG_DIAG_SQLSTATE)
when nil
if exception.message.match?(/connection is closed/i)
ConnectionNotEstablished.new(exception)
else
super
end
when UNIQUE_VIOLATION
RecordNotUnique.new(message, sql: sql, binds: binds)
when FOREIGN_KEY_VIOLATION
Expand Down
Expand Up @@ -475,18 +475,17 @@ def copy_table_contents(from, to, columns, rename = {})
end

def translate_exception(exception, message:, sql:, binds:)
case exception.message
# SQLite 3.8.2 returns a newly formatted error message:
# UNIQUE constraint failed: *table_name*.*column_name*
# Older versions of SQLite return:
# column *column_name* is not unique
if exception.message.match?(/(column(s)? .* (is|are) not unique|UNIQUE constraint failed: .*)/i)
when /column(s)? .* (is|are) not unique/, /UNIQUE constraint failed: .*/
RecordNotUnique.new(message, sql: sql, binds: binds)
elsif exception.message.match?(/(.* may not be NULL|NOT NULL constraint failed: .*)/i)
when /.* may not be NULL/, /NOT NULL constraint failed: .*/
NotNullViolation.new(message, sql: sql, binds: binds)
elsif exception.message.match?(/FOREIGN KEY constraint failed/i)
when /FOREIGN KEY constraint failed/i
InvalidForeignKey.new(message, sql: sql, binds: binds)
elsif exception.message.match?(/called on a closed database/i)
ConnectionNotEstablished.new(exception)
else
super
end
Expand Down
4 changes: 2 additions & 2 deletions activerecord/test/cases/adapters/mysql2/connection_test.rb
Expand Up @@ -58,7 +58,7 @@ def test_successful_reconnection_after_timeout_with_verify
def test_execute_after_disconnect
@connection.disconnect!

error = assert_raise(ActiveRecord::ConnectionNotEstablished) do
error = assert_raise(ActiveRecord::StatementInvalid) do
@connection.execute("SELECT 1")
end
assert_kind_of Mysql2::Error, error.cause
Expand All @@ -67,7 +67,7 @@ def test_execute_after_disconnect
def test_quote_after_disconnect
@connection.disconnect!

assert_raise(ActiveRecord::ConnectionNotEstablished) do
assert_raise(ActiveRecord::StatementInvalid) do
@connection.quote("string")
end
end
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/disconnected_test.rb
Expand Up @@ -22,7 +22,7 @@ def setup
test "can't execute statements while disconnected" do
@connection.execute "SELECT count(*) from products"
@connection.disconnect!
assert_raises(ActiveRecord::ConnectionNotEstablished) do
assert_raises(ActiveRecord::StatementInvalid) do
silence_warnings do
@connection.execute "SELECT count(*) from products"
end
Expand Down

0 comments on commit 6f92c40

Please sign in to comment.