Skip to content

Commit

Permalink
Make after_commit and after_rollback respect :server option (Fixes je…
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyevans committed Dec 1, 2011
1 parent 28cc339 commit ffbb638
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/sequel/database/misc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions spec/core/database_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down

0 comments on commit ffbb638

Please sign in to comment.