From 4ed2ea9c7a1c7ed5b43c287df6e3e02706dccfdb Mon Sep 17 00:00:00 2001 From: Andrey Novikov Date: Wed, 12 Apr 2017 20:31:25 +0300 Subject: [PATCH] Return database connection into pool after configuration When application code being loaded `has_closure_tree` method will get database connection implicitly and will not return it into the pool. In some circumstances this connection will not be available for request processing at runtime anymore. --- lib/closure_tree/has_closure_tree.rb | 2 ++ lib/closure_tree/has_closure_tree_root.rb | 2 ++ spec/pool_spec.rb | 27 +++++++++++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 spec/pool_spec.rb diff --git a/lib/closure_tree/has_closure_tree.rb b/lib/closure_tree/has_closure_tree.rb index 89a53f7d..0e7d61ac 100644 --- a/lib/closure_tree/has_closure_tree.rb +++ b/lib/closure_tree/has_closure_tree.rb @@ -29,6 +29,8 @@ def has_closure_tree(options = {}) include ClosureTree::DeterministicOrdering if _ct.order_option? include ClosureTree::NumericDeterministicOrdering if _ct.order_is_numeric? + + connection_pool.release_connection rescue StandardError => e raise e unless ClosureTree.configuration.database_less end diff --git a/lib/closure_tree/has_closure_tree_root.rb b/lib/closure_tree/has_closure_tree_root.rb index 56d5568f..5aa1e275 100644 --- a/lib/closure_tree/has_closure_tree_root.rb +++ b/lib/closure_tree/has_closure_tree_root.rb @@ -81,6 +81,8 @@ def has_closure_tree_root(assoc_name, options = {}) @closure_tree_roots[assoc_name][assoc_map] = root end + + connection_pool.release_connection end end end diff --git a/spec/pool_spec.rb b/spec/pool_spec.rb new file mode 100644 index 00000000..de25c4fb --- /dev/null +++ b/spec/pool_spec.rb @@ -0,0 +1,27 @@ +require 'spec_helper' + +describe 'Configuration' do + it 'returns connection to the pool after has_closure_tree setup' do + class TypeDuplicate < ActiveRecord::Base + self.table_name = "namespace_type#{table_name_suffix}" + has_closure_tree + end + expect(ActiveRecord::Base.connection_pool.active_connection?).to be_falsey # +false+ in AR 4, +nil+ in AR 5 + end + + it 'returns connection to the pool after has_closure_tree setup with order' do + class MetalDuplicate < ActiveRecord::Base + self.table_name = "#{table_name_prefix}metal#{table_name_suffix}" + has_closure_tree order: 'sort_order', name_column: 'value' + end + expect(ActiveRecord::Base.connection_pool.active_connection?).to be_falsey + end + + it 'returns connection to the pool after has_closure_tree_root setup' do + class GroupDuplicate < ActiveRecord::Base + self.table_name = "#{table_name_prefix}group#{table_name_suffix}" + has_closure_tree_root :root_user + end + expect(ActiveRecord::Base.connection_pool.active_connection?).to be_falsey + end +end