[rspec-users] Database clearing

Scott Taylor mailing_lists at railsnewbie.com
Sat Sep 20 13:17:04 EDT 2008

On Sep 20, 2008, at 8:08 AM, Todd Tyree wrote:

> Ok, here's what I've come up with on the spur of the moment (goes in  
> spec_helper.rb):
>  config.after(:each) do
>     result = ActiveRecord::Base.connection.execute('SHOW TABLES;')
>     while table = result.fetch_row
>       # Or whatever you think is appropriate.
>       next if table.index('schema_migrations') or table.index('roles')
>       ActiveRecord::Base.connection.execute("TRUNCATE #{table}")
>     end
>   end

Seems like it would be more efficient if you pull the "SHOW TABLES"  
statement out of the block, and then allow closure to capture that  
variable.  This way "SHOW TABLES" would only be evaluated once (since  
the tables aren't changing in each example).

Also, it looks like there is no way to truncate multiple tables all in  
one shot (at least in mysql and sql92):



> The main problem is that it's slow.  I tried batching them all up,  
> and running them in a single execute, but foundered on the  
> mysql_options multi-statements setting (it seems that mysql won't,  
> by default, allow multi-statement strings unless you set the multi- 
> statement flag after init and before connect. Not sure how to do  
> this from Base.connect, or even if it's possible.  Sorry, lost track  
> of the article where I found the original reference).
> Best,
> Todd
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

More information about the rspec-users mailing list