Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7760627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:56:24+00:00 2026-06-01T13:56:24+00:00

I started deploying my latest RoR app on Heroku , which required me to

  • 0

I started deploying my latest RoR app on Heroku, which required me to start using PostgreSQL — I’d previously been using SQLite and MySQL. I wanted a dead-simple way to continually do red/green testing against all three databases to make sure I didn’t break anything in the heat of development.

What’s a good way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T13:56:25+00:00Added an answer on June 1, 2026 at 1:56 pm

    @awendt kindly pointed out that I could answer my own question.

    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.

    1. Modifying your files

    In config/database.yml, include ERB constructs like this:

    test:
    <% if (ENV["RAILS_DB"] == "PostgreSQL") %>
      adapter: postgresql
      encoding: unicode
      database: bd_test
      pool: 5
      username: <%= ENV['POSTGRESQL_USERNAME'] || 'root' %>
      password: <%= ENV['POSTGRESQL_PASSWORD'] || '' %>
    <% elsif (ENV["RAILS_DB"] == "MySQL") %>
      adapter: mysql
      encoding: utf8
      reconnect: false
      database: bd_test
      pool: 5
      username: <%= ENV['MYSQL_USERNAME'] || 'root' %>
      password: <%= ENV['MYSQL_PASSWORD'] || '' %>
      socket: <%= ENV['MYSQL_SOCKET'] || '/tmp/mysql.sock' %>
    <% else %>
      # default to SQLite
      adapter: sqlite3
      database: db/test.sqlite3
      pool: 5
      timeout: 5000
    <% end %>
    

    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.

    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.

    Similarly, extend Gemfile as follows (note that your version numbers may vary):

    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 => 'sqlite3'
    end
    ...
    

    2. Add conditions to your code

    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 ActiveRecord::Base.connection.adapter_name. Here’s an example from one of my migration files:

    file: migrate/20110129023453_create_cached_web_pages.rb

    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 => 16777215
        end
      end
    end
    ...
    

    3. Running and testing

    You can now select a database simply by setting the RAILS_DB environment variable, but there’s a catch: you have to run bundle install 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:

    $ RAILS_DB=SQLite autotest
    

    and

    $ RAILS_DB=PostgreSQL autotest
    

    Now I can hack away at my files and autotest will quietly alert me if I’ve broken anything as I go along.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I started playing with Sinatra and deploying on Heroku, which I find really enjoyable.
I am having trouble with deploying rails 3.1. app to heroku. After some research,
I am using deploying a Ruby on Rails app to a Linode VPS using
Started working on a new application this week that is running the latest rails
I started an application in Google App Engine right when it came out, to
I started using subversion for one of my projects and it would be absolutely
I just recently started having issues while deploying EARS within websphere. The error I
I started to use the database projects that are in Visual Studio to start
I'm having problems deploying my website to the live server using VWD 2010 Express.
Started using thinking sphinx today and I'd like to know what's going wrong here:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.