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

Fix connection #29

Open
wants to merge 8 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
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ gemfile:
- Gemfile
- ar41.gemfile
- ar416.gemfile
- ar42.gemfile
- ar50.gemfile
- ar51.gemfile
- ar52.gemfile
services:
- mysql
script:
Expand All @@ -18,5 +21,13 @@ matrix:
exclude:
- rvm: 2.1.8
gemfile: ar50.gemfile
- rvm: 2.1.8
gemfile: ar51.gemfile
- rvm: 2.1.8
gemfile: ar52.gemfile
- rvm: 2.0.0-p648
gemfile: ar50.gemfile
- rvm: 2.0.0-p648
gemfile: ar51.gemfile
- rvm: 2.0.0-p648
gemfile: ar52.gemfile
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source "https://rubygems.org"

gem "activerecord", ">= 4.2.0"

gem "activerecord", ">= 4.2.0", "< 5.0.0"
gem "mysql2", "~> 0.3.18"
# Specify your gem's dependencies in active_record_sharding.gemspec
gemspec
1 change: 0 additions & 1 deletion activerecord-sharding.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "bundler", "~> 1.5"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec"
spec.add_development_dependency "mysql2", "~> 0.3.18"
spec.add_development_dependency "pry"
spec.add_development_dependency "pry-byebug"
spec.add_development_dependency "awesome_print"
Expand Down
2 changes: 1 addition & 1 deletion ar41.gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source "https://rubygems.org"

gem "activerecord", ">= 4.1.0", "< 4.2.0"

gem "mysql2", "~> 0.3.18"
# Specify your gem's dependencies in active_record_sharding.gemspec
gemspec
2 changes: 1 addition & 1 deletion ar416.gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source "https://rubygems.org"

gem "activerecord", ">= 4.1.0", "< 4.1.7"

gem "mysql2", "~> 0.3.18"
# Specify your gem's dependencies in active_record_sharding.gemspec
gemspec
6 changes: 6 additions & 0 deletions ar42.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source "https://rubygems.org"

gem "activerecord", ">= 4.2.0", "< 5.0.0"
gem "mysql2", "~> 0.3.18"
# Specify your gem's dependencies in active_record_sharding.gemspec
gemspec
4 changes: 2 additions & 2 deletions ar50.gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source "https://rubygems.org"

gem "activerecord", ">= 5.0.0.beta3"

gem "activerecord", ">= 5.0.0", "< 5.1.0"
gem "mysql2", ">= 0.4.4", "< 0.6.0"
# Specify your gem's dependencies in active_record_sharding.gemspec
gemspec
6 changes: 6 additions & 0 deletions ar51.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source "https://rubygems.org"

gem "activerecord", ">= 5.1.0", "< 5.2.0"
gem "mysql2", ">= 0.4.4", "< 0.6.0"
# Specify your gem's dependencies in active_record_sharding.gemspec
gemspec
6 changes: 6 additions & 0 deletions ar52.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source "https://rubygems.org"

gem "activerecord", ">= 5.2.0"
gem "mysql2", ">= 0.4.4", "< 0.6.0"
# Specify your gem's dependencies in active_record_sharding.gemspec
gemspec
37 changes: 37 additions & 0 deletions lib/active_record/sharding/abstract_repository.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
module ActiveRecord
module Sharding
class AbstractRepository
Expand All @@ -7,6 +8,14 @@ def generate_model_for_shard(connection_name)
base_class_name = @base_class.name
class_name = generate_class_name connection_name

if ActiveRecord.version >= Gem::Version.new("5.0")
generate_model_for_shard_ar5(connection_name, base_class_name, class_name)
else
generate_model_for_shard_ar4(connection_name, base_class_name, class_name)
end
end

def generate_model_for_shard_ar4(connection_name, base_class_name, class_name)
model = Class.new(base_class) do
self.table_name = base_class.table_name

Expand All @@ -21,6 +30,34 @@ def self.name
model
end

def generate_model_for_shard_ar5(connection_name, base_class_name, class_name)
a_class = Class.new(base_class) do
module_eval <<-RUBY, __FILE__, __LINE__ + 1
def self.name
"#{class_name}"
end
RUBY
end
a_class.class_eval { abstract_class = true }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lint/UselessAssignment: Useless assignment to variable - abstract_class.

a_class.class_eval do
if ActiveRecord::Base.connection_handler.connection_pool_list.map { |c| c.spec.name }.include?(name)
self.connection_specification_name = name
else
establish_connection(connection_name)
end
end

model = Class.new(a_class) do
self.table_name = base_class.table_name
module_eval <<-RUBY, __FILE__, __LINE__ + 1
def self.name
"#{base_class_name}::#{class_name}"
end
RUBY
end
model
end

def generate_class_name(connection_name) # rubocop:disable Lint/UnusedMethodArgument
raise NotImplementedError, "#{self.class.name}.#{__method__} is an abstract method."
end
Expand Down
14 changes: 14 additions & 0 deletions spec/active_record/sharding/model_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
describe ActiveRecord::Sharding::Model do
before(:all) { ActiveRecord::Base.clear_all_connections! }

let!(:model) do
Class.new(ActiveRecord::Base) do
def self.name
Expand Down Expand Up @@ -35,6 +37,18 @@ def find_from_all_by_name(name)

let(:alice) { model.put! name: "Alice" }

context "ActiveRecord::ConnectionAdapters::ConnectionPool" do
let(:connection_names) { ["primary", "ShardForTestUser001", "ShardForTestUser002", "ShardForTestUser003", "SequencerForTestUserSequencer"] }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/WordArray: Use %w or %W for an array of words.


it "Create 1 connection for each DB" do
if ActiveRecord.version >= Gem::Version.new("5.0")
expect(ActiveRecord::Base.connection_handler.connection_pool_list.map { |c| c.spec.name }).to match_array(connection_names)
else
skip("This test is for AR5. ")
end
end
end

describe ".put!" do
it "example" do
expect(alice.persisted?).to be true
Expand Down
1 change: 1 addition & 0 deletions spec/active_record/sharding/sequencer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def self.name

context "forget insert sequencer record" do
before do
ActiveRecord::Base.clear_all_connections!
ActiveRecord::Sharding::DatabaseTasks.drop_sequencer_database sequencer_args
ActiveRecord::Sharding::DatabaseTasks.create_sequencer_database sequencer_args
ActiveRecord::Sharding::DatabaseTasks.create_table_sequencer_database sequencer_args
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
require "activerecord-sharding"
puts "======= ActiveRecord::VERSION::STRING #{ActiveRecord::VERSION::STRING} ============"

require "pry"
require "pry-byebug"
require "awesome_print"
Expand Down