aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2019-03-10 02:06:48 +0900
committerRyuta Kamizono <kamipo@gmail.com>2019-03-10 03:10:51 +0900
commit1cd150d2417ff750d75002e2fe4866cd1c651896 (patch)
treeb40dbd1aa2b7e7c5a1b657c6550ebb0f8ec8e71c /activerecord
parent6fc1bd6eea4373d869acc25354e5b4adfecc5758 (diff)
downloadrails-1cd150d2417ff750d75002e2fe4866cd1c651896.tar.gz
rails-1cd150d2417ff750d75002e2fe4866cd1c651896.tar.bz2
rails-1cd150d2417ff750d75002e2fe4866cd1c651896.zip
Fix `reconnect!` to work after `disconnect!`
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb2
-rw-r--r--activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb18
-rw-r--r--activerecord/test/cases/adapter_test.rb16
3 files changed, 25 insertions, 11 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
index 05d8d5ac00..3990c0a71a 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -281,6 +281,8 @@ module ActiveRecord
super
@connection.reset
configure_connection
+ rescue PG::ConnectionBad
+ connect
end
end
diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
index cb3d34a740..5e9b3afae4 100644
--- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
@@ -95,8 +95,6 @@ module ActiveRecord
def initialize(connection, logger, connection_options, config)
super(connection, logger, config)
-
- @active = true
configure_connection
end
@@ -144,14 +142,18 @@ module ActiveRecord
alias supports_insert_conflict_target? supports_insert_on_conflict?
def active?
- @active
+ !@connection.closed?
+ end
+
+ def reconnect!
+ super
+ connect if @connection.closed?
end
# Disconnects from the database if already connected. Otherwise, this
# method does nothing.
def disconnect!
super
- @active = false
@connection.close rescue nil
end
@@ -611,6 +613,14 @@ module ActiveRecord
StatementPool.new(self.class.type_cast_config_to_integer(@config[:statement_limit]))
end
+ def connect
+ @connection = ::SQLite3::Database.new(
+ @config[:database].to_s,
+ @config.merge(results_as_hash: true)
+ )
+ configure_connection
+ end
+
def configure_connection
execute("PRAGMA foreign_keys = ON", "SCHEMA")
end
diff --git a/activerecord/test/cases/adapter_test.rb b/activerecord/test/cases/adapter_test.rb
index 2baf3db49a..03bd9fd384 100644
--- a/activerecord/test/cases/adapter_test.rb
+++ b/activerecord/test/cases/adapter_test.rb
@@ -452,16 +452,16 @@ module ActiveRecord
class AdapterTestWithoutTransaction < ActiveRecord::TestCase
self.use_transactional_tests = false
- class Klass < ActiveRecord::Base
- end
-
def setup
- Klass.establish_connection :arunit
- @connection = Klass.connection
+ @connection = ActiveRecord::Base.connection
end
- teardown do
- Klass.remove_connection
+ test "reconnect after a disconnect" do
+ assert_predicate @connection, :active?
+ @connection.disconnect!
+ assert_not_predicate @connection, :active?
+ @connection.reconnect!
+ assert_predicate @connection, :active?
end
unless in_memory_db?
@@ -477,6 +477,8 @@ module ActiveRecord
assert_predicate @connection, :transaction_open?
@connection.disconnect!
assert_not_predicate @connection, :transaction_open?
+ ensure
+ @connection.reconnect!
end
end