Migration Troubles

I was always wondering why my migration files were numbered 1, 3, 5, 7, ... What is the secret reason behind choosing only odd numbers? Does rails trie to only use prime numbers?

Well, the last possibility was ruled out quickly, as 1 is not a prime number and 2 is one (Knuth once said: “2 is the only even prime number, but it is the oddets of all”).

But wait, things became even more mysterious: Recently I wanted to create a new migration for the first time after updating to 0.14.2 and rails generated a file with a number that already existed.

A quick look at the source and the history of the migrations generator explained everything:

Up to 0.14.2 the generator simply counted all the files in the migrations directory and added one to this number. Since my editor creates backup files by appending a ”~” character, I always had twice as many files as migrations and if you add one, you get an odd number.

Now, in 0.14.2 (or 0.14.1) they thanged this, to only count ”*.rb” files. Everything is great now, you think? Not at all! Now, rails correctly recognized how many migrations I have, but the number it used for the next migration simply was too low, as my migrations weren’t numbered consecutively in my case.

Luckily the trunk contains a new version that looks for the migration with the biggest number and increments this by one. Finally, what should you do, until this fix is included in a new release? Simply rename the migration file generated by rails to a sane number.