通过rake测试重置测试数据库抱怨:“数据库配置没有指定适配器”

时间:2012-08-02 16:50:55

标签: ruby-on-rails postgresql rake

我正在尝试使用this question中的这段有用代码:

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: *******

1 个答案:

答案 0 :(得分:1)

您的test文件中需要database.yml个条目,我认为您可能缺少一个条目。确保在database.yml

中有一个如下所示的条目
test:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

使用开发数据库正在使用的任何适配器和数据库。