【问题标题】:heroku rails app database migration issuesheroku rails 应用程序数据库迁移问题
【发布时间】:2012-02-03 02:03:08
【问题描述】:

我最近在我的 rails 应用程序中添加了设计可确认模块。我的应用程序在开发中运行良好。但是,当我把它推到 heroku 时,它说“我们很抱歉,但出了点问题。”。以下是我的heroku日志-

Rendered devise/confirmations/new.html.erb within layouts/application (2.7ms)
2012-02-03T01:55:07+00:00 app[web.1]: Rendered devise/shared/_links.erb (0.5ms)
2012-02-03T01:55:07+00:00 app[web.1]: cache: [GET /users/confirmation/new] miss
2012-02-03T01:55:07+00:00 app[web.1]: Completed 200 OK in 19ms (Views: 16.9ms | ActiveRecord: 0.0ms)
2012-02-03T01:55:07+00:00 heroku[router]: GET personaldiary.herokuapp.com/users/confirmation/new dyno=web.1 queue=0 wait=0ms service=42ms status=200 bytes=1540
2012-02-03T01:55:11+00:00 app[web.1]: 
2012-02-03T01:55:11+00:00 app[web.1]: 
2012-02-03T01:55:11+00:00 app[web.1]: Started POST "/users/confirmation" for 50.131.164.83 at 2012-02-03 01:55:11 +0000
2012-02-03T01:55:11+00:00 app[web.1]:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"iVVTpWgIrBTR8b9k07lr2tYDFQfAYD0R8JmGVkmfzl4=", "user"=>{"email"=>"hrishikeshp19@gmail.com"}, "commit"=>"Resend confirmation instructions"}
2012-02-03T01:55:11+00:00 app[web.1]:   Processing by Devise::ConfirmationsController#create as HTML
2012-02-03T01:55:11+00:00 app[web.1]: Completed 500 Internal Server Error in 4ms
2012-02-03T01:55:11+00:00 app[web.1]: 
2012-02-03T01:55:11+00:00 app[web.1]: NameError (undefined local variable or method `confirmed_at' for #<User:0x000000023254e0>):
2012-02-03T01:55:11+00:00 app[web.1]:   
2012-02-03T01:55:11+00:00 app[web.1]: cache: [POST /users/confirmation] invalidate, pass
2012-02-03T01:55:11+00:00 app[web.1]: 

很明显,数据库没有“confirmed_at”列。我的迁移文件如下:

class AddConfirmableToDeviseV1 < ActiveRecord::Migration
  def up
   add_column :users, :confirmation_token, :string
   add_column :users, :confirmed_at, :datetime
   add_column :users, :confirmation_sent_at , :datetime

   add_index  :users, :confirmation_token, :unique => true
  end

  def down
    remove_index  :users, :confirmation_token

    remove_column :users, :confirmation_sent_at
    remove_column :users, :confirmed_at
    remove_column :users, :confirmation_token
  end
end

我的作品托管在 heroku 上:

所以我跑了

heroku run rake db:migrate

它没有给我任何输出。

然后我跑了

heroku run rake db:rollback

它给了我

==  AddConfirmableToDeviseV1: reverting =======================================
-- remove_index(:users, :confirmation_token)
rake aborted!
An error has occurred, this and all later migrations canceled:

Index name 'index_users_on_confirmation_token' on table 'users' does not exist

Tasks: TOP => db:rollback

还有,

heroku run rake db:migrate --trace

让我关注

** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Invoke rails_env (first_time)
** Execute rails_env
** Execute db:load_config
** Execute db:migrate
** Invoke db:schema:dump (first_time)
** Invoke environment 
** Invoke db:load_config 
** Execute db:schema:dump

任何想法如何解决这个问题?

【问题讨论】:

  • 你能用 --trace 运行 db:migrate 看看有没有输出吗?
  • ** 调用 db:migrate (first_time) ** 调用环境 (first_time) ** 执行环境 ** 调用 db:load_config (first_time) ** 调用 rails_env (first_time) ** 执行 rails_env **执行 db:load_config ** 执行 db:migrate ** 调用 db:schema:dump (first_time) ** 调用环境 ** 调用 db:load_config ** 执行 db:schema:dump
  • 您是否在 Heroku 中设置了多个应用程序?
  • 你在使用 Postgres 和 Heroku 吗?您可以登录数据库并确保 schema_migrations 表中的最新迁移等于或小于您生成的迁移吗?
  • 投反对票的人能说出投反对票的理由吗?

标签: ruby-on-rails heroku devise


【解决方案1】:

您肯定需要确保您的迁移已针对生产数据库运行。

RAILS_ENV=production rake db:migrate

这假设你的本地环境当然可以访问生产数据库。

(注意:这是一个通用注释 - 不是特定于 heroku 的答案 - “heroku run rake db:migrate”显然是针对生产 Heroku 环境运行迁移的正确方法)

【讨论】:

  • 我运行了 rake db:migrate。它什么也没给我。我跑了 rake db:rollback。它说。表 'users' 上的索引名称 'index_users_on_confirmation_token' 不存在
  • 这不是必需的,因为 Heroku 应该默认为生产环境,它会用自己的数据库替换你的 database.yml
【解决方案2】:

问题解决了。

我删除了我的迁移文件。在迁移更改方法中添加了带有设计助手的新迁移文件。

class AddConfirmableToDevise < ActiveRecord::Migration
    def change
        change_table(:users) do |t|
            t.confirmable
        end
        add_index :users, :confirmation_token,   :unique => true
    end
end

再次运行迁移

heroku run rake db:migrate

哇……它奏效了。我不知道这有什么意义,但我很高兴它奏效了。请留下 cmets,我热衷于阅读解释。

无论如何,谢谢大家。

【讨论】:

  • 我不确定,但是 heroku 似乎不支持 up 和 down 方法。谁能确认一下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-21
  • 1970-01-01
  • 2011-07-19
  • 2019-04-16
  • 2011-10-19
  • 1970-01-01
相关资源
最近更新 更多