<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <generator>Persumi - Level up your writing and blogging with AI</generator>
  <category label="Blog" scheme="http://persumi.com/u/fredwu/tech/e/blog" term="blog"/>
  <category label="Tech" scheme="http://persumi.com/u/fredwu/tech" term="tech"/>
  <link href="http://persumi.com/u/fredwu/tech/e/blog/t/database_rewinder"/>
  <link href="http://persumi.com/u/fredwu/tech/e/blog/t/database_rewinder/feed/rss"/>
  <link rel="self" href="http://persumi.com/u/fredwu/tech/e/blog/t/database_rewinder/feed/atom"/>
  <author>
    <name>Fred Wu</name>
    <email>ifredwu@gmail.com</email>
    <uri>http://persumi.com/u/fredwu</uri>
  </author>
  <subtitle/>
  <id>http://persumi.com/u/fredwu/tech/e/blog/t/database_rewinder</id>
  <title>Blog (database_rewinder) - Fred Wu&apos;s Tech</title>
  <updated>2026-06-21T03:25:43.618435Z</updated>
  <entry>
    <content type="html">&lt;![CDATA[&lt;p&gt;
&lt;em&gt;Please also see &lt;a href=&quot;/blog/2013-09-06-protip-ruby-devs-please-tweak-your-gc-settings/&quot;&gt;this blog post on tweaking your ruby GC settings&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;
I use and love &lt;a href=&quot;https://github.com/bmabey/database_cleaner&quot;&gt;DatabaseCleaner&lt;/a&gt;, although historically I had never paid too much attention on the performance of its varies cleaning strategies - I’d always used &lt;code class=&quot;inline&quot;&gt;truncation&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;
We use Postgres, and after digging around and finding out &lt;a href=&quot;http://stackoverflow.com/questions/11419536/postgresql-truncation-speed/11423886#11423886&quot;&gt;the difference between DELETE and TRUNCATE&lt;/a&gt;, I ended up improving our test suite speed by about 30-40% simply by tweaking the cleaning strategies.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;RSpec.configure do |config|
  config.before :suite do
    DatabaseCleaner.clean_with :truncation
    DatabaseCleaner.strategy = :transaction
  end

  config.before do
    if example.metadata[:js] || example.metadata[:type] == :feature
      DatabaseCleaner.strategy = :deletion
    else
      DatabaseCleaner.strategy = :transaction
      DatabaseCleaner.start
    end
  end

  config.after do
    DatabaseCleaner.clean
  end
end&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
Essentially, we want to &lt;code class=&quot;inline&quot;&gt;truncate&lt;/code&gt; the DB only once before the whole suite runs to ensure a clean slate DB, then we only want to use &lt;code class=&quot;inline&quot;&gt;deletion&lt;/code&gt; on Capybara tests, everything else should just use &lt;code class=&quot;inline&quot;&gt;transaction&lt;/code&gt; which is the fastest strategy.&lt;/p&gt;
&lt;p&gt;
Now, as a bonus, I have just discovered @amatsuda’s &lt;a href=&quot;https://github.com/amatsuda/database_rewinder&quot;&gt;DatabaseRewinder&lt;/a&gt; which is a lightweight alternative that supports only ActiveRecord. It offers comparable performance with a much similar API.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;RSpec.configure do |config|
  config.before :suite do
    DatabaseRewinder.clean_all
  end

  config.after do
    DatabaseRewinder.clean
  end
end&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
By the way, we also use &lt;a href=&quot;https://github.com/grosser/parallel_tests&quot;&gt;parallel_tests&lt;/a&gt; to scale our test suite to multiple processes, even on Travis CI and Wercker.&lt;/p&gt;
&lt;p&gt;
Hooray to faster tests! :)&lt;/p&gt;
]]&gt;</content>
    <published>2013-09-18T06:50:00.000000Z</published>
    <category label="Blog" scheme="http://persumi.com/u/fredwu/tech/e/blog" term="blog"/>
    <category label="Tech" scheme="http://persumi.com/u/fredwu/tech" term="tech"/>
    <link href="http://persumi.com/u/fredwu/tech/e/blog/p/protip-faster-ruby-tests-with-databasecleaner-and-databaserewinder"/>
    <author>
      <name>Fred Wu</name>
      <email>ifredwu@gmail.com</email>
      <uri>http://persumi.com/u/fredwu</uri>
    </author>
    <id>http://persumi.com/u/fredwu/tech/e/blog/p/protip-faster-ruby-tests-with-databasecleaner-and-databaserewinder</id>
    <title>Protip: Faster Ruby Tests with DatabaseCleaner and DatabaseRewinder</title>
    <updated>2013-09-18T06:50:00.000000Z</updated>
  </entry>
</feed>