user3277633
user3277633

Reputation: 1923

adding a new column in a rails database migration

So I've been going through a lot of rails tutorial, and I get that the default for adding a new column to a database is , for example,

rails generate migration add_reset_to_users reset_digest:string reset_sent_at:datetime

The above will add a reset_digest in the form of a string and reset_sent_at in the form of a date to the migration add_reset_to_users

My questions is what if I am clumsy one night at 4 AM and only call the following

rails generate migration add_reset_to_users reset_digest:string

I completely forgot about reset_sent_at but want to implement it the next morning. I made the mistake of adding the link directly to the db file, which was a huge mistake.

In this case what should I do? Do I simply call a new migration such as

rails generate migration add_reset_sent_to_users reset_sent_at:datetime

or is there an even better way?

Upvotes: 0

Views: 2095

Answers (3)

doz87
doz87

Reputation: 601

I think it depends what state your rails app is in.

If you were working on a production app then editing migrations is not advisable due to data loss and changes should be made with a new migration.

If your working locally in development then I would edit the migration directly and add the missing column and rerun your migration.

Don't be worried about editing you migrations, just remember to rake db:rollback the migration you are editing before making changes or you will encounter errors.

This is where changing your migration from:

def change

    add_column('users', 'reset_digest', :string) 
    add_column('users', 'reset_sent_at', :datetime) # Would have to perform rollback before adding this line


end 

to:

def up

    add_column('users', 'reset_digest', :string) 
    add_column('users', 'reset_sent_at', :datetime) # Added after migration

    **rake db:migrate

end

def down

    remove_column('users', 'reset_digest', :string) 
    remove_column('users', 'reset_sent_at', :datetime) # Add this after rollback
   **rake db:rollback

end

Allows you to make changes to your migrations before you rake db:rollback

This requires a bit more code but I find it easier when I'm building a new app and things are changing frequently.

Upvotes: 0

Raza
Raza

Reputation: 2388

first, if you have not run your migration, you can directly open the migration file, and add your column to the file, as

def change
 add columns :table_name :column_name :column_type
end

In your case, you will modify the file as,

def change
 add columns :users :reset_digest :string
 add columns :users :reset_sent_at :datetime
end

and then run

rake db:migrate

if you have already ran your migration, and you have not run any other migration after that, you can undo it, by

rake db:rollback STEP=1

and then edit the migration file, and run your migration

Upvotes: 2

Surya
Surya

Reputation: 16022

Rule of thumb for migrations in Rails is that you always create a new migration file unless you have not already shared your code with others, i.e. pushed to remote repository, otherwise you can just change the old migration after running $ rake db:rollback and everything will be fine, and nobody will know about it, and won't affect other developers work(since it's still on your local repository).

So, I'd encourage you to create a new migration if you have already committed and pushed the code onto remote repository, and changing the old migration file again will hurt other developers productivity. In case of any confusion, always create a new migration:

rails generate migration add_reset_sent_to_users reset_sent_at:datetime

Upvotes: 0

Related Questions