Re: Database Cleaner

71 views
Skip to first unread message

Ben Mabey

unread,
Mar 28, 2012, 1:10:18 PM3/28/12
to Evans, Brandon, database...@googlegroups.com
On 3/28/12 10:17 AM, Evans, Brandon wrote:

Good afternoon Ben,

 

                We’re using database cleaner and we’ve come across some issues trying to use it for our particular situation. We can’t use transaction because it seems it’s not actually inserting the data into the database and as such our app won’t see the data that we try to insert into it via ActiveRecord. Truncation doesn’t quite work for us because it will cause problems if we have tests that access the same table (which we do), or if we attempt to run multiple instances of the same test. To work around this I wrote some code to do a surgical delete. Essentially what it does is keep track of the primary id’s that are being inserted into the DB (currently in an array) and then goes through those arrays and removes all of the records that were inserted into the tables that have id’s in this array. My question is this… Would this be a strategy that we might implement and roll into Database cleaner and if so did you have any pointers and ideas that you might give me in regards to the code of DatabaseCleaner that I might keep in mind as I look through the code? I’m not quite sure if this can even be added into it. A coworker and I are working on generalizing it as best as we can in hopes that it can be self-aware and/or generalized so other people might be able to make use of it. Just thought I’d get any thoughts or ideas that you might have on this.

 

Thanks,

 

                Brandon



Hey Brandon,
How are you keeping track of the IDs that are inserted?  Via a hook with ActiveRecord or something similar? 

One approach that has been brought up that may fit your needs is a snapshot approach[1].  Where DatabaseCleaner would DUMP the DB contents before the test runs and then restore the DB contents via a LOAD (much like fixtures).   Another approach may be to just take a snapshot of the IDs in the database prior to testing and then remove any ids above the last known good one.  That seems like a simpler approach since all you would need to know is that last good id to keep. (This of course would only work with DBs/tables with auto-incrementing ids.)  Would such an approach work in your situation?

-Ben


1. https://github.com/bmabey/database_cleaner/issues/80

Reply all
Reply to author
Forward
0 new messages