Should I delete migration after rollback

2019-03-31 06:40发布

I'm fairly new to ruby and rails and am just getting my head around migrations.

My question is what is the best practice or right time to delete a migration after a rollback. So far what I have read is a matter of opinion whether you delete a migration after a rollback, but is there any major implications to deleting a migration when working in a team, and are there any benefits to leaving the migration file as opposed to deleting it?

In my case what would make most sense?

I had my original migrate file 20140731141350_create_users.rb

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :email
      t.string :password

      t.timestamps
    end
  end
end

To which I needed to add a salt column, so I created the migration 20140804125449_add_salt_colum_to_users.rb

class AddSaltColumToUsers < ActiveRecord::Migration
  def change
    add_column :users, :salt, :string
  end
end

But during development I realised the salt column wasn't necessary and performed

rake db:migrate:down VERSION=20140731141350

Now I am left with an unused 20140804125449_add_salt_colum_to_users.rb migrate file.

Delete or no?

3条回答
Luminary・发光体
2楼-- · 2019-03-31 07:03

You should never change your old migrations. If you realised that given column is unnecessary, write a new migration to remove it.

The reason for this is so that you can restore database schema in any point of development. If you are working in a team, the fact that you removed the migration won't change your teammates' schemas. Even more, they have no migration to revert now!

查看更多
神经病院院长
3楼-- · 2019-03-31 07:07

Have a look at this blog post about rails migrations. As stated in this article:

When I fire up a new environment, it's much easier to run rake db:schema:load. And the migrations are redundant. All of that data is in the schema file.

Thus I recommend that you delete this migration if no other migration relies on this one.

查看更多
We Are One
4楼-- · 2019-03-31 07:18

Never delete or edit your old migrations! You can bring a lot of harm to your app working with a team or even alone.

查看更多
登录 后发表回答