Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use with_connection where possible #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions lib/counter_culture/counter.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require_relative './with_connection'

module CounterCulture
class Counter
CONFIG_OPTIONS = [ :column_names, :counter_cache_name, :delta_column, :foreign_key_values, :touch, :delta_magnitude, :execute_after_commit ]
Expand Down Expand Up @@ -65,11 +67,13 @@ def change_counter_cache(obj, options)
# MySQL throws an ambiguous column error if any joins are present and we don't include the
# table name. We isolate this change to MySQL because sqlite has the opposite behavior and
# throws an exception if the table name is present after UPDATE.
quoted_column = if klass.connection.adapter_name == 'Mysql2'
"#{klass.quoted_table_name}.#{model.connection.quote_column_name(change_counter_column)}"
else
"#{model.connection.quote_column_name(change_counter_column)}"
end
quoted_column = WithConnection.new(klass).call do |connection|
if connection.adapter_name == 'Mysql2'
"#{klass.quoted_table_name}.#{connection.quote_column_name(change_counter_column)}"
else
"#{connection.quote_column_name(change_counter_column)}"
end
end

column_type = klass.type_for_attribute(change_counter_column).type

Expand Down
6 changes: 5 additions & 1 deletion lib/counter_culture/reconciler.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'active_support/core_ext/module/delegation'
require 'active_support/core_ext/module/attribute_accessors'

require_relative './with_connection'

module CounterCulture
class Reconciler
ACTIVE_RECORD_VERSION = Gem.loaded_specs["activerecord"].version
Expand Down Expand Up @@ -312,7 +314,9 @@ def join_clauses(where)
# using Postgres with schema-namespaced tables. But then it's required,
# and otherwise it's just a no-op, so why not do it?
def quote_table_name(table_name)
relation_class.connection.quote_table_name(table_name)
WithConnection.new(relation_class).call do |connection|
connection.quote_table_name(table_name)
end
end

def parameterize(string)
Expand Down
33 changes: 33 additions & 0 deletions lib/counter_culture/with_connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module CounterCulture
class WithConnection
def initialize(recipient)
@recipient = recipient
end

attr_reader :recipient

def call
if rails_7_2_or_greater?
recipient.with_connection do |connection|
yield connection
end
elsif rails_7_1?
recipient.connection_pool.with_connection do |connection|
yield connection
end
else
yield recipient.connection
end
end

private

def rails_7_1?
Gem::Requirement.new('~> 7.1.0').satisfied_by?(Gem::Version.new(Rails.version))
end

def rails_7_2_or_greater?
Gem::Requirement.new('>= 7.2.0').satisfied_by?(Gem::Version.new(Rails.version))
end
end
end