I have the following task:
namespace :db do
desc "Drop, create, migrate, seed the database and prepare the test database for rspec"
task :reset_db => :environment do
puts "Environment Check: Rails Environment = #{Rails.env}"
Rake::Task['db:drop'].invoke
Rake::Task['db:create'].invoke
Rake::Task['db:migrate'].invoke
#Rake::Task['db:fixtures:load'].invoke
Rake::Task['db:test:prepare'].invoke
puts 'Seeding Database..'
Rake::Task['db:seed'].invoke
end
end
This task works fine up to the last db:seed line.
It seems to be using the test environment and creates the data there, while all the other tasks execute in the development environment. When I execute rake db:seed via the command line, it runs correctly in the development environment.
How can I prevent it from running in the test environment?
The
db:test:preparerake task prepares the test database, and therefore setsRAILS_ENV=test.So the reason why your task gets run in the testing environment is because
db:test:prepareactually overwrites yourRAILS_ENVvariable.