Skip to content

Commit a04b9a8

Browse files
committed
Add an option database_type
Problem: When using `numeric_ordering: true`, closure_tree tries to find out the type of database during the initialization process. This can be problematic, for example when doing `rails db:create`, in which case there is no database, yet. Another scenario is `rails asssets:prcompile` in a Docker container that is being made for production, but it doesn't have access to the production database yet, because it isn't rolled out, yet. Solution: I have added an option to specify the database_type manually. This option is *only* used then numeric_ordering is set to `true`
1 parent 22bbbf1 commit a04b9a8

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

lib/closure_tree/has_closure_tree.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ def has_closure_tree(options = {})
1111
:dont_order_roots,
1212
:numeric_order,
1313
:touch,
14-
:with_advisory_lock
14+
:with_advisory_lock,
15+
:database_type
1516
)
1617

1718
class_attribute :_ct

lib/closure_tree/numeric_order_support.rb

+11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ def self.adapter_for_connection(connection)
1212
end
1313
end
1414

15+
def self.adapter_for_database_type(database_type)
16+
case database_type
17+
when :postgres
18+
::ClosureTree::NumericOrderSupport::PostgreSQLAdapter
19+
when :mysql
20+
::ClosureTree::NumericOrderSupport::MysqlAdapter
21+
else
22+
::ClosureTree::NumericOrderSupport::GenericAdapter
23+
end
24+
end
25+
1526
module MysqlAdapter
1627
def reorder_with_parent_id(parent_id, minimum_sort_order_value = nil)
1728
return if parent_id.nil? && dont_order_roots

lib/closure_tree/support.rb

+13-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ def initialize(model_class, options)
2727
}.merge(options)
2828
raise ArgumentError, "name_column can't be 'path'" if options[:name_column] == 'path'
2929
if order_is_numeric?
30-
extend NumericOrderSupport.adapter_for_connection(connection)
30+
database_type = database_type_from_options || database_type_from_connection
31+
extend NumericOrderSupport.adapter_for_database_type(database_type)
3132
end
3233
end
3334

@@ -170,5 +171,16 @@ def create(model_class, attributes)
170171
def create!(model_class, attributes)
171172
create(model_class, attributes).tap { |ea| ea.save! }
172173
end
174+
175+
def database_type_from_connection
176+
das = WithAdvisoryLock::DatabaseAdapterSupport.new(connection)
177+
if das.postgresql?
178+
:postgres
179+
elsif das.mysql?
180+
:mysql
181+
else
182+
:generic
183+
end
184+
end
173185
end
174186
end

lib/closure_tree/support_flags.rb

+4
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,9 @@ def has_inheritance_column?(hash = columns_hash)
3131
def has_name?
3232
model_class.new.attributes.include? options[:name_column]
3333
end
34+
35+
def database_type_from_options
36+
options[:database_type]
37+
end
3438
end
3539
end

0 commit comments

Comments
 (0)