From ffbb6386e9a66822ecd0c4aaf2ad8e092479fb9d Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Thu, 1 Dec 2011 08:25:10 -0800 Subject: [PATCH] Make after_commit and after_rollback respect :server option (Fixes #401) --- lib/sequel/database/misc.rb | 8 ++++++-- spec/core/database_spec.rb | 7 +++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/sequel/database/misc.rb b/lib/sequel/database/misc.rb index cdead1e958..592152ac6f 100644 --- a/lib/sequel/database/misc.rb +++ b/lib/sequel/database/misc.rb @@ -69,9 +69,11 @@ def initialize(opts = {}, &block) # If a transaction is not currently in process, yield to the block immediately. # Otherwise, add the block to the list of blocks to call after the currently # in progress transaction commits (and only if it commits). + # Options: + # :server :: The server/shard to use. def after_commit(opts={}, &block) raise Error, "must provide block to after_commit" unless block - synchronize(opts) do |conn| + synchronize(opts[:server]) do |conn| if h = @transactions[conn] raise Error, "cannot call after_commit in a prepared transaction" if h[:prepare] (h[:after_commit] ||= []) << block @@ -84,9 +86,11 @@ def after_commit(opts={}, &block) # If a transaction is not currently in progress, ignore the block. # Otherwise, add the block to the list of the blocks to call after the currently # in progress transaction rolls back (and only if it rolls back). + # Options: + # :server :: The server/shard to use. def after_rollback(opts={}, &block) raise Error, "must provide block to after_rollback" unless block - synchronize(opts) do |conn| + synchronize(opts[:server]) do |conn| if h = @transactions[conn] raise Error, "cannot call after_rollback in a prepared transaction" if h[:prepare] (h[:after_rollback] ||= []) << block diff --git a/spec/core/database_spec.rb b/spec/core/database_spec.rb index 4704fdbddf..08ba9b421b 100644 --- a/spec/core/database_spec.rb +++ b/spec/core/database_spec.rb @@ -739,6 +739,13 @@ def @db.ret_commit proc{@db.after_rollback}.should raise_error(Sequel::Error) end + specify "should have after_commit and after_rollback respect :server option" do + @db.transaction(:server=>:test){@db.after_commit(:server=>:test){@db.execute('foo', :server=>:test)}} + @db.sqls.should == ['BEGIN -- test', 'COMMIT -- test', 'foo -- test'] + @db.transaction(:server=>:test){@db.after_rollback(:server=>:test){@db.execute('foo', :server=>:test)}; raise Sequel::Rollback} + @db.sqls.should == ['BEGIN -- test', 'ROLLBACK -- test', 'foo -- test'] + end + specify "should execute after_commit outside transactions" do @db.after_commit{@db.execute('foo')} @db.sqls.should == ['foo']