Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>@awendt kindly pointed out that I could answer my own question.</p> <p>It turns out the recipe is rather simple. The secret is to use a environment variable to tell Rails which db you want to use. </p> <h1>1. Modifying your files</h1> <p>In <code>config/database.yml</code>, include ERB constructs like this:</p> <pre><code>test: &lt;% if (ENV["RAILS_DB"] == "PostgreSQL") %&gt; adapter: postgresql encoding: unicode database: bd_test pool: 5 username: &lt;%= ENV['POSTGRESQL_USERNAME'] || 'root' %&gt; password: &lt;%= ENV['POSTGRESQL_PASSWORD'] || '' %&gt; &lt;% elsif (ENV["RAILS_DB"] == "MySQL") %&gt; adapter: mysql encoding: utf8 reconnect: false database: bd_test pool: 5 username: &lt;%= ENV['MYSQL_USERNAME'] || 'root' %&gt; password: &lt;%= ENV['MYSQL_PASSWORD'] || '' %&gt; socket: &lt;%= ENV['MYSQL_SOCKET'] || '/tmp/mysql.sock' %&gt; &lt;% else %&gt; # default to SQLite adapter: sqlite3 database: db/test.sqlite3 pool: 5 timeout: 5000 &lt;% end %&gt; </code></pre> <p>Note 1: I've only shown it for the test environment. In fact, that's the only one I've modified, since (supposedly) it provides enough coverage to tell me if all three databases are properly supported.</p> <p>Note 2: You don't need to use environment variables to set username and password -- that's just something I prefer to do since it avoids exposing passwords in a commonly viewed file.</p> <p>Similarly, extend Gemfile as follows (note that your version numbers may vary):</p> <pre><code>source 'http://rubygems.org' gem 'rails', '3.0.3' case ENV["RAILS_DB"] when "PostgreSQL" gem 'pg', '0.10.0' when "MySQL" gem 'mysql2' else gem 'sqlite3', '1.3.3' gem 'sqlite3-ruby', '1.3.3', :require =&gt; 'sqlite3' end ... </code></pre> <h1>2. Add conditions to your code</h1> <p>Despite the best efforts of the Rails development team, there are a few spots where ActiveRecord constructs aren't compatible across all flavors of database. In these cases, you can condition your code on <code>ActiveRecord::Base.connection.adapter_name</code>. Here's an example from one of my migration files:</p> <p><code>file: migrate/20110129023453_create_cached_web_pages.rb</code></p> <pre><code>def self.up create_table :cached_web_pages do |t| t.string :key if ActiveRecord::Base.connection.adapter_name == "PostgreSQL" t.binary :value else t.binary :value, :limit =&gt; 16777215 end end end ... </code></pre> <h1>3. Running and testing</h1> <p>You can now select a database simply by setting the RAILS_DB environment variable, but there's a catch: you have to run <code>bundle install</code> each time to set up the appropriate database adaptor from the Gemfile. Fortunately, that's exactly what the test code does. So, for example, I can run rspec's autotest in two windows:</p> <pre><code>$ RAILS_DB=SQLite autotest </code></pre> <p>and</p> <pre><code>$ RAILS_DB=PostgreSQL autotest </code></pre> <p>Now I can hack away at my files and autotest will quietly alert me if I've broken anything as I go along.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload