0

Is it bad practice that I don't write the down steps of irreversible migrations, such as drop_table commands? Do migrations have to be completely reversible?

picardo
  • 24,530
  • 33
  • 104
  • 151

1 Answers1

0

They "have" to be reversible only if you are ever going to reverse one or more of them...which in all honesty, WILL happen at least once during your development. You should always strive to make reversible migrations, as you never know when you might need it.

The thing that kicks you in the nuts so to speak is that your data may not survive reversed migrations. i.e., Simply adding a column and populating it with data only to have to reverse it will cause you to lose all of that data in the reversal. Going forward again won't repopulate the data. Don't reverse with impunity.

That being said, rails provides a change method which - as far as structure is concerned - in a lot of cases provides the reversal for you. It doesn't work in all situations though.

  • For destructive migrations such as dropping tables or columns, I usually don't write them at the same time as the code change. Instead I remove the model and make all the code changes, deploy, and only after some period of time do I separate write a migration to clear the data. I do these in batches and keep backups before they run just to be extra safe. I have an external spreadsheet for this purpose. Yeah it's a bit tedious but it has saved my ass more than once. – gtd May 25 '14 at 13:03