March 24, 2008 @ 11:36 PM
Rails Migrations in Git Branches
For those of you that don’t stalk follow my other streams:
Migration Buddy
Or, “the inevitable renumbering and conflict-resolution of miggy tardust”
This is a tool to help merge rails branches with conflicting migrations. The basic idea goes like this:
- First you migrate down to the migration you were at when you branched.
- Renumber any new migrations you created that conflict with any new migrations in the target branch.
- Commit the renames.
- Merge to the target branch (usually ‘master’).
- Migrate back up.
Wow, what a pain, right? (Apologies to Saul Williams, you rock).
I keep threatening, but I may have to replace this blog with a combination twitter / tumblr / github stream.

by Hongli Lai on 25 Mar 05:03
Good stuff. :D I’ve always figured that to properly solve the problem of merging migrations, it is best to integrate with the version control system, but I couldn’t think of a good way to do it.
by Jean-Francois Couture on 25 Mar 08:34
I wonder if, instead of this ‘hack’, you tried a plugin like enhanced-migrations that uses timestamps instead of auto increment numbers (and a table to keep track of run migrations)?
I haven’t been using it for long, but it seems to work great.
by James MacAulay on 25 Mar 09:27
This looks nice, and it would be convenient to have in rails. I implemented a similar kind of thing as a standalone script to use as a pre-rebase hook, but didn’t spend much time de-uglifying it:
http://jmacaulay.net/2008/2/14/rails-migrations-in-local-git-branches-not-so-atrocious-now
by Kris Nuttycombe on 25 Mar 10:01
I’ll second the vote for the enhanced-migrations plugin. Our Git repos have usually a minimum of 3 active branches at once (dev, QA, and release plus feature-specific branches) and with enhanced migrations we don’t even have to think about it.
Also, if you use rails.vim, you can use this fix to get it to work with enhanced migrations:
function! s:migrationList(A,L,P) let migrations = s:relglob(“db/migrate/”,”[0-9]”,”.rb”) let migrations = join(map(split(migrations,”\n”),’s:sub(v:val,”^\\d_”,”“)’),”\n”) return s:autocamelize(migrations,a:A) endfunction
tpope wrote that for me upon request – that dude rocks!
by rick on 25 Mar 10:24
Yup, I’ve known about the enhanced migrations plugin for awhile. There’s talk on the core team on adopting UTC timestamps,or going with some newer git-friendly approach.
For now, I like being having my branch’s migrations run in a row, and being forced to review other’s migrations.
by Zach Moazeni on 25 Mar 11:27
We use the enhanced_migrations plugin, but still ran into database state collisions between feature branches. One modifies the database, and assume the code to back up the schema change, (validations, callbacks, etc).
To solve this, I wrote a post-checkout hook for git 1.5.4 that changes our database.yml file based on which branch we moved to. Soon I’m hoping to clean it up and share it soon (wrote it last night).
by Chris on 26 Mar 09:38
This is super awesome, thanks for releasing it.
by Helder on 27 Mar 14:04
Just found out about this after telling my friend about a similar hack I did: http://obvio171.wordpress.com/2008/03/26/rails-vs-scm-resolving-conflicts-between-local-and-upstream-migrations/
Mine doesn’t scale to more than one branch though. I use it mainly to resolve conflicts between the central (svn) repo and my local git repo.
@Zach: This looks like a great idea! It’s a natural extension of the problems, having different dbs for different branches. Please let us know when you’ve released it!
by Pat Nakajima on 28 Mar 10:00
Hey Rick, if you’re looking to consolidate feeds, you might want to check out my little open source tumblog project:
http://github.com/nakajima/aintablog/tree/master
You can put in Twitter/Blog/Flickr feeds and it’ll aggregate them. I’m running it at blog.patnakajima.com right now.
by rick on 29 Mar 21:45
Well, the other part of that was using a service I didn’t have to babysit. Perhaps I can use some of your normalizer code and generate static files? Sounds like a project for my next free weekend…
by VitalieL on 31 Mar 08:12
It would be nice to integrate it in git-rails gem
by rick on 31 Mar 14:05
Pat: Hey look, tumblr splices other feeds for me: http://technoweenie.tumblr.com/
by rick on 01 Apr 10:15
VitalieL: hey it’s licensed under WTFPL, so anyone’s free to do so if they wish!
Seriously though, I’ve never used the git-rails gem…