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

Rails 6 support #1

Open
wants to merge 2 commits 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
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ Querrel makes it easy to query multiple databases in parallel (threads) using Ac

## Installation

Querrel version >= 2.0 is required for Rails 6 (and does not support earlier versions). For Rails versions 5.x and earlier, use version 1.4.

Add this line to your application's Gemfile:

gem 'querrel'
gem 'querrel', '~> 2.0' # Rails 6

or

gem 'querrel', '~> 1.4' # Rails 5 and earlier

And then execute:

Expand Down
27 changes: 14 additions & 13 deletions lib/querrel/connection_resolver.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
module Querrel
class ConnectionResolver
attr_accessor :resolver

def initialize(conns, db_names)
if db_names
base_spec = ActiveRecord::Base.connection_config
base_spec = ActiveRecord::Base.connection_db_config.configuration_hash

specs = conns.map do |c|
[c, base_spec.dup.update(database: c)]
[ c.to_s, base_spec.dup.update(database: c) ]
end
specs = Hash[specs]
@specs = Hash[specs]
else
case conns
when Hash
specs = conns
@specs = conns
when Array
conns.map!(&:to_s)
specs = ActiveRecord::Base.configurations.select{ |n, _| conns.include?(n.to_s) }
specs = conns.map do |c|
[ c.to_s, ActiveRecord::Base.configurations.find_db_config(c).configuration_hash ]
end
@specs = Hash[specs]
end
end
end

@resolver = ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new(specs)
def resolve(db)
@specs[db.to_s]
end

[:configurations, :spec, :resolve].each do |m|
define_method(m) do |*args, &block|
@resolver.send(m, *args, &block)
end
def configurations
@specs
end
end
end
end
14 changes: 1 addition & 13 deletions lib/querrel/querreller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,8 @@ def run(options = {}, &blk)
pool.do_your_thang!
end

def retrieve_connection_spec(db, resolver)
resolver.spec(db.to_sym)
end

def retrieve_connection_config(db, resolver)
resolver.resolve(db.to_sym)
end

def while_connected_to(db, resolver, &b)
conf = resolver.spec(db.to_sym)
pool = ActiveRecord::ConnectionAdapters::ConnectionPool.new(conf)
pool.with_connection(&b)
ensure
pool.disconnect!
end
end
end
end
2 changes: 1 addition & 1 deletion lib/querrel/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Querrel
VERSION = "1.4.0"
VERSION = "2.0.0"
end
4 changes: 2 additions & 2 deletions querrel.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_runtime_dependency "activerecord", ">= 4.0"
spec.add_runtime_dependency "activerecord", "~> 6.0"

spec.add_development_dependency "bundler", "~> 1.6"
spec.add_development_dependency "bundler", ">= 2.0"
spec.add_development_dependency "rake"
spec.add_development_dependency "sqlite3", "~> 1.3.5"
spec.add_development_dependency "database_rewinder", "~> 0"
Expand Down
4 changes: 2 additions & 2 deletions test/instance_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_runner

@q.run do
s.synchronize do
configs_actual << Product.connection_config
configs_actual << Product.connection_db_config.configuration_hash
end
end

Expand All @@ -59,4 +59,4 @@ def test_runner

assert_equal configs, configs_actual
end
end
end
4 changes: 2 additions & 2 deletions test/non_instance_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_runner
configs_actual = []
Querrel.run(on: @dbs) do
s.synchronize do
configs_actual << Product.connection_config
configs_actual << Product.connection_db_config.configuration_hash
end
end

Expand All @@ -59,4 +59,4 @@ def test_runner

assert_equal configs, configs_actual
end
end
end
12 changes: 7 additions & 5 deletions test/setup/active_record_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
ActiveRecord::Base.logger = Logger.new(File.join(File.dirname(__FILE__), "debug.log"))

# load schema and fixtures to each db
ActiveRecord::Base.configurations.keys.each do |c|
ActiveRecord::Base.establish_connection(c.to_sym)
ActiveRecord::Base.configurations.configurations.each do |config|
ActiveRecord::Base.establish_connection(config)
ActiveRecord::Migration.suppress_messages do
load('schema.rb')
end
Expand All @@ -21,8 +21,10 @@

require "active_support"
require "database_rewinder"
require "database_rewinder/cleaner"

# configure DatabaseRewinder
ActiveRecord::Base.configurations.keys.each do |c|
DatabaseRewinder[c]
end
DatabaseRewinder.init
ActiveRecord::Base.configurations.configurations.each do |config|
DatabaseRewinder[config.env_name]
end