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

Initial changes needed for supporting rails 3.2 #6

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
27 changes: 23 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
PATH
remote: .
specs:
active_record_shards (2.5.2)
activerecord (>= 2.3.5, < 3.3)

GEM
remote: http://rubygems.org/
specs:
activerecord (2.3.14)
activesupport (= 2.3.14)
activemodel (3.1.3)
activesupport (= 3.1.3)
builder (~> 3.0.0)
i18n (~> 0.6)
activerecord (3.1.3)
activemodel (= 3.1.3)
activesupport (= 3.1.3)
arel (~> 2.2.1)
tzinfo (~> 0.3.29)
activerecord-jdbc-adapter (1.0.3-java)
activerecord-jdbcmysql-adapter (1.0.3-java)
activerecord-jdbc-adapter (= 1.0.3)
jdbc-mysql (~> 5.0.0)
activesupport (2.3.14)
activesupport (3.1.3)
multi_json (~> 1.0)
archive-tar-minitar (0.5.2)
arel (2.2.1)
builder (3.0.0)
columnize (0.3.4)
i18n (0.6.0)
jdbc-mysql (5.0.4)
linecache (0.46)
rbx-require-relative (> 0.0.4)
linecache19 (0.5.12)
ruby_core_source (>= 0.1.4)
mocha (0.9.12)
multi_json (1.0.4)
mysql (2.8.1)
rake (0.9.2)
rbx-require-relative (0.0.5)
Expand All @@ -35,13 +53,14 @@ GEM
ruby_core_source (0.1.5)
archive-tar-minitar (>= 0.5.2)
shoulda (2.11.3)
tzinfo (0.3.31)

PLATFORMS
java
ruby

DEPENDENCIES
activerecord (~> 2.3.5)
active_record_shards!
activerecord-jdbcmysql-adapter (~> 1.0.3)
bundler
mocha
Expand Down
4 changes: 2 additions & 2 deletions active_record_shards.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ $:.push File.expand_path("../lib", __FILE__)

Gem::Specification.new do |s|
s.name = "active_record_shards"
s.version = "2.5.2"
s.version = "2.5.3"
Copy link
Owner

Choose a reason for hiding this comment

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

Never bump the version in a pull request

s.platform = Gem::Platform::RUBY
s.authors = ["Mick Staugaard", "Eric Chapweske", "Ben Osheroff"]
s.email = ["[email protected]", "[email protected]", "[email protected]"]
s.homepage = "http://github.com/staugaard/active_record_shards"
s.summary = "Simple database switching for ActiveRecord."
s.description = "Easily run queries on shard and slave databases."

s.add_runtime_dependency("activerecord", ">= 2.3.5", "< 3.2")
s.add_runtime_dependency("activerecord", ">= 2.3.5", "< 3.3")

s.add_development_dependency("rake")
s.add_development_dependency("bundler")
Expand Down
10 changes: 9 additions & 1 deletion lib/active_record_shards.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@
require 'active_record_shards/association_collection_connection_selection'
require 'active_record_shards/connection_pool'
require 'active_record_shards/migration'
if ActiveRecord::VERSION::MAJOR >= 3 && ActiveRecord::VERSION::MINOR >= 2
require 'active_record_shards/connection_specification'
end

ActiveRecord::Base.extend(ActiveRecordShards::ConfigurationParser)
ActiveRecord::Base.extend(ActiveRecordShards::Model)
ActiveRecord::Base.extend(ActiveRecordShards::ConnectionSwitcher)
ActiveRecord::Associations::AssociationCollection.send(:include, ActiveRecordShards::AssociationCollectionConnectionSelection)

if ActiveRecord::VERSION::MAJOR >= 3 && ActiveRecord::VERSION::MINOR >= 1
ActiveRecord::Associations::CollectionProxy.send(:include, ActiveRecordShards::AssociationCollectionConnectionSelection)
else
ActiveRecord::Associations::AssociationCollection.send(:include, ActiveRecordShards::AssociationCollectionConnectionSelection)
end

module ActiveRecordShards
def self.rails_env
Expand Down
2 changes: 1 addition & 1 deletion lib/active_record_shards/configuration_parser.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module ActiveRecordShards
module ConfigurationParser
module_function

def explode(conf)
conf.keys.each do |env_name|
env_config = conf[env_name]
Expand Down
6 changes: 3 additions & 3 deletions lib/active_record_shards/connection_pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
# The only difference here is that we use klass.connection_pool_name
# instead of klass.name as the pool key
def retrieve_connection_pool(klass)
pool = @connection_pools[klass.connection_pool_name]
pool = @class_to_pool[klass.connection_pool_name]
return pool if pool
return nil if ActiveRecord::Base == klass
retrieve_connection_pool klass.superclass
end

def remove_connection(klass)
pool = @connection_pools[klass.connection_pool_name]
@connection_pools.delete_if { |key, value| value == pool }
pool = @class_to_pool[klass.connection_pool_name]
@class_to_pool.delete_if { |key, value| value == pool }
pool.disconnect! if pool
pool.spec.config if pool
end
Expand Down
14 changes: 14 additions & 0 deletions lib/active_record_shards/connection_specification.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class ActiveRecord::Base

def self.establish_connection(spec = ENV["DATABASE_URL"])
resolver = ConnectionSpecification::Resolver.new spec, configurations
spec = resolver.spec

unless respond_to?(spec.adapter_method)
raise AdapterNotFound, "database configuration specifies nonexistent #{spec.config[:adapter]} adapter"
end

remove_connection
connection_handler.establish_connection connection_pool_name, spec
end
end