Search code examples
ruby-on-railsdatabaseruby-on-rails-3sqliterails-postgresql

Using Sqlite3 test database and Postgres dev/production database in Rails


I currently have a rails project which I deploy to a production server which uses a postgres database. I develop my rails project in Windows, which means that if I want to test locally, I have to change all of the databases in my database.yml file from postgres over to sqlite3 (because setting up Windows to run a postgres server appears to be a pain).

What I would like to be able to do is format my database.yml something like this:

development:
  adapter: postgresql
  encoding: utf8
  database: <%= begin IO.read("/home/www-data/.db/.dev_name") rescue "" end %>
  pool: 5
  username: <%= begin IO.read("/home/www-data/.db/.user") rescue "" end %>
  password: <%= begin IO.read("/home/www-data/.db/.pass") rescue "" end %>

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

production:
  adapter: postgresql
  encoding: utf8
  database: <%= begin IO.read("/home/www-data/.db/.prod_name") rescue "" end %>
  pool: 5
  username: <%= begin IO.read("/home/www-data/.db/.user") rescue "" end %>
  password: <%= begin IO.read("/home/www-data/.db/.pass") rescue "" end %>

That way I can run rails s -e test locally and test with an sqlite3 database, but when I deploy to my development and production servers I can use postgres.

The problem I am having is that, with the changes to my database.yml shown above, when I run rails s -e test locally I get an error saying that rails could not find the pg gem which seems to imply that it is still trying to use either the development or the production server.


Solution

  • With all the warnings acknowledged, the answer to the question would be to use group in your Gemfile like

    gem 'pg', group: [:development, :production]
    gem 'sqlite3', group: :test