From 4c531e8abd1cd83fb1c2fb9364d7378243cb2ebd Mon Sep 17 00:00:00 2001 From: Michael Krogh Date: Tue, 24 Jan 2012 10:11:36 +0100 Subject: [PATCH] Initial changes needed for supporting rails 3.2 --- Gemfile.lock | 27 ++++++++++++++++--- active_record_shards.gemspec | 4 +-- lib/active_record_shards.rb | 10 ++++++- .../configuration_parser.rb | 2 +- lib/active_record_shards/connection_pool.rb | 6 ++--- .../connection_specification.rb | 14 ++++++++++ 6 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 lib/active_record_shards/connection_specification.rb diff --git a/Gemfile.lock b/Gemfile.lock index 3afbcba..5e77b21 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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 diff --git a/active_record_shards.gemspec b/active_record_shards.gemspec index e5c3495..b9db464 100644 --- a/active_record_shards.gemspec +++ b/active_record_shards.gemspec @@ -3,7 +3,7 @@ $:.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" s.platform = Gem::Platform::RUBY s.authors = ["Mick Staugaard", "Eric Chapweske", "Ben Osheroff"] s.email = ["mick@staugaard.com", "eac@zendesk.com", "ben@gimbo.net"] @@ -11,7 +11,7 @@ Gem::Specification.new do |s| 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") diff --git a/lib/active_record_shards.rb b/lib/active_record_shards.rb index b3e06f9..1c6c938 100644 --- a/lib/active_record_shards.rb +++ b/lib/active_record_shards.rb @@ -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 diff --git a/lib/active_record_shards/configuration_parser.rb b/lib/active_record_shards/configuration_parser.rb index e658c0e..f7b685b 100644 --- a/lib/active_record_shards/configuration_parser.rb +++ b/lib/active_record_shards/configuration_parser.rb @@ -1,7 +1,7 @@ module ActiveRecordShards module ConfigurationParser module_function - + def explode(conf) conf.keys.each do |env_name| env_config = conf[env_name] diff --git a/lib/active_record_shards/connection_pool.rb b/lib/active_record_shards/connection_pool.rb index 118dbe9..61a8f32 100644 --- a/lib/active_record_shards/connection_pool.rb +++ b/lib/active_record_shards/connection_pool.rb @@ -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 diff --git a/lib/active_record_shards/connection_specification.rb b/lib/active_record_shards/connection_specification.rb new file mode 100644 index 0000000..e8b1a55 --- /dev/null +++ b/lib/active_record_shards/connection_specification.rb @@ -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