views:

1128

answers:

3

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.

A: 

Is this something that you ran once as a migration because it happened to be needed, then turns out to be a useful query that might need to get run a number of times?

perhaps you should refactor the contents of the migration into a model or other object, then have the migration reference that new location.

Then you can simply execute the new object at your lesure by invoking ruby on the command line.

Nathan Feger
+2  A: 

Assuming fairly recent version of Rails you can always run:

rake db:migrate:up VERSION=20090408054532

Where version is the timestamp in the filename of the migration.

dasil003
This will run migrations up to that version. I was looking for a way to run just a specific migration.
nan
Actually the command is rake db:migrate:redo VERSION=my_version
Chirag Patel
+3  A: 

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

irb
>> require "db/migrate/20090408054532_add_foos.rb"
>> AddFoos.up

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 probably won't update the schema_migrations table, but it seems like that's what you want anyway.

Orion Edwards
Perfect. Just what I needed.
nan