<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Wood for the Trees: Superfast Testing How To: In-Memory SQLite3</title>
    <link>http://www.mathewabonyi.com/articles/2006/11/26/superfast-testing-how-to-in-memory-sqlite3</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>struggling to dig life</description>
    <item>
      <title>Superfast Testing How To: In-Memory SQLite3</title>
      <description>&lt;p style="text-align:justify;"&gt;&lt;strong&gt;Audience&lt;/strong&gt;: This is for both application and plugin developers. Especially plugin developers. Always use SQLite3 in-memory databases as your default for plugin tests which require a database.&lt;/p&gt;


	&lt;p style="text-align:justify;"&gt;Some of you may have heard of SQLite3, played with it and decided it is too limited or don&amp;#8217;t like the files which it creates in your project tree. There is a less-sung alternative which is much, much faster (actual benchmarks vary because of memory speeds, but for me PostgreSQL is about 33% slower and MySQL about 10% slower). Over a large testing library with thousands of assertions, you will feel that meagre 10% by a comfortable minute. &lt;strong&gt;Slight disadvantage&lt;/strong&gt;: you can&amp;#8217;t get that coffee during testing anymore!&lt;/p&gt;


	&lt;p style="text-align:justify;"&gt;There are a handful of sites which have spoken of SQLite3 in-memory databases this year, but to get it working you&amp;#8217;ll find the information is a little scattered. The following is a step-by-step &amp;#8216;how to&amp;#8217; on making your application and plugin tests superfast and lightweight, but please read over these pages first:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://nubyonrails.com/articles/2006/06/01/san-francisco-sqlite3-memory-tests-asteroids"&gt;TopFunky on fixing Rails support for SQLite in memory&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://martinfowler.com/bliki/InMemoryTestDatabase.html"&gt;Martin Fowler on memory databases&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://code.whytheluckystiff.net/camping/wiki/BeAlertWhenOnSqlite3"&gt;Why on Being Alert on SQLite&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://wiki.rubyonrails.com/rails/pages/HowtoUseSQLite"&gt;Rails Wiki on using SQLite&lt;/a&gt;&lt;/li&gt;
	&lt;/ul&gt;


	&lt;h4&gt;Set Up&lt;/h4&gt;


	&lt;p style="text-align:justify;"&gt;A lot of this doesn&amp;#8217;t need much explanation, so I&amp;#8217;m just going to dump out the procedure to get you from MySQL/PostgreSQL/whatever testing to SQLite3 in-memory database testing, making you wait less and code more.&lt;/p&gt;


	&lt;h4&gt;Step 1: Install SQLite3 and friends&lt;/h4&gt;


	&lt;p style="text-align:justify;"&gt;If you do not have SQLite3 already, you need to go to the &lt;a href="http://www.sqlite.org/"&gt;SQLite website&lt;/a&gt; and download the appropriate package. If you use Mac &lt;span class="caps"&gt;OS X&lt;/span&gt;, you need to install the &lt;span class="caps"&gt;SWIG&lt;/span&gt; library too.&lt;/p&gt;


	&lt;p style="text-align:justify;"&gt;Once you have the latest SQLite3 distribution, you need to install the sqlite3-ruby gem:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_shell "&gt;  user@host railsproj$ sudo gem install sqlite3-ruby --source code.whytheluckystiff.net&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p style="text-align:justify;"&gt;Select the highest &amp;#8216;(ruby)&amp;#8217; version (1.1.0.1 at the time of this article). You should see it compile a native extension and install without a hitch.&lt;/p&gt;


	&lt;p style="text-align:justify;"&gt;&lt;strong&gt;Note&lt;/strong&gt;: If you already have sqlite3-ruby installed but do not remember seeing a native extension being compiled, you should uninstall (`gem uninstall sqlite3-ruby`) and install it again with the &lt;span class="caps"&gt;SWIG&lt;/span&gt; library.&lt;/p&gt;


	&lt;h4&gt;Step 2: Install the Memory Test Fix plugin&lt;/h4&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_shell "&gt;  user@host railsproj$ ./script/plugin install http://topfunky.net/svn/plugins/memory_test_fix&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;h4&gt;Step 3: Change &lt;span class="caps"&gt;RAILS&lt;/span&gt;_ROOT/config/database.yml&lt;/h4&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_yaml "&gt;&lt;span class="key"&gt;test&lt;/span&gt;&lt;span class="punct"&gt;:&lt;/span&gt;
  &lt;span class="key"&gt;adapter&lt;/span&gt;&lt;span class="punct"&gt;:&lt;/span&gt; sqlite3
  &lt;span class="key"&gt;database&lt;/span&gt;&lt;span class="punct"&gt;:&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;:memory:&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;
  &lt;span class="key"&gt;verbosity&lt;/span&gt;&lt;span class="punct"&gt;:&lt;/span&gt; quiet&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p style="text-align:justify;"&gt;&lt;strong&gt;Note&lt;/strong&gt;: remove `verbosity: quiet` at the beginning to make sure SQLite3 is loading your db/schema.rb file. If you are not using Migrations or db/schema.rb, you will also need to set `config.active_record.schema_format = :sql` in your environment files and point Rails to the correct schema file.&lt;/p&gt;


	&lt;h4&gt;Step 4: Test it&lt;/h4&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_shell "&gt;  user@host railsproj$ rake test&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;h4&gt;Notes&lt;/h4&gt;


	&lt;p style="text-align:justify;"&gt;As TopFunky says: if you use database specific &lt;span class="caps"&gt;SQL&lt;/span&gt;, using SQLite3 will only get in the way.&lt;/p&gt;</description>
      <pubDate>Sun, 26 Nov 2006 16:15:49 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:c836ac9b-68c4-4964-9f63-6dbfed2b5c52</guid>
      <author>Mathew Abonyi</author>
      <link>http://www.mathewabonyi.com/articles/2006/11/26/superfast-testing-how-to-in-memory-sqlite3</link>
      <category>Discoveries</category>
      <category>Ruby</category>
      <category>Rails</category>
      <trackback:ping>http://www.mathewabonyi.com/articles/trackback/31</trackback:ping>
    </item>
  </channel>
</rss>
