Read more

Delete all MySQL records while keeping the database schema

Henning Koch
March 14, 2011Software engineer at makandra GmbH

You will occasionally need to clean out your database while keeping the schema intact, e.g. when someone inserted data in a migration or when you had to kill -9 a frozen test process.

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

Old Capybara versions already have the Database Cleaner gem Show archive.org snapshot as dependency. Otherwise add database_cleaner to your *Gemfile`. This lets you say this from the Rails console:

DatabaseCleaner.strategy = :truncation
DatabaseCleaner.clean

You can package this into a nice little Rake task by copying the attachment into lib/tasks/clean_database.rake. Now you can e.g. clean the test database by saying on the shell:

rake db:clean RAILS_ENV=test

If you are using the parallel_tests gem, the task is aware and will clean all your test databases.

Posted by Henning Koch to makandra dev (2011-03-14 11:08)