Increment Ruby on Rails migration number

We recently needed to create a new database migration, and needed to insert it into at certain position. Normally what I have done in the past is to alter the migration index of an existing file or two.

But this time, it needed to go in at 003. I was going to have to manually increment about 30 or 40 migration scripts. So, it was time to apply my Ruby skills to do it automatically.

Here is the result:

So when you run it, you will get output such as:

Instructions

  1. Cut and paste the file, or download it from increment_migrations.rb.
  2. Save it somewhere
  3. Make it executable chmod 755 /path/to/increment_migrations.rb
  4. The change directory to ./db/migrations
  5. then run /path/to/increment_migrations.rb 3
  6. and if the output looks as expected
  7. finally run /path/to/increment_migrations.rb 3 RUN

Finally, thanks to the client for permission to publish. Thanks for giving back to the community!

One Response to “Increment Ruby on Rails migration number”

  1. bryanl says:

    Don’t fret. With Rails 2.1, the migration numbers will default to date based, so all you will have to do is rename it to an earlier date.

Leave a Reply