From 6f92c40d3b5bf6ef65f4cc36645d28018450c878 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Wed, 2 Sep 2020 13:17:17 +0900 Subject: [PATCH] Revert "Raise ConnectionNotEstablished rather than StatementInvalid in Mysql2Adapter#quote_string" --- .../connection_adapters/abstract_mysql_adapter.rb | 6 ------ .../connection_adapters/postgresql_adapter.rb | 6 ------ .../active_record/connection_adapters/sqlite3_adapter.rb | 9 ++++----- .../test/cases/adapters/mysql2/connection_test.rb | 4 ++-- activerecord/test/cases/disconnected_test.rb | 2 +- 5 files changed, 7 insertions(+), 20 deletions(-) diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb index f313abf4bc..754b26b7a3 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -650,12 +650,6 @@ module ActiveRecord 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 diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 7017412b07..d1c4cc48db 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -478,12 +478,6 @@ module ActiveRecord 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 diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb index 8964147a6c..0dcc8fd9f3 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb @@ -475,18 +475,17 @@ module ActiveRecord 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 diff --git a/activerecord/test/cases/adapters/mysql2/connection_test.rb b/activerecord/test/cases/adapters/mysql2/connection_test.rb index afe2cc68bb..e46c2a669e 100644 --- a/activerecord/test/cases/adapters/mysql2/connection_test.rb +++ b/activerecord/test/cases/adapters/mysql2/connection_test.rb @@ -58,7 +58,7 @@ class Mysql2ConnectionTest < ActiveRecord::Mysql2TestCase 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 @@ -67,7 +67,7 @@ class Mysql2ConnectionTest < ActiveRecord::Mysql2TestCase def test_quote_after_disconnect @connection.disconnect! - assert_raise(ActiveRecord::ConnectionNotEstablished) do + assert_raise(ActiveRecord::StatementInvalid) do @connection.quote("string") end end diff --git a/activerecord/test/cases/disconnected_test.rb b/activerecord/test/cases/disconnected_test.rb index 8b2a5e999a..a8fa37c7da 100644 --- a/activerecord/test/cases/disconnected_test.rb +++ b/activerecord/test/cases/disconnected_test.rb @@ -22,7 +22,7 @@ class TestDisconnectedAdapter < ActiveRecord::TestCase 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