1

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.

Chris Travers
  • 25,424
  • 6
  • 65
  • 182
Alex
  • 113
  • 2
  • 7
  • 1
    Setting up Postgres oin Windows shouldn't be a pain at all. Developing with SQLite but using Postgres in production, now that's a pain, and a big one. I would recommend to use the same RDBMS for both. – Erwin Brandstetter Jul 01 '13 at 17:51
  • Developing in the one and switching to the other was very easy for me; just a few changes to my database.yml and adding "gem 'pg'" to my gemfile. What I am really looking for is a solution to not having to repeatedly make those changes every time I want to deploy. – Alex Jul 01 '13 at 18:29
  • That is all very well - until it isn't. There are subtle differences that can break your code or change behavior slightly and you don't even realize it (until somebody complains). [Like in this related question.](http://stackoverflow.com/questions/11249059/generic-ruby-solution-for-sqlite3-like-or-postgresql-ilike) – Erwin Brandstetter Jul 01 '13 at 18:43

1 Answers1

7

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
mlt
  • 1,595
  • 2
  • 21
  • 52