Skip to content

Commit

Permalink
[Rails 5] Add select_prepared to whitelist, duplicate 4.2 QueryCache …
Browse files Browse the repository at this point in the history
…behavior (#19)
  • Loading branch information
kyeah authored Oct 17, 2017
1 parent 1265d62 commit f4779ba
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
8 changes: 7 additions & 1 deletion lib/replica_pools/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@ class Engine < Rails::Engine
:select_rows, :select, :verify!, :raw_connection, :active?, :reconnect!,
:disconnect!, :reset_runtime, :log, :log_info
]
elsif [4, 5].include? ActiveRecord::VERSION::MAJOR
elsif ActiveRecord::VERSION::MAJOR == 4
[
:select_all, :select_one, :select_value, :select_values,
:select_rows, :select, :verify!, :raw_connection, :active?, :reconnect!,
:disconnect!, :reset_runtime, :log
]
elsif ActiveRecord::VERSION::MAJOR == 5
[
:select_all, :select_one, :select_value, :select_values,
:select_rows, :select, :select_prepared, :verify!, :raw_connection,
:active?, :reconnect!, :disconnect!, :reset_runtime, :log
]
else
warn "Unsupported ActiveRecord version #{ActiveRecord.version}. Please whitelist the safe methods."
end
Expand Down
33 changes: 21 additions & 12 deletions lib/replica_pools/query_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,29 @@ def #{method_name}(*a, &b)
# connection for cache logic, but ultimately pass its query
# through to whatever connection is current.
def select_all(*args)
# there are more args, but we only care about arel, name, and binds for caching
arel, name, binds = args
if query_cache_enabled && !locked?(arel)
sql = to_sql(arel, binds)
args[0] = sql

if ActiveRecord::VERSION::STRING >= '5.1'
cache_sql(sql, name, binds) { route_to(current, :select_all, *args) }
else
cache_sql(sql, binds) { route_to(current, :select_all, *args) }
end
# there may be more args for Rails 5.0+, but we only care about arel, name, and binds for caching.
relation, name, raw_binds = args

if !query_cache_enabled || locked?(relation)
return route_to(current, :select_all, *args)
end

# duplicate binds_from_relation behavior introduced in 4.2.
if raw_binds.blank? && relation.is_a?(ActiveRecord::Relation)
arel, binds = relation.arel, relation.bind_values
else
arel, binds = relation, raw_binds
end

sql = to_sql(arel, binds)

args[0] = sql
args[2] = binds

if Gem::Version.new(ActiveRecord.version) < Gem::Version.new('5.1')
cache_sql(sql, binds) { route_to(current, :select_all, *args) }
else
route_to(current, :select_all, *args)
cache_sql(sql, name, binds) { route_to(current, :select_all, *args) }
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/replica_pools/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ReplicaPools
VERSION = "2.1.0.rc1"
VERSION = "2.1.0"
end
2 changes: 0 additions & 2 deletions spec/query_cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
require_relative 'spec_helper'

describe ReplicaPools::QueryCache do

before(:each) do
@sql = 'SELECT NOW()'

Expand Down Expand Up @@ -104,5 +103,4 @@ def executor
end
end
end

end

0 comments on commit f4779ba

Please sign in to comment.