"Hmm, this feature will require a big change to my database! I mean, we're going to have to touch every single record in that table."
If you have, you've probably followed up with this thought:
"Good thing I'm using Rails! They make it so easy!"
And then you went and wrote this migration:
Model.all.each do |m|
#..some important function
#performed on every object..
And then you were really proud of how quickly that went, and you run it on your development machine, and it works really well. But THEN you push it to your staging or production server that has way more data than your dev machine, and you get this staring back at you from your command line:
** [out :: 18.104.22.168:8063] ==
** YourCrazyMigration: migrating
And you stare at that for about 30 seconds before shouting:
"Mother F%^&er! I did it again! I can't believe I did it again! I built a stupid migration that uses the stupid 'all' method which is now dominating the memory on that box and I can either kill it and pick up the pieces or let it run for the next 3 hours as it pages the hell out of the hard disk!"
Well, since I did EXACTLY THAT just now, I decided that from now on we'll be using a new migration task at our development shop called "safety_migrate", which you're welcome to take advantage of if you'd like. It runs through every file in your migration directory, checking for the dreaded "all" method, and WILL NOT run the migrations unless every file is clean!
check the gist: