我正在尝试使用这个问题中的一些有用代码:
namespace :db do
namespace :test do
task :reset do
ActiveRecord::Base.establish_connection('test')
Rake::Task['db:drop'].invoke
Rake::Task['db:create'].invoke
Rake::Task['db:migrate'].invoke
ActiveRecord::Base.establish_connection(ENV['RAILS_ENV']) #Make sure you don't have side-effects!
end
end
end
不幸的是,我收到了这个错误:
** Invoke db:test:reset (first_time)
** Execute db:test:reset
rake aborted!
database configuration does not specify adapter
/home/nick/.rvm/gems/ruby-1.9.3-p194/gems/activerecord- 3.2.5/lib/active_record/connection_adapters/abstract/connection_specification.rb:45:in `resolve_hash_connection'
/home/nick/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.5/lib/active_record/connection_adapters/abstract/connection_specification.rb:39:in `resolve_string_connection'
/home/nick/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.5/lib/active_record/connection_adapters/abstract/connection_specification.rb:25:in `spec'
/home/nick/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.5/lib/active_record/connection_adapters/abstract/connection_specification.rb:127:in `establish_connection'
/home/nick/Projects/yumbin/lib/tasks/test_environment.rake:4:in `block (3 levels) in <top (required)>'
/home/nick/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
/home/nick/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
/home/nick/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
/home/nick/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
/home/nick/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/home/nick/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/home/nick/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/home/nick/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
/home/nick/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
/home/nick/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level'
/home/nick/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
/home/nick/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level'
/home/nick/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/home/nick/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
/home/nick/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run'
/home/nick/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/home/nick/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/home/nick/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>'
./bundler_stubs/rake:16:in `load'
./bundler_stubs/rake:16:in `<main>'
Tasks: TOP => db:test:reset
如您所见,我通过 RVM 使用 Ruby 1.9.3。我正在使用 Rails 3.2.5、Rake 版本 0.9.2.2 和 Postgresql 9.1,以及 pg gem (0.13.2)。此外,我的环境中没有数据库错误。该应用程序在 Heroku 上部署和运行,在本地开发中运行,我可以毫无问题地运行我的完整测试套件。在运行该行的 Rails 控制台中:
ActiveRecord::Base.establish_connection('test')
成功。我在 vi 中检查了 database.yml 中的不可见字符,但找不到任何字符。每行末尾只有“$”。我开始认为答案非常简单并且就在我的鼻子面前,但我就是看不到它。
更新:添加我的 database.yml 文件:
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
adapter: postgresql
encoding: unicode
database: myapp-dev
pool: 5
username: ******
password: *******
# 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: postgresql
encoding: unicode
database: myapp-test
pool: 5
username: *****
password: ******
production:
adapter: postgresql
encoding: unicode
database: myapp
pool: 5
username: ******
password: *******