Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I delete migration after rollback

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?

like image 269
SteWoo Avatar asked Feb 13 '23 08:02

SteWoo


2 Answers

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!

like image 62
BroiSatse Avatar answered Feb 14 '23 21:02

BroiSatse


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.

like image 29
Dimitri Avatar answered Feb 14 '23 23:02

Dimitri