Skip to content

Instantly share code, notes, and snippets.

@rosenfeld
Created August 5, 2013 12:44
Show Gist options
  • Select an option

  • Save rosenfeld/6155645 to your computer and use it in GitHub Desktop.

Select an option

Save rosenfeld/6155645 to your computer and use it in GitHub Desktop.
RSpec run_all_in_transaction helper for running before(:all) blocks under a transaction with Sequel
# support/group_helpers.rb
module GroupHelpers
def self.start_transaction
# @_conn = DB.pool.send :acquire, Thread.current
@_conn = DB.pool.hold{|conn| conn }
DB.send(:add_transaction, @_conn, {savepoint: true})
DB.send(:begin_transaction, @_conn, savepoint: true)
end
def self.rollback
DB.send(:rollback_transaction, @_conn, savepoint: true)
DB.send(:remove_transaction, @_conn, false)
# DB.pool.send(:sync){DB.pool.send(:release, Thread.current)}
end
def run_all_in_transaction
before(:all){ GroupHelpers.start_transaction }
after(:all){ GroupHelpers.rollback }
end
end
require_relative 'support/group_helpers'
RSpec.configure do |c|
c.extend GroupHelpers
c.around(:each) do |example|
DB.transaction(savepoint: true, rollback: :always){ example.run }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment