迁移不适用于 Heroku [英] Migrate not working on Heroku

查看:14
本文介绍了迁移不适用于 Heroku的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Heroku 上运行 pg:reset 并尝试运行 db:migrate,所有迁移都运行,但迁移失败并显示以下错误和跟踪:

I've run pg:reset on Heroku and on trying to run db:migrate, all migrations run but the migration fails with the following error and trace:

rake aborted!
Error dumping database
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0.rc2/lib/active_record/tasks/postgresql_database_tasks.rb:55:in `structure_dump'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0.rc2/lib/active_record/tasks/database_tasks.rb:142:in `structure_dump'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0.rc2/lib/active_record/railties/databases.rake:288:in `block (3 levels) in <top (required)>'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0.rc2/lib/active_record/railties/databases.rake:51:in `block (2 levels) in <top (required)>'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0.rc2/lib/active_record/railties/databases.rake:45:in `block (2 levels) in <top (required)>'

这里可以看到 有问题的一行和上面的一行是:

As can be seen here the problematic line and the one above it are:

command = "pg_dump -i -s -x -O -f #{Shellwords.escape(filename)} #{search_path} #{Shellwords.escape(configuration['database'])}"
raise 'Error dumping database' unless Kernel.system(command)

这在本地工作,在开发和生产环境中都没有任何问题.

This works locally without any problems, both in development and production environments.

有人遇到过这种情况吗?

Has anyone experienced anything like this?

推荐答案

这是一个有趣的错误,结果证明可以忽略.基于它正在尝试执行 db:structure:dump 的事实,您正在使用sql"作为您的 active_record.schema_format.rake 任务 db:structure:dump 将在 heroku 上失败,因为 pg_dump(不出所料)不在二进制路径中.有趣的是,Heroku 声明不支持 db:schema:dump 但如果您将架构格式设置为 ruby​​,它就可以工作很好.

This is an interesting bug that, as it turns out, can be ignored. Based on the fact it's attempting to do a db:structure:dump, you are using 'sql' as your active_record.schema_format. The rake task db:structure:dump will fail on heroku because pg_dump is (unsurprisingly) not in the binary path. Interestingly enough, Heroku states that db:schema:dump is not supported but if you set the schema format to ruby, it works fine.

在 Rails 3 中,dump 任务只会在命令的退出代码为 1 时引发错误.在基于 unix 的系统上,如果找不到该命令,则退出代码为 127.因此即使 pg_dump 命令失败rails 3(确实如此),它不会引发错误,也不会停止 rake 任务.所以任何在 Rails 3 中使用 sql 模式格式的人都不会遇到这个问题,因为它会默默地失败.Rails 4 中的重构以在转储失败时正确引发错误导致 db:migrate 在 Heroku 上引发错误.然而,即使 rake aborted 出现错误,ddl 实际执行并提交.

In Rails 3, the dump task would only raise an error is the exit code of the command was 1. On unix based systems if the command is not found, the exit code is 127. So even if the pg_dump command fails on rails 3 (which it does), it won't raise an error and it won't halt the rake task. So anyone using a sql schema format with Rails 3 wouldn't have this issue because it would fail silently. The refactor in Rails 4 to properly raise an error if the dump failed causes db:migrate to raise an error on Heroku. However, even though it errors with rake aborted the ddl is actually performed and committed.

可能的解决方案:

  • 在迁移实际运行时忽略错误.
  • 由于您不关心生产中的结构转储,请将 schema_format 设置为 ruby​​.在 config/environments/production.rb 中:

config.active_record.schema_format = :ruby

  • 如果由于某种原因您不想更改配置文件:添加一个包含以下内容的 rake 任务以抑制错误:

  • If for some reason you don't want to change the config file: add a rake task with the following to suppress the error:

    if Rails.env == 'production'
        Rake::Task["db:structure:dump"].clear
    end
    

  • 这篇关于迁移不适用于 Heroku的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆