Skip to content

Commit

Permalink
Merge 76d0d58 into 95e4858
Browse files Browse the repository at this point in the history
  • Loading branch information
curi1119 committed Oct 23, 2018
2 parents 95e4858 + 76d0d58 commit f8343e4
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 7 deletions.
11 changes: 11 additions & 0 deletions .travis.yml
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
@@ -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
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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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 }
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
@@ -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"] }

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
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
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

0 comments on commit f8343e4

Please sign in to comment.