views:

405

answers:

3

Is there a best practice naming convention for Rails migrations, particularly when editing a model?

e.g. if I'm adding a column bar to the Foo model, should I name it edit_foo or add_bar_to_foo

I'm assuming if I'm editing mutliple models then I should create multiple migrations, but what if I'm making multiple modifications to a single model, do I name it add_bar_remove_x_edit_y_to_foo?

A: 

The point is readability- to find out fast what the migration is responsible for.. if you write too much "data" in the name, it makes it harder to scan and you shoot yourself in the foot.

So.. if it's 1-2 changes, write it in the name, if there are too many changes, write update_foo_model (or edit_foo_model)

amikazmi
The problem with that is if you have to make a similar change later on in life. You can't have migrations with the same name.
Omar Qureshi
Indeed, it's a straight forward solution for most of the cases, I think the simplicity of the common case is worth it.
amikazmi
+2  A: 

I agree with the previous poster. The naming should focus on readability. But also keep in mind that you can't (nor should) have two migrations with the same name.

So, general names like edit_foo_model is generally not a good idea (since, what happens when you want to add more columns to that model), then it would be better to group the columns into what the purpose is, like update_foo_for_bar_support. You can usually skip adding model since, well, everyone knows that migrations do handle models, so there is no need to mention that in the name (that is, update_foo instead of update_foo_model).

Also, what I usually do is to keep different changes separated. So, if there are multiple different changes in a model, I'd separate them into different migration files, one for adding columns and one for removing columns for instance.

Jimmy Stenke
It sounds like the gist of what your saying is come up with sensible names that reflect the changes you're making to the model, rather than generic edit_foo or verbose add_bar_to_foo. e.g. add_bar_support_to_foo. And if your migration is making multiple, unrelated changes then split it up.That makes sense :)
roryf
+2  A: 

I would split up multiple schema changes in multiple migrations! Then you can easyly name the single migrations!

Lichtamberg