Renumber clashing Rails migrations in one easy step

Tagged:  •    •  

If you work in a team on a Rails project, chances are that you and your team mates occasionally create identically-numbered migrations. Renumbering a migration that you're working on is a pain, unless you're using this handy 'renumber_migrations' plugin.

Scenario

You're working on migration 045_add_my_new_feature.rb, when one of your team-mates checks 045_some_other_migration.rb into svn. You notice the numbering clash (or 'rake db:migrate' notices it for you), and you simply run the db:migrate:renumber task:

% rake db:migrate:renumber

Voilá - your migration is renumbered to 046, and the database schema is left at version 044 ready for you to re-test your migration. (If you had multiple new migrations, all of them would get renumbered.)

Requirements and assumptions

To be able to use this plugin, you must:

  1. Be using subversion for your version control
  2. Have a working #down method for each local clashing migration
  3. Have network access to the subversion server, since clashing migrations are temporarily removed locally, and later re-requested.

Getting it

You can use script/plugin to install the plugin:

% script/plugin install http://rails.sanityinc.com/plugins/renumber_migrations/

Alternatively, you can download a snapshot from the plugin's git repository instead (tip: that page has an RSS feed for tracking check-ins). Unpack the snapshot tarball under your RAILS_ROOT/vendor/plugins directory.

More

If you like the look of this plugin, why not subscribe to my feed?

Only works with Subversion, but the plugin is stored in Git? That's some trick!

Have you attempted a version for those of us who are fully gitified?

I've been thinking about how to make it work with git, but not actually bashed out an implementation.

The tricky part is that in Subversion, it's easy to see which changes are local and which changes are upstream. In git, however, it's practically mandatory to commit new migrations locally before fetching new clashing migrations from an upstream tree, at which point this plugin would be unable to know which of the clashing migrations is to be renumbered.

When using git-svn, one could at least look to see which commits are "local", ie. have no git-svn-id, but that doesn't solve the general case.

Rick Olson has written something up to handle migration numbers in Git. Thought you might be interested.

http://weblog.techno-weenie.net/2008/3/25/rails-migrations-in-git-branch...

Cheers

A colleague pointed that out to me last week. Looks nifty.

Syndicate content