Friday, April 29, 2011

Run a single migration file

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.

From stackoverflow
  • 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.

  • 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.

    nan : This will run migrations up to that version. I was looking for a way to run just a specific migration.
    Chirag Patel : Actually the command is rake db:migrate:redo VERSION=my_version
  • 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.

    nan : Perfect. Just what I needed.

0 comments:

Post a Comment

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