Don P
Don P

Reputation: 63758

Rerun all db migrations on Heroku (rake db:migrate:reset)

While developing a heroku app, before launch, I have been keeping my migrations into 2 files (Create Users, Create Posts) instead of adding a new migration everytime I change the structure of the tables (probably would be 20 iterations at this point). There is no data in the db yet.

How can I do rake db:migrate:reset's equivalent on Heroku? Heroku does not allow this. I was thinking I could do heroku pg:reset and then rake db:migrate, but this doesn't seem to rerun old migrations. Again, there is no data in Heroku's database, so it can be dropped.

migrations:

Upvotes: 2

Views: 1779

Answers (1)

Mandeep
Mandeep

Reputation: 9173

You should not make changes in your migration files which have already been migrated. If you look at docs it says

In general, editing existing migrations is not a good idea. You will be creating extra work for yourself and your co-workers and cause major headaches if the existing version of the migration has already been run on production machines. Instead, you should write a new migration that performs the changes you require

You should create a new migration file by

rails g migration AddColumnNameToUsers column_name:type

This will generate a migration file like:

class AddColumnNameToUsers < ActiveRecord::Migration
  def change
    add_column :users, :column_name, :type
  end
end

And then you can simply run heroku run rake db:migrate

If you still want to Drop your database from heroku then you need to do:

heroku pg:reset DATABASE_URL --confirm nameofapp

and then heroku run rake db:migrate. For details checkout heroku docs

Upvotes: 13

Related Questions