rspecでrake db:createの対象がtestのみになる件
Rails2.2.2とrspec1.1.12(rspec-rails1.1.12)で確認。
通常、こうするとconfig/database.ymlのdevelopmentを参照する。
$ export RAILS_ENV=development $ rake db:create
しかし、rspecをconfig.gemで利用するようにした場合、
同様のコマンドでconfig/database.ymlのtestのみを参照するようになってしまう。
まぁ、rake db:migrateはちゃんとdevelopmentを見てくれるので影響は小さいけど。
微妙だよなぁ、これ。
[2009/05/16 追記]
やはり、rspecまたはrspec_railsが悪さをしているように思える。
productionモードで動かしている場合でも、rspec_railsを使用していると、
RAILS_ENV == test、ENV['RAILS_ENV'] == 'procution'
となってしまう。
ググっても同じ現象で困っている人を見かけないのだが、本当に大丈夫なんだろうか?
とりあえず、開発環境(development)で、このような状況になったら
db:create、db:dropを以下のように書き換えれば良い。
{RAILS_ROOT}/lib/tasks/databases.rakeに以下を記述する。
namespace :db do desc 'Create the database defined in config/database.yml for the current RAILS_ENV' task :create => :environment do ENV['RAILS_ENV'] ||= 'development' puts "target=#{ENV['RAILS_ENV']}" create_database(ActiveRecord::Base.configurations[ENV['RAILS_ENV']]) end desc 'Drops the database for the current RAILS_ENV' task :drop => :environment do ENV['RAILS_ENV'] ||= 'development' puts "target=#{ENV['RAILS_ENV']}" config = ActiveRecord::Base.configurations[ENV['RAILS_ENV']] begin drop_database(config) rescue Exception => e puts "Couldn't drop #{config['database']} : #{e.inspect}" end end end
やっている事は本来のdb:create、db:dropがRAILS_ENVを参照しているところを
という感じにしただけ。これで当面の問題は凌げるだろう。
productionモードでもrspec_railsを使っているとRAILS_ENVはtestになってしまうので、RAILS_ENVの値で動作を変えたい場合は、ENV['RAILS_ENV']を使用するほうが良いだろう。