Capture state of Rails test database

Ever want to explore the state of the database during a Rails test run? I was recently writing a test to create a seam in some complex legacy code and needed this. Dropping this code in does the job in MySQL:

ActiveRecord::Base.connection.tables.each do |table_name|
ActiveRecord::Base.connection.execute("select * from #{table_name} into outfile '/tmp/datadump-#{table_name}.outfile'")

(or see this gist)

That creates a series of tab-separated files in /tmp/. Then LOAD DATA INFILE queries can bring the data back in, or in my case I used Sequel Pro’s import command configured for tab-separated input.