Thursday, December 14, 2006

Rails: Running migrations in another environment

From the command line it's possible to run migrations in any environment. For example, using the code below it is possible to run migrations in the test environment.
rake db:migrate RAILS_ENV=test
Because the above code works it would appear that you could create the following rake task to automate running the migrations in the test environment.
task :test_migrate do
ENV["RAILS_ENV"] = "test"
Rake::Task["db:migrate"].invoke
end
Unfortunately, that didn't work, but the following change does work.
task :test_migrate do
ActiveRecord::Base.establish_connection "test"
Rake::Task["db:migrate"].invoke
end

2 comments:

  1. Anonymous1:51 AM

    Out of curiosity, why do you need this? Certainly you don't need to maintain your test data...even if you do, it should be provided in fixtures. rake db:test:prepare will bring the database up to date.

    ReplyDelete
  2. Anonymous7:49 AM

    Pat,
    On my current team, following a check in, the code is checked out by our continuous integration (CI) process to our CI server. This server runs all the tests and publishes a build report so the team can see the status of the codebase. This CI environment doesn't need a 'development' database since all it cares about is running all the tests. Since there is no development database, we don't have the option to rely on db:test:prepare, db:schema:dump, db:test:clone, and db:schema:load. All of the above have been removed from our test prerequsite list.

    ReplyDelete

Note: Only a member of this blog may post a comment.