简体   繁体   English

Heroku rake db:migrate

[英]Heroku rake db:migrate

Heroku is telling me that there are migrations that haven't been run, when clearly they have been. Heroku告诉我,有些迁移尚未运行,但显然它们已经存在。 It looks like it is behind one migration. 它似乎落后于一次迁移。 How could I solve this problem. 我怎么能解决这个问题。

When I run rake db:migrate it tells me rake aborted Mysql2::Error: Duplicate column name . 当我运行rake db:migrate它告诉我rake aborted Mysql2::Error: Duplicate column name I know these fields have already been created, also pretty sure that migration ran, as those fields don't exist in any other migration and rake db:migrate runs just fine on my local system. 我知道这些字段已经创建,也很确定迁移已经运行,因为这些字段在任何其他迁移中都不存在,并且rake db:migrate在我的本地系统上运行正常。

How can I fix this? 我怎样才能解决这个问题? I think Heroku just didn't realize it already ran that migration. 我认为Heroku只是没有意识到它已经进行了迁移。 How can I tell it "you already ran migration xxx"? 我怎么能告诉它“你已经运行了迁移xxx”?

That probably means you ran it once, but it failed; 这可能意味着你曾经运行过一次,但它失败了; table alterations in mysql are not transactional, so you can get left in a bad state. mysql中的表更改不是事务性的,因此您可以处于错误状态。 Some of the changes may have run, but not all of them. 某些更改可能已经运行,但并非所有更改都已运行。

The only thing you can do is determine which parts already ran, comment out those line in the migration, commit and push and run the migration, bypassing the parts that already ran. 您唯一能做的就是确定已运行的部分,在迁移中注释掉这些部分,提交并推送并运行迁移,绕过已经运行的部分。

If a migration is incompletely applied, do one of the following: 如果未完全应用迁移,请执行以下操作之一:

  • Use the dbconsole to undo the changes that were applied and then run the migration again, or 使用dbconsole撤消已应用的更改,然后再次运行迁移,或
  • make the remaining schema changes manually using the dbconsole and then insert a record into the schema_migrations table. 使用dbconsole手动更改剩余的架构,然后将记录插入schema_migrations表。

Depend on what types of changes are in the migration script, you'll need to decide which of the above is easiest. 根据迁移脚本中的更改类型,您需要确定上述哪个更容易。 If the changes already applied are destructive changes, such as dropping a column or table, you could recreate the column or table so that the migration will be able to run. 如果已应用的更改是破坏性更改(例如删除列或表),则可以重新创建列或表,以便能够运行迁移。 If the remaining changes are simple to translate into SQL, then that might be easier. 如果剩下的更改很容易转换为SQL,那么这可能会更容易。

Inserting a record into the schema_migrations table will allow the app to think that the migration was successfully applied. 将记录插入schema_migrations表将允许应用程序认为已成功应用迁移。 Do this only if you're absolutely satisfied that the migration changes were completely applied. 仅当您完全满意迁移更改已完全应用时才执行此操作。 It has one column, version , which will need to contain the numeric part of the migration filename. 它有一列, version ,需要包含迁移文件名的数字部分。

http://api.rubyonrails.org/classes/ActiveRecord/Migration.html http://guides.rubyonrails.org/migrations.html http://api.rubyonrails.org/classes/ActiveRecord/Migration.html http://guides.rubyonrails.org/migrations.html

Finally, this is an example of why you should use Postgres instead of MySQL. 最后,这是为什么你应该使用Postgres而不是MySQL的一个例子。 Postgres is able to roll back (most) schema changes in a transaction, so you wouldn't be left with a half-applied migration. Postgres能够在事务中回滚(大多数)架构更改,因此您不会留下半应用的迁移。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM