Skip to content

Commit

Permalink
Add an option database_type
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
markusherzog committed May 4, 2021
1 parent 22bbbf1 commit a04b9a8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/closure_tree/has_closure_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ def has_closure_tree(options = {})
:dont_order_roots,
:numeric_order,
:touch,
:with_advisory_lock
:with_advisory_lock,
:database_type
)

class_attribute :_ct
Expand Down
11 changes: 11 additions & 0 deletions lib/closure_tree/numeric_order_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ def self.adapter_for_connection(connection)
end
end

def self.adapter_for_database_type(database_type)
case database_type
when :postgres
::ClosureTree::NumericOrderSupport::PostgreSQLAdapter
when :mysql
::ClosureTree::NumericOrderSupport::MysqlAdapter
else
::ClosureTree::NumericOrderSupport::GenericAdapter
end
end

module MysqlAdapter
def reorder_with_parent_id(parent_id, minimum_sort_order_value = nil)
return if parent_id.nil? && dont_order_roots
Expand Down
14 changes: 13 additions & 1 deletion lib/closure_tree/support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def initialize(model_class, options)
}.merge(options)
raise ArgumentError, "name_column can't be 'path'" if options[:name_column] == 'path'
if order_is_numeric?
extend NumericOrderSupport.adapter_for_connection(connection)
database_type = database_type_from_options || database_type_from_connection
extend NumericOrderSupport.adapter_for_database_type(database_type)
end
end

Expand Down Expand Up @@ -170,5 +171,16 @@ def create(model_class, attributes)
def create!(model_class, attributes)
create(model_class, attributes).tap { |ea| ea.save! }
end

def database_type_from_connection
das = WithAdvisoryLock::DatabaseAdapterSupport.new(connection)
if das.postgresql?
:postgres
elsif das.mysql?
:mysql
else
:generic
end
end
end
end
4 changes: 4 additions & 0 deletions lib/closure_tree/support_flags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,9 @@ def has_inheritance_column?(hash = columns_hash)
def has_name?
model_class.new.attributes.include? options[:name_column]
end

def database_type_from_options
options[:database_type]
end
end
end

0 comments on commit a04b9a8

Please sign in to comment.