Skip to content

Commit

Permalink
Add new adapter for ActiveRecord 4.2, fix #198
Browse files Browse the repository at this point in the history
  • Loading branch information
exAspArk committed Jan 14, 2016
1 parent ab7db1c commit efbceec
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 8 deletions.
7 changes: 6 additions & 1 deletion lib/active_record/connection_adapters/em_mysql2_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ class Client < Mysql2::EM::Client
include EM::Synchrony::ActiveRecord::Client
end

include EM::Synchrony::ActiveRecord::Adapter
if ::ActiveRecord.version >= Gem::Version.new('4.2')
require 'em-synchrony/activerecord_4_2'
include EM::Synchrony::ActiveRecord::Adapter_4_2
else
include EM::Synchrony::ActiveRecord::Adapter
end
end
end
end
61 changes: 61 additions & 0 deletions lib/em-synchrony/activerecord_4_2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require 'active_record'

module EM::Synchrony
module ActiveRecord
module Adapter_4_2
def configure_connection
nil
end

def transaction(*args)
@connection.execute(false) do |conn|
super
end
end

def reset_transaction #:nodoc:
@transaction_manager = TransactionManager.new(self)
end
end

class TransactionManager < ::ActiveRecord::ConnectionAdapters::TransactionManager
def initialize(*args)
super
@stack = Hash.new([])
end

def current_transaction #:nodoc:
_current_stack.last || NULL_TRANSACTION
end

def open_transactions
_current_stack.size
end

def begin_transaction(options = {}) #:nodoc:
transaction =
if _current_stack.empty?
::ActiveRecord::ConnectionAdapters::RealTransaction.new(@connection, options)
else
::ActiveRecord::ConnectionAdapters::SavepointTransaction.new(@connection, "active_record_#{Fiber.current.object_id}_#{open_transactions}", options)
end
_current_stack.push(transaction)
transaction
end

def commit_transaction #:nodoc:
_current_stack.pop.commit
end

def rollback_transaction #:nodoc:
_current_stack.pop.rollback
end

private

def _current_stack
@stack[Fiber.current.object_id]
end
end
end
end
23 changes: 16 additions & 7 deletions spec/activerecord_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,24 @@ class Widget < ActiveRecord::Base; end;
describe "Fiberized ActiveRecord driver for mysql2" do
DELAY = 0.25
QUERY = "SELECT sleep(#{DELAY})"
LOGGER = Logger.new(STDOUT).tap do |logger|
logger.formatter = proc do |_severity, datetime, _progname, msg|
"[#{datetime.strftime('%Y-%m-%d %H:%M:%S')} ##{Fiber.current.object_id}] -- : #{msg}\n"
end
end

before(:all) do
ActiveRecord::Base.logger = LOGGER if ENV['LOGGER']
end

def establish_connection
ActiveRecord::Base.establish_connection(
:adapter => 'em_mysql2',
:database => 'widgets',
:username => 'root',
:pool => 10
)
Widget.delete_all
ActiveRecord::Base.establish_connection(
:adapter => 'em_mysql2',
:database => 'widgets',
:username => 'root',
:pool => 10
)
Widget.delete_all
end

it "should establish AR connection" do
Expand Down

0 comments on commit efbceec

Please sign in to comment.