1. Add your migration file
There is a rails tool to help you make the file. For example, if I wanted to add an age column to the users table, I would run:
> rails g migration add_age_to_users age:integer
invoke active_record
create db/migrate/20161005225132_add_age_to_users.rb
Open the created file and add your migration code.
# db/migrate/20161005225132_add_age_to_users.rb
class AddAgeToUsers < ActiveRecord::Migration
def change
add_column :users, :age, :integer, default: 0, null: false
end
end
2. Migrate your local database
> rake db:migrate
== 20161005225132 AddAgeToUsers: migrating ====================================
-- add_column(:users, :age, :integer)
-> 0.0017s
== 20161005225132 AddAgeToUsers: migrated (0.0018s) ===========================
Ensure that this succeeds as expected.
3. Test rolling back
> rake db:rollback
== 20161005225132 AddAgeToUsers: reverting ====================================
-- remove_column(:users, :age, :integer)
-> 0.0016s
== 20161005225132 AddAgeToUsers: reverted (0.0018s) ===========================
Ensure that this is a succeeds as expected. Most migrations are automatically reversable. Others can be made automatically reversable by adding additional options.
4. Re-migrate the database
> rake db:migrate
again
5. Commit your changes
When you run rake db:migrate
it will change the db/schema.rb
file. Ensure that the only change to that file is the version number at the top, and the change you made in the migration file.
For this example, the change would look like this:
Note:
If there are other changes, please do not commit them. They are there because your local database and the production database are out of sync.
Please use the bin/restore-snapshot
tool to download and restore a local copy of the database. Then re-migrate your changes.
DONE THANKS!