Run a single migration file

migration, ruby, ruby-on-rails

Solution

You can just run the code directly out of the ruby file:

rails console
>> require "db/migrate/20090408054532_add_foos.rb"
>> AddFoos.new.up

Note: Very old versions of rails may require `AddFoos.up` rather than `AddFoos.new.up`. If your migration has a "change" method, you need to run `AddFoos.new.change`.

An alternative way (without IRB) which relies on the fact that require returns an array of class names:

script/runner 'require("db/migrate/20090408054532_add_foos.rb").first.constantize.up'

Note that if you do this, it won't update the `schema_migrations` table, but it seems like that's what you want anyway.

Additionally, if it can't find the file you may need to use `require("./db/..."` or try `require_relative` depending on your working directory

Problem

Is there an easy way to run a single migration? I don't want to migrate to a certain version I just want to run a specific one.

Original source