Permalink
Browse files

Remove AR2.1 and less support, code restructuring

  • Loading branch information...
1 parent 25eb9f3 commit 06be923a450945b048863bd2a8d5cb55fb4a23df @mperham mperham committed Nov 21, 2010
View
@@ -38,20 +38,12 @@
# end
module DataFabric
- # Set this logger to log DataFabric operations.
- # The logger should quack like a standard Ruby Logger.
- mattr_accessor :logger
-
- def self.init
- logger = ActiveRecord::Base.logger unless logger
- log { "Loading data_fabric #{DataFabric::Version::STRING} with ActiveRecord #{ActiveRecord::VERSION::STRING}" }
-
- if ActiveRecord::VERSION::STRING < '2.2.0'
- require 'data_fabric/ar20'
- else
- require 'data_fabric/ar22'
- end
- ActiveRecord::Base.send(:include, DataFabric::Extensions)
+ def self.logger
+ @logger ||= ActiveRecord::Base.logger
+ end
+
+ def self.logger=(log)
+ @logger = log
end
def self.activate_shard(shards, &block)
@@ -99,8 +91,7 @@ def self.ensure_setup
Thread.current[:shards] = {} unless Thread.current[:shards]
end
- def self.log(level=Logger::INFO, &block)
- logger && logger.add(level, &block)
- end
-
end
+
+require 'data_fabric/extensions'
+ActiveRecord::Base.send(:include, DataFabric::Extensions)
View
@@ -1,133 +0,0 @@
-module DataFabric
- module Extensions
- def self.included(model)
- # Wire up ActiveRecord::Base
- model.extend ClassMethods
- end
-
- # Class methods injected into ActiveRecord::Base
- module ClassMethods
- def data_fabric(options)
- proxy = DataFabric::ConnectionProxy.new(self, options)
- ActiveRecord::Base.active_connections[name] = proxy
-
- raise ArgumentError, "data_fabric does not support ActiveRecord's allow_concurrency = true" if allow_concurrency
- DataFabric.log { "Creating data_fabric proxy for class #{name}" }
- end
- end
- end
-
- class ConnectionProxy
- def initialize(model_class, options)
- @model_class = model_class
- @replicated = options[:replicated]
- @shard_group = options[:shard_by]
- @prefix = options[:prefix]
- @role = 'slave' if @replicated
-
- @model_class.send :include, ActiveRecordConnectionMethods if @replicated
- end
-
- delegate :insert, :update, :delete, :create_table, :rename_table, :drop_table, :add_column, :remove_column,
- :change_column, :change_column_default, :rename_column, :add_index, :remove_index, :initialize_schema_information,
- :dump_schema_information, :execute, :execute_ignore_duplicate, :to => :master
-
- delegate :insert_many, :to => :master # ar-extensions bulk insert support
-
- def transaction(start_db_transaction = true, &block)
- with_master { connection.transaction(start_db_transaction, &block) }
- end
-
- def method_missing(method, *args, &block)
- DataFabric.log(Logger::DEBUG) { "Calling #{method} on #{connection}" }
- connection.send(method, *args, &block)
- end
-
- def connection_name
- connection_name_builder.join('_')
- end
-
- def disconnect!
- if connected?
- connection.disconnect!
- cached_connections[connection_name] = nil
- end
- end
-
- def verify!(arg)
- connection.verify!(arg) if connected?
- end
-
- def with_master
- # Allow nesting of with_master.
- old_role = @role
- set_role('master')
- yield
- ensure
- set_role(old_role)
- end
-
- private
-
- def cached_connections
- @cached_connections ||= {}
- end
-
- def connection_name_builder
- @connection_name_builder ||= begin
- clauses = []
- clauses << @prefix if @prefix
- clauses << @shard_group if @shard_group
- clauses << StringProxy.new { DataFabric.active_shard(@shard_group) } if @shard_group
- clauses << RAILS_ENV
- clauses << StringProxy.new { @role } if @replicated
- clauses
- end
- end
-
- def connection
- name = connection_name
- if not connected?
- config = ActiveRecord::Base.configurations[name]
- raise ArgumentError, "Unknown database config: #{name}, have #{ActiveRecord::Base.configurations.inspect}" unless config
- DataFabric.log { "Connecting to #{name}" }
- @model_class.establish_connection(config)
- cached_connections[name] = @model_class.connection
- @model_class.active_connections[@model_class.name] = self
- end
- cached_connections[name].verify!(3600)
- cached_connections[name]
- end
-
- def connected?
- DataFabric.shard_active_for?(@shard_group) and cached_connections[connection_name]
- end
-
- def set_role(role)
- @role = role if @replicated
- end
-
- def master
- with_master { return connection }
- end
- end
-
- module ActiveRecordConnectionMethods
- def self.included(base)
- base.alias_method_chain :reload, :master
- end
-
- def reload_with_master(*args, &block)
- connection.with_master { reload_without_master }
- end
- end
-
- class StringProxy
- def initialize(&block)
- @proc = block
- end
- def to_s
- @proc.call
- end
- end
-end
@@ -1,47 +1,23 @@
+module DataFabric
+ module ActiveRecordConnectionMethods
+ def self.included(base)
+ base.alias_method_chain :reload, :master
+ end
-class ActiveRecord::ConnectionAdapters::ConnectionHandler
- def clear_active_connections_with_data_fabric!
- clear_active_connections_without_data_fabric!
- DataFabric::ConnectionProxy.shard_pools.each_value { |pool| pool.release_connection }
+ def reload_with_master(*args, &block)
+ connection.with_master { reload_without_master }
+ end
end
- alias_method_chain :clear_active_connections!, :data_fabric
-end
-module DataFabric
- module Extensions
- def self.included(model)
- # Wire up ActiveRecord::Base
- model.extend ClassMethods
- ConnectionProxy.shard_pools = {}
- end
-
- # Class methods injected into ActiveRecord::Base
- module ClassMethods
- def data_fabric(options)
- DataFabric.log { "Creating data_fabric proxy for class #{name}" }
- @proxy = DataFabric::ConnectionProxy.new(self, options)
-
- class << self
- def connection
- @proxy || superclass.connection
- end
-
- def connected?
- @proxy.connected?
- end
-
- def remove_connection(klass=self)
- DataFabric.log(Logger::WARN) { "remove_connection not implemented by data_fabric" }
- end
-
- def connection_pool
- raise "dynamic connection switching means you cannot get direct access to a pool"
- end
- end
- end
+ class StringProxy
+ def initialize(&block)
+ @proc = block
+ end
+ def to_s
+ @proc.call
end
end
-
+
class ConnectionProxy
cattr_accessor :shard_pools
@@ -73,7 +49,7 @@ def transaction(start_db_transaction = true, &block)
end
def method_missing(method, *args, &block)
- DataFabric.log(Logger::DEBUG) { "Calling #{method} on #{connection}" }
+ DataFabric.logger.debug { "Calling #{method} on #{connection}" }
connection.send(method, *args, &block)
end
@@ -159,23 +135,4 @@ def master
with_master { return connection }
end
end
-
- module ActiveRecordConnectionMethods
- def self.included(base)
- base.alias_method_chain :reload, :master
- end
-
- def reload_with_master(*args, &block)
- connection.with_master { reload_without_master }
- end
- end
-
- class StringProxy
- def initialize(&block)
- @proc = block
- end
- def to_s
- @proc.call
- end
- end
-end
+end
@@ -0,0 +1,47 @@
+require 'data_fabric/connection_proxy'
+
+class ActiveRecord::ConnectionAdapters::ConnectionHandler
+ def clear_active_connections_with_data_fabric!
+ clear_active_connections_without_data_fabric!
+ DataFabric::ConnectionProxy.shard_pools.each_value { |pool| pool.release_connection }
+ end
+ alias_method_chain :clear_active_connections!, :data_fabric
+end
+
+module DataFabric
+ module Extensions
+ def self.included(model)
+ DataFabric.logger.info { "Loading data_fabric #{DataFabric::Version::STRING} with ActiveRecord #{ActiveRecord::VERSION::STRING}" }
+
+ # Wire up ActiveRecord::Base
+ model.extend ClassMethods
+ ConnectionProxy.shard_pools = {}
+ end
+
+ # Class methods injected into ActiveRecord::Base
+ module ClassMethods
+ def data_fabric(options)
+ DataFabric.logger.info { "Creating data_fabric proxy for class #{name}" }
+ @proxy = DataFabric::ConnectionProxy.new(self, options)
+
+ class << self
+ def connection
+ @proxy || superclass.connection
+ end
+
+ def connected?
+ @proxy.connected?
+ end
+
+ def remove_connection(klass=self)
+ DataFabric.logger.warn { "remove_connection not implemented by data_fabric" }
+ end
+
+ def connection_pool
+ raise "dynamic connection switching means you cannot get direct access to a pool"
+ end
+ end
+ end
+ end
+ end
+end
View
@@ -1 +0,0 @@
-DataFabric.init
View
@@ -98,11 +98,8 @@ def test_enchilada
private
def setup_configuration_for(clazz, name)
- if ar22?
- flexmock(ActiveRecord::ConnectionAdapters::ConnectionPool).new_instances.should_receive(:new_connection).and_return(AdapterMock.new(RawConnection.new))
- else
- flexmock(clazz).should_receive(:mysql_connection).and_return(AdapterMock.new(RawConnection.new))
- end
+ flexmock(ActiveRecord::ConnectionAdapters::ConnectionPool).new_instances.should_receive(
+ :new_connection).and_return(AdapterMock.new(RawConnection.new))
ActiveRecord::Base.configurations ||= HashWithIndifferentAccess.new
ActiveRecord::Base.configurations[name] = HashWithIndifferentAccess.new({ :adapter => 'mysql', :database => name, :host => 'localhost'})
end
View
@@ -10,14 +10,10 @@ class DatabaseTest < Test::Unit::TestCase
def setup
ActiveRecord::Base.configurations = load_database_yml
- if ar22?
- DataFabric::ConnectionProxy.shard_pools.clear
- end
+ DataFabric::ConnectionProxy.shard_pools.clear
end
- def test_ar22_features
- return unless ar22?
-
+ def test_features
DataFabric.activate_shard :city => :dallas do
assert_equal 'fiveruns_city_dallas_test_slave', TheWholeBurrito.connection.connection_name
View
@@ -5,7 +5,7 @@
Dir.chdir(ROOT_PATH)
require 'rubygems'
-require 'test/unit'
+require 'minitest/unit'
require 'erb'
require 'logger'
@@ -18,22 +18,15 @@
require 'active_record'
require 'active_record/version'
ActiveRecord::Base.logger = Logger.new(STDOUT)
-ActiveRecord::Base.logger.level = Logger::WARN
+ActiveRecord::Base.logger.level = Logger::DEBUG
-# Bootstrap DF
-deps = defined?(ActiveSupport::Dependencies) ? ActiveSupport::Dependencies : Dependencies
-deps.autoload_paths << File.join(File.dirname(__FILE__), '../lib')
-require 'init'
+require 'data_fabric'
def load_database_yml
filename = DATABASE_YML_PATH
YAML::load(ERB.new(IO.read(filename)).result)
end
-def ar22?
- ActiveRecord::VERSION::STRING >= '2.2.0'
-end
-
if !File.exist?(DATABASE_YML_PATH)
puts "\n*** ERROR ***:\n" <<
"You must have a 'test/database.yml' file in order to run the unit tests. " <<

0 comments on commit 06be923

Please sign in to comment.