The concept of db migrations is new to me, but it's pretty awesome at the same time. :)
I'm curious about the proper use of migrations in that I need to know how to properly add/remove/modify columns in tables via migrations. In the past, I've always directly edited the databases via a gui. :/
What's the best way to add a new column? I've tried using rails g migration :new_col => :attributes
, but this seems like a messy thing to do if I'm going to be creating/removing/fiddling with many columns.
My biggest fear right now is learning how to do this with a live database--I can't go around hazardously editing the live database, but I need to be able to add, say new user_profile fields and whatnot.
I'm interested to hear first specifically the best way to go about this, and secondly your recommen开发者_开发知识库dations and past experience in doing this.
Thanks, SO community for basically giving me a quasi education.
When applying multiple database changes, you can separate these into multiple commands on their own lines inside the migration file. If an operation requires calculations, you can also embed Ruby code into these methods, the same as if you were working in any other Ruby class. You can also execute SQL outside of ActiveRecord to do database specific operations, or operations which are too complex for an ORM to manage effectively:
More information on Migration methods can be found here: http://api.rubyonrails.org/classes/ActiveRecord/Migration.html
As for doing this in a live environment, I would first test your migrations in a staging environment that is a mirror of your live environment. If staging is successful, I would then backup the live database before running any migrations in production.
A word of warning, if a migration fails part way through, you can easily get stuck in a half applied state, where rake db:migrate:down will also fail. These operations may require an understanding of DDL syntax (or a GUI program to generate this DDL) to resolve. That is why a good staging dry-run is important before applying migrations in production.
精彩评论