Looks like the new rails version has "change" versus self.up and self.down methods.
So what happens when one has to roll back a migration how does it know what actions to perform. I have the following method that I need to implement based on an online tutorial:
class AddImageToUsers < ActiveRecord::Migration def self.up add_column :users, :image_file_name, :string add_column :users, :image_content_type, :string add_column :users, :image_file_size, :integer add_column :users, :image_updated_at, :datetime end def self.down remove_column :users, :image_file_name, :string remove_column :users, :image_content_type, :string remove_column :users, :image_file_size, :integer remove_column :users, :image_updated_at, :datetime end end
How can I do the same using the new change method?
When you run rails db:migrate, the up method is executed, whereas rails db:rollback executes the down method. Simply, up runs the migration, down rolls the migration back.
If tablename is the name of your table, fieldname is the name of your field and you want to change from a datetime to date, you can write a migration to do this. Show activity on this post. Step 2: Go to /db/migrate folder and edit the migration file you made.
timestamps was added to the migration by Rails and it added the columns created_at and updated_at to the events table. These special columns are used by Rails to keep track of when a record is created and updated.
You can run db:migrate:down and db:migrate:up with one command, which is db:migrate:redo . You can check if a specific migration is reversible. If not, ActiveRecord::IrreversibleMigration error will be raised.
For many operations rails can guess what is the inverse operation (without problems). For example, in your case what is the reverse operation of add_column
to call when you rollback? Of course it's remove_column
. What is the inverse of create_table
? It's drop_table
. So in these cases rails know how to rollback and define a down
method is superfluous (you can see in the documentation the methods currently supported from the change method).
But pay attention because for some kind of operation you still need to define the down
method, for example if you change the precision of a decimal column how to guess the original precision on rollback? It's not possible, so you need to define the down
method.
As said, I suggest you to read the Rails Migrations Guide.
Better to use Up, Down, Change:
On Rails 3 (Reversible): which should add new column on up and fill all records in table only on up, and only delete this column on down
def up add_column :users, :location, :string User.update_all(location: 'Minsk') end def down remove_column :users, :location end
But:
You had to avoid using change method which allows to save some time. For example, if you didn’t need to update column value immediately after it’s adding you would cut this code down to like this:
def change add_column :users, :location, :string end
On up it will add column to table and remove it on down. Much less code and it’s a profit.
On Rails 4: one more useful way to write what we need in one place:
def change add_column :users, :location, :string reversible do |direction| direction.up { User.update_all(location: 'Minsk') } end end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With