Poetry of Programming

Its about Ruby on Rails – Kiran Soumya


I need an individual file to be migrated in Rails

Rails migrations are great, they allow continual evolution of database schema. Sometimes, especially when prototyping, I hate to run migrations down and up and load data, just to make small independent change in database table. In this situations I run only specific migration:

ruby script/runner 'require "db/migrate/005_create_blogs"; \n
ruby script/runner 'require "db/migrate/005_create_blogs";\n

This is much typing (for just one migration at least 😉 so here is the rake task to do the same:

namespace :db do
  task :migrate_one => :environment do
    file = Dir["db/migrate/#{ENV["VERSION"]}_*.rb"].first
    migration_class = file.scan(
    migration_class.migrate(:down) unless ENV["DIRECTION"] == 'up'
    migration_class.migrate(:up) unless ENV["DIRECTION"] == 'down'

Put this rake task in lib/tasks and you can call it with rake db:migrate_one VERSION=005. This would run migration down and up. You can also add DIRECTION=up or DIRECTION=down to control direction.

Thanks to Bojan Mihelac.

Leave a Reply

Your email address will not be published. Required fields are marked *