已删除的迁移和 Heroku

Posted

技术标签:

【中文标题】已删除的迁移和 Heroku【英文标题】:Deleted migrations and Heroku 【发布时间】:2013-01-26 22:20:45 【问题描述】:

几天前,我为 Rails 创建了一个迁移以添加 Paperclip 头像,但最终走向了不同的方向。作为 Rails 新手,我不知道像我一样删除迁移文件是多么糟糕的想法。

我的应用程序在本地运行良好,但是当运行 heroku run rake db:migrate 时,我得到了这个:

undefined method `attachment' for #<ActiveRecord::ConnectionAdapters::Table:0x000000046092e0>

这是因为它正在尝试运行一个名为 AddAttachmentAvatarToVenues 的迁移,这是我愚蠢地删除的迁移。

它还在将已删除迁移中指定的头像添加到schema.rb 的列,但我创建了一个新迁移来摆脱这些。新的迁移摆脱了它们,但没有改变 heroku 迁移错误。

知道如何解决这个问题吗?我做了很多谷歌搜索和环顾四周,虽然有很多人有类似的错误,但他们主要是他们使用的命令有问题。

这是在我的 heroku 迁移中尝试删除迁移后的输出。

==  AddAttachmentAvatarToVenues: migrating ====================================
-- change_table(:venues)
rake aborted!
An error has occurred, this and all later migrations canceled:

undefined method `attachment' for #<ActiveRecord::ConnectionAdapters::Table:0x00000003bdb7c8>
/app/db/migrate/20130206222434_add_attachment_avatar_to_venues.rb:4:in `block in up'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/schema_statements.rb:243:in `change_table'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:466:in `block in method_missing'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:438:in `block in say_with_time'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:438:in `say_with_time'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:458:in `method_missing'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:334:in `method_missing'
/app/db/migrate/20130206222434_add_attachment_avatar_to_venues.rb:3:in `up'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:370:in `up'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:410:in `block (2 levels) in migrate'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:410:in `block in migrate'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:129:in `with_connection'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:389:in `migrate'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:528:in `migrate'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:720:in `block (2 levels) in migrate'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:775:in `call'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:775:in `block in ddl_transaction'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/transactions.rb:208:in `transaction'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:775:in `ddl_transaction'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:719:in `block in migrate'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:700:in `each'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:700:in `migrate'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:570:in `up'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/migration.rb:551:in `migrate'
/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.11/lib/active_record/railties/databases.rake:179:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

我确实看到了输出有什么问题,我只是不确定如何在不搞砸的情况下修复它。

编辑:这是文件结构的一些截图:

(这两个名称相似的原因是我忘记删除一列,但这是在我遇到此问题之后,并且没有任何影响)

EDIT2:

这是从我的 git 历史记录中删除的迁移。在此之后我添加了更多频道。它们只是几个字符串,但如果这能有所作为,我会找到一个更新的版本。

class AddAttachmentAvatarToVenues < ActiveRecord::Migration
  def self.up
    change_table :venues do |t|
      t.attachment :avatar
    end
  end

  def self.down
    drop_attached_file :venues, :avatar
  end
end

提前致谢!

【问题讨论】:

你能显示迁移吗? 这就是问题所在,我从迁移文件夹中删除了它,但我不确定它还会来自哪里。 它不在这里? /app/db/migrate/20130206222434_add_attachment_avatar_to_venues.rb 不,它似乎不在本地的 migrate 文件夹中或我正在推送的 git 文件中。但是这很奇怪,因为我的迁移文件夹不在 /app/ 中,是否有我缺少的配置或设置?我用几个屏幕截图编辑了我的问题,希望这会有所帮助。非常感谢您帮助我! 如果你这样做会发生什么rake db:rollback 【参考方案1】:

也许你应该看看这个:How to empty DB in heroku

你所有的普通命令也可以在heroku中使用,唯一的区别是你必须把heroku run放在它前面。

如果您的应用程序尚未上线,您可以简单地重置数据库:

heroku pg:reset SHARED_DATABASE --confirm NAME_OF_THE_APP

然后重新创建它,使用:

heroku run rake db:migrate

为数据库播种:

heroku run rake db:seed

最后,重启 Heroku:

heroku restart

附:如果这些步骤没有帮助,您可以尝试在删除数据库后运行“heroku run rake db:setup”

【讨论】:

感谢您的帮助,不幸的是,当我这样做时,AddAttachmentAvatarToVenues: migrating 仍然尝试并中止,导致我的其余迁移也像以前一样中止。 你可以试试:heroku run rake db:rollback。因此,您的迁移将回滚到开始。然后,您可以创建具有所需属性的迁移 AddAttachmentAvatarToVenues。最后,您可以再次运行 heroku run rake db:migrate。 替代方案:git rm [migration_number]_create_users.rb git push heroku master heroku run rake db:drop => heroku run rake db:create => heroku run rake db:migrate,见:***.com/questions/11023645/…

以上是关于已删除的迁移和 Heroku的主要内容,如果未能解决你的问题,请参考以下文章

删除 Rails 应用程序并将其重新部署到 heroku

删除项目后的僵尸heroku进程?

ruby on rails Heroku 迁移错误

如何在已部署的 Heroku 应用上查看文件和文件结构

Heroku 不会在 Django 中迁移模型

已删除的迁移文件仍在迁移