On Thursday, January 18, 2018 at 1:01:56 AM UTC-8, blambeau wrote:
>
> Hi!
>
> I've read on the group that Sequel does not support explicit
> begin/commit/rollback because it would lead to an unsafe API. I partly
> agree with that.
>
> Still, I have a situation where I need start a transaction in a
> `before(:each)` and rollback it in a `after(:each)`, in RSpec. I ended up
> doing the following monkey patch:
>
> module Sequel
> class Database
>
> def ext_start_transaction(opts=OPTS)
> synchronize(opts[:server]){|conn|
> add_transaction(conn, opts)
> begin_transaction(conn, opts)
> }
> end
>
> def ext_rollback_transaction(opts=OPTS)
> synchronize(opts[:server]){|conn|
> rollback_transaction(conn, opts)
> remove_transaction(conn, false)
> }
> end
>
> end
> end
>
> Does any one know a cleaner solution that this?
>
RSpec already supports an around hook that you can use, I would use that.
The example you gave will probably not work correctly if there is more than
one connection in the database pool, as there is no guarantee that conn
yielded in ext_start_transaction will be the same in
ext_rollback_transaction.
Note that if you use minitest and minitest-hooks, then you can do:
around(:all) do |&block|
DB.transaction(:rollback=>:always){super(&block)}
end
around do |&block|
DB.transaction(:rollback=>:always, :savepoint=>true,
:auto_savepoint=>true){super(&block)}
end
Which allows you to run spec suites in a transaction, and each spec in a
savepoint, I don't believe rspec supports that.
Thanks,
Jeremy
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.