重新运行Heroku上的所有数据库迁移(rake db:migrate:reset)

Don*_*n P 2 ruby-on-rails heroku ruby-on-rails-4

在开发heroku应用程序时,在启动之前,我一直将迁移保存到2个文件(创建用户,创建帖子),而不是每次更改表的结构时添加新的迁移(此时可能会进行20次迭代).数据库中还没有数据.

如何rake db:migrate:reset在Heroku上做同等的事情?Heroku不允许这样做.我想我可以做的heroku pg:reset,然后rake db:migrate,但这似乎并没有重新运行老迁移.同样,Heroku的数据库中没有数据,因此可以删除它.

迁移:

  • 2014070781234_create_users_table 我在此迁移中添加了一列.它已经在heroku上运行了.如何重新运行表以便更新表?
  • 2014070789999_create_posts_table

Man*_*eep 10

您不应对已迁移的迁移文件进行更改.如果你看docs它说

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

您应该创建一个新的迁移文件

rails g migration AddColumnNameToUsers column_name:type
Run Code Online (Sandbox Code Playgroud)

这将生成一个迁移文件,如:

class AddColumnNameToUsers < ActiveRecord::Migration
  def change
    add_column :users, :column_name, :type
  end
end
Run Code Online (Sandbox Code Playgroud)

然后你可以简单地运行heroku run rake db:migrate

如果您仍想从heroku删除数据库,则需要执行以下操作:

heroku pg:reset DATABASE_URL --confirm nameofapp
Run Code Online (Sandbox Code Playgroud)

然后heroku运行rake db:migrate.有关详细信息,请查看heroku文档