2012-08-02 77 views
1

我試圖使用的有用的代碼此位從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/[email protected]/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call' 
/home/nick/.rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute' 
/home/nick/.rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each' 
/home/nick/.rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute' 
/home/nick/.rvm/gems/[email protected]/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/[email protected]/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain' 
/home/nick/.rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke' 
/home/nick/.rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task' 
/home/nick/.rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level' 
/home/nick/.rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each' 
/home/nick/.rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level' 
/home/nick/.rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling' 
/home/nick/.rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level' 
/home/nick/.rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run' 
/home/nick/.rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling' 
/home/nick/.rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run' 
/home/nick/.rvm/gems/[email protected]/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中的隱形字符,但找不到任何字符。每行末尾只有'$'。我開始認爲答案非常簡單,在我的鼻子前面,但我看不到它。

UPDATE:添加我的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: ******* 
+0

這將有助於查看你的'database.yml'。 – BaronVonBraun 2012-08-02 17:01:40

+0

對不起,用database.yml更新了這個問題 – Nicholas 2012-08-02 17:03:57

回答

1

你需要在你database.yml文件test條目,我想你可能缺乏一個。請確保你有一個看起來像這樣的條目database.yml

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

使用任何適配器和數據庫開發數據庫使用。