rails/rails — pull request #10602 by Ichimonji10 (2013-05-13 20:43:35+00:00)
Title: Don't touch test db when `Rails.env == development`

Don't touch test db when `Rails.env == development`.

Currently, `rake db:create`, `rake db:drop`, and derived tasks (such as `rake db:reset`) create and drop both the development and test databases when `'development' == Rails.env`. Such behaviour vioates the Principle of Least Surprise. It's extremely confusing to execute this:

$ bundle exec rake db:reset

or this:

$ RAILS_ENV=development bundle exec rake db:reset

...and discover that your test database is being recreated. This behaviour can be functionally problematic. For example, assume a Rails project had the following in their `database.yml` config:

development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000

test:
adapter: postgresql
encoding: unicode
database: myapp_test
pool: 5
username: username
password: hackme

If a developer does not have postgresql installed and configured, Rake tasks which attempt to recreate the development database will fail because of the test database. That's silly.

This commit fixes this behaviour. I've tested the changes with sqlite, mysql, and postgresql. sqlite and mysql pass with flying colors.

postgresql failed unit testing, but I think it failed because of unrelated reasons. If someone else could verify, that would be great. An example of the messages I'm receiving:

/home/ichimonji10/code/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:582: warning: instance variable @socket_io not initialized
...
10) Error:
PostgresqlUUIDTest#test_schema_dumper_for_uuid_primary_key:
ActiveRecord::StatementInvalid: PG::Error: ERROR: current transaction is aborted, commands ignored until end of transaction block
: drop table if exists pg_uuids
/home/ichimonji10/code/rails/activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in `exec'
/home/ichimonji10/code/rails/activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in `block in execute'
/home/ichimonji10/code/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:425:in `block in log'
/home/ichimonji10/code/rails/activesupport/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/home/ichimonji10/code/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:420:in `log'
/home/ichimonji10/code/rails/activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb:127:in `execute'
/home/ichimonji10/code/rails/activerecord/test/cases/adapters/postgresql/uuid_test.rb:35:in `teardown'