Skip to content

Commit

Permalink
Return database connection into pool after configuration
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Envek committed Apr 12, 2017
1 parent f03ebd7 commit c38e0f6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/closure_tree/has_closure_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.checkin(connection_pool.active_connection?) if connection_pool.active_connection?
rescue StandardError => e
raise e unless ClosureTree.configuration.database_less
end
Expand Down
2 changes: 2 additions & 0 deletions lib/closure_tree/has_closure_tree_root.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ def has_closure_tree_root(assoc_name, options = {})

@closure_tree_roots[assoc_name][assoc_map] = root
end

connection_pool.checkin(connection_pool.active_connection?) if connection_pool.active_connection?
end
end
end
27 changes: 27 additions & 0 deletions spec/pool_spec.rb
Original file line number Diff line number Diff line change
@@ -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_nil
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_nil
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_nil
end
end

0 comments on commit c38e0f6

Please sign in to comment.