Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rake db:migrate and rake db:create both work on test database, not development database

I am new to Stack Overflow and Ruby on Rails. My problem is, when I run the command rake db:create or rake db:migrate, the test database is affected, but the development database is not.

rails (3.2.2)

my database.yml:

# 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: &test
  adapter: postgresql
  encoding: unicode
  database: ticketee_test
  pool: 5
  username: ticketee
  password: my_password_here

development: 
  adapter: postgresql
  encoding: unicode
  database: ticketee_development
  pool: 5
  username: ticketee
  password: my_password_here

production:
  adapter: postgresql
  encoding: unicode
  database: ticketee_production
  pool: 5
  username: ticketee
  password: my_password_here

cucumber:
  <<: *test

Thanks for the suggestion, but I am afraid that isn't the reason. My RAILS_ENV=development. When I start the rails server, the server runs the development database. if I run "rails server -e test" then the server uses the test database. I am still not sure why migrations are running on my test database...

UPDATE: Based on a suggestion, I commented out all of the database configurations except development, and now I get an error. I am running postgresql, I have the gem 'pg' installed. First, my new database.yml:

development:
  adapter: postgresql
  encoding: unicode
  database: ticketee_development
  pool: 5
  username: ticketee
  password: my_password_here

# 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: &test
#  adapter: postgresql
#  encoding: unicode
#  database: ticketee_test
#  pool: 5
#  username: ticketee
#  password: my_password_here

#production:
#  adapter: postgresql
#  encoding: unicode
#  database: ticketee_production
#  pool: 5
#  username: ticketee
#  password: my_password_here

#cucumber:
#  <<: *test

My error message!

Someguys-MacBook-Air:ticketee someguy$ rake db:migrate
rake aborted!
database configuration does not specify adapter

Tasks: TOP => db:migrate => db:load_config
(See full trace by running task with --trace)
Someguys-MacBook-Air:ticketee someguy$ rake db:migrate -t
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Invoke rails_env (first_time)
** Execute rails_env
** Execute db:load_config
rake aborted!
database configuration does not specify adapter
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:45:in `resolve_hash_connection'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:39:in `resolve_string_connection'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:23:in `spec'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:127:in `establish_connection'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/activerecord-3.2.2/lib/active_record/railtie.rb:76:in `block (2 levels) in <class:Railtie>'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/activesupport-3.2.2/lib/active_support/lazy_load_hooks.rb:36:in `instance_eval'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/activesupport-3.2.2/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/activesupport-3.2.2/lib/active_support/lazy_load_hooks.rb:43:in `block in run_load_hooks'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/activesupport-3.2.2/lib/active_support/lazy_load_hooks.rb:42:in `each'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/activesupport-3.2.2/lib/active_support/lazy_load_hooks.rb:42:in `run_load_hooks'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/activerecord-3.2.2/lib/active_record/base.rb:718:in `<top (required)>'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/activerecord-3.2.2/lib/active_record/railties/databases.rake:6:in `block (2 levels) in <top (required)>'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/Users/someguy/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/task.rb:176:in `block in invoke_prerequisites'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/task.rb:174:in `each'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/task.rb:174:in `invoke_prerequisites'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/task.rb:157:in `block in invoke_with_call_chain'
/Users/someguy/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/bin/rake:19:in `load'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/bin/rake:19:in `<main>'

Using the database.yml above, rake db:create:all RAN CORRECTLY and created my table. However, my migration still fails with the same stack trace as the one above.

I changed my database.yml to this: development: adapter: sqlite3 database: db/development.sqlite3 pool: 5 timeout: 5000

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

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

UPDATE: I completely changed my database.yml to this:

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

test: &test
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

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

cucumber:
  <<: *test

Now, this is what happens when I run rake db:drop:all, followed by rake db:migrate. The rake db:migrate command runs a migration, creating a db/test.sqlite3 AND adding a table to it. So, my migration is still running on the test environment. When I run rake environment RAILS_ENV=development db:migrate, the same thing happens and I get a test.sqlite3 file...

like image 538
geography_guy Avatar asked Mar 29 '12 17:03

geography_guy


People also ask

What does rake db setup do?

The rake db:setup task will create the database, load the schema and initialize it with the seed data.

What does Rails DB Migrate do?

A Rails migration is a tool for changing an application's database schema. Instead of managing SQL scripts, you define database changes in a domain-specific language (DSL). The code is database-independent, so you can easily move your app to a new platform.

What does DB Migrate Reset do?

db:reset: Resets your database using your migrations for the current environment. It does this by running the db:drop , db:create , db:migrate tasks. db:rollback: Rolls the schema back to the previous version, undoing the migration that you just ran. If you want to undo previous n migrations, pass STEP=n to this task.


1 Answers

There is a 'brilliant' line in activerecord/lib/active_record/tasks/database_tasks.rb:

environments << 'test' if environment == 'development' && ENV['RAILS_ENV'].nil?

which, until jan 8, 2014 looked like:

environments << 'test' if environment == 'development'
like image 65
ironic Avatar answered Oct 06 '22 12:10

ironic