Skip to content

Commit d147b95

Browse files
committed
fix
1 parent dee7b57 commit d147b95

File tree

9 files changed

+125
-120
lines changed

9 files changed

+125
-120
lines changed

Diff for: lib/closure_tree/has_closure_tree.rb

-4
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ def has_closure_tree(options = {})
3131

3232
include ClosureTree::DeterministicOrdering if _ct.order_option?
3333
include ClosureTree::NumericDeterministicOrdering if _ct.order_is_numeric?
34-
35-
connection_pool.release_connection
36-
rescue StandardError => e
37-
raise e unless ClosureTree.configuration.database_less
3834
end
3935

4036
alias_method :acts_as_tree, :has_closure_tree

Diff for: lib/closure_tree/has_closure_tree_root.rb

-2
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ def has_closure_tree_root(assoc_name, options = {})
7777

7878
@closure_tree_roots[assoc_name][assoc_map] = root
7979
end
80-
81-
connection_pool.release_connection
8280
end
8381
end
8482
end

Diff for: lib/closure_tree/numeric_order_support.rb

+40-38
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,42 @@
11
module ClosureTree
22
module NumericOrderSupport
3-
4-
def self.adapter_for_connection(connection)
5-
das = WithAdvisoryLock::DatabaseAdapterSupport.new(connection)
6-
if das.postgresql?
7-
::ClosureTree::NumericOrderSupport::PostgreSQLAdapter
8-
elsif das.mysql?
9-
::ClosureTree::NumericOrderSupport::MysqlAdapter
10-
else
11-
::ClosureTree::NumericOrderSupport::GenericAdapter
12-
end
13-
end
14-
153
module MysqlAdapter
16-
def reorder_with_parent_id(parent_id, minimum_sort_order_value = nil)
17-
return if parent_id.nil? && dont_order_roots
4+
module_function def reorder_with_parent_id(ct, parent_id, minimum_sort_order_value = nil)
5+
return if parent_id.nil? && ct.dont_order_roots
6+
187
min_where = if minimum_sort_order_value
19-
"AND #{quoted_order_column} >= #{minimum_sort_order_value}"
8+
"AND #{ct.quoted_order_column} >= #{minimum_sort_order_value}"
209
else
2110
""
2211
end
23-
connection.execute 'SET @i = 0'
24-
connection.execute <<-SQL.squish
25-
UPDATE #{quoted_table_name}
26-
SET #{quoted_order_column} = (@i := @i + 1) + #{minimum_sort_order_value.to_i - 1}
27-
WHERE #{where_eq(parent_column_name, parent_id)} #{min_where}
28-
ORDER BY #{nulls_last_order_by}
12+
ct.connection.execute 'SET @i = 0'
13+
ct.connection.execute <<-SQL.squish
14+
UPDATE #{ct.quoted_table_name}
15+
SET #{ct.quoted_order_column} = (@i := @i + 1) + #{minimum_sort_order_value.to_i - 1}
16+
WHERE #{ct.where_eq(ct.parent_column_name, parent_id)} #{min_where}
17+
ORDER BY #{ct.nulls_last_order_by}
2918
SQL
3019
end
3120
end
3221

3322
module PostgreSQLAdapter
34-
def reorder_with_parent_id(parent_id, minimum_sort_order_value = nil)
35-
return if parent_id.nil? && dont_order_roots
23+
module_function def reorder_with_parent_id(ct, parent_id, minimum_sort_order_value = nil)
24+
return if parent_id.nil? && ct.dont_order_roots
3625
min_where = if minimum_sort_order_value
37-
"AND #{quoted_order_column} >= #{minimum_sort_order_value}"
26+
"AND #{ct.quoted_order_column} >= #{minimum_sort_order_value}"
3827
else
3928
""
4029
end
41-
connection.execute <<-SQL.squish
42-
UPDATE #{quoted_table_name}
43-
SET #{quoted_order_column(false)} = t.seq + #{minimum_sort_order_value.to_i - 1}
30+
ct.connection.execute <<-SQL.squish
31+
UPDATE #{ct.quoted_table_name}
32+
SET #{ct.quoted_order_column(false)} = t.seq + #{minimum_sort_order_value.to_i - 1}
4433
FROM (
45-
SELECT #{quoted_id_column_name} AS id, row_number() OVER(ORDER BY #{order_by}) AS seq
46-
FROM #{quoted_table_name}
47-
WHERE #{where_eq(parent_column_name, parent_id)} #{min_where}
34+
SELECT #{ct.quoted_id_column_name} AS id, row_number() OVER(ORDER BY #{ct.order_by}) AS seq
35+
FROM #{ct.quoted_table_name}
36+
WHERE #{ct.where_eq(ct.parent_column_name, parent_id)} #{min_where}
4837
) AS t
49-
WHERE #{quoted_table_name}.#{quoted_id_column_name} = t.id and
50-
#{quoted_table_name}.#{quoted_order_column(false)} is distinct from t.seq + #{minimum_sort_order_value.to_i - 1}
38+
WHERE #{ct.quoted_table_name}.#{ct.quoted_id_column_name} = t.id and
39+
#{ct.quoted_table_name}.#{ct.quoted_order_column(false)} is distinct from t.seq + #{minimum_sort_order_value.to_i - 1}
5140
SQL
5241
end
5342

@@ -57,18 +46,31 @@ def rows_updated(result)
5746
end
5847

5948
module GenericAdapter
60-
def reorder_with_parent_id(parent_id, minimum_sort_order_value = nil)
61-
return if parent_id.nil? && dont_order_roots
62-
scope = model_class.
63-
where(parent_column_sym => parent_id).
64-
order(nulls_last_order_by)
49+
module_function def reorder_with_parent_id(ct, parent_id, minimum_sort_order_value = nil)
50+
return if parent_id.nil? && ct.dont_order_roots
51+
binding.irb
52+
scope = ct.
53+
where(ct.parent_column_sym => parent_id).
54+
order(ct.nulls_last_order_by)
6555
if minimum_sort_order_value
66-
scope = scope.where("#{quoted_order_column} >= #{minimum_sort_order_value}")
56+
scope = scope.where("#{ct.quoted_order_column} >= #{minimum_sort_order_value}")
6757
end
6858
scope.each_with_index do |ea, idx|
6959
ea.update_order_value(idx + minimum_sort_order_value.to_i)
7060
end
7161
end
7262
end
63+
64+
65+
module_function def adapter_for_connection(ct, parent_id, minimum_sort_order_value = nil)
66+
das = WithAdvisoryLock::DatabaseAdapterSupport.new(ct.connection)
67+
if das.postgresql?
68+
PostgreSQLAdapter.reorder_with_parent_id(ct, parent_id, minimum_sort_order_value = nil)
69+
elsif das.mysql?
70+
MysqlAdapter.reorder_with_parent_id(ct, parent_id, minimum_sort_order_value = nil)
71+
else
72+
GenericAdapter.reorder_with_parent_id(ct, parent_id, minimum_sort_order_value = nil)
73+
end
74+
end
7375
end
7476
end

Diff for: lib/closure_tree/support.rb

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ def initialize(model_class, options)
2626
:numeric_order => false
2727
}.merge(options)
2828
raise ArgumentError, "name_column can't be 'path'" if options[:name_column] == 'path'
29-
if order_is_numeric?
30-
extend NumericOrderSupport.adapter_for_connection(connection)
31-
end
3229
end
3330

3431
def hierarchy_class_for_model
@@ -54,6 +51,10 @@ def hash
5451
hierarchy_class
5552
end
5653

54+
def reorder_with_parent_id(parent_id, minimum_sort_order_value = nil)
55+
NumericOrderSupport.adapter_for_connection(self, parent_id, minimum_sort_order_value)
56+
end
57+
5758
def hierarchy_table_name
5859
# We need to use the table_name, not something like ct_class.to_s.demodulize + "_hierarchies",
5960
# because they may have overridden the table name, which is what we want to be consistent with

Diff for: spec/spec_helper.rb

+1-6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
ActiveRecord::Base.configurations = {
3030
default_env: {
3131
primary: {
32+
primary: true,
3233
url: primary_database_url,
3334
properties: { allowPublicKeyRetrieval: true } # for JRuby madness
3435
},
@@ -99,14 +100,8 @@ def sqlite?
99100
# Require our gem
100101
require 'closure_tree'
101102

102-
ActiveRecord::Tasks::DatabaseTasks.drop_current(:primary)
103-
ActiveRecord::Tasks::DatabaseTasks.create_current(:primary)
104-
ActiveRecord::Tasks::DatabaseTasks.drop_current(:secondary)
105-
ActiveRecord::Tasks::DatabaseTasks.create_current(:secondary)
106-
107103
# Load test helpers
108104
require_relative 'support/schema'
109-
require_relative 'support/models'
110105
require_relative 'support/helpers'
111106
require_relative 'support/exceed_query_limit'
112107
require_relative 'support/query_counter'

Diff for: spec/support/models.rb

+13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
# frozen_string_literal: true
22

3+
4+
5+
class ApplicationRecord < ActiveRecord::Base
6+
self.abstract_class = true
7+
8+
connects_to database: { writing: :primary, reading: :primary }
9+
end
10+
11+
class SecondDatabaseRecord < ActiveRecord::Base
12+
self.abstract_class = true
13+
14+
connects_to database: { writing: :secondary, reading: :secondary }
15+
end
316
class Tag < ApplicationRecord
417
has_closure_tree dependent: :destroy, order: :name
518
before_destroy :add_destroyed_tag

0 commit comments

Comments
 (0)