【问题标题】:Unable to run migration scripts无法运行迁移脚本
【发布时间】:2018-07-08 02:37:48
【问题描述】:

近两年来,我一直在 Windows 上使用 Ruby on Rails 创建一个应用程序,我已经拥有它。这个周末我开始将我的开发环境迁移到 Ubuntu,因为它可以运行一些 Windows 中不可用的工具/服务。

我尝试运行迁移并收到此错误消息:

$ bundle exec rake db:migrate

rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

Directly inheriting from ActiveRecord::Migration is not supported. Please specify the Rails release the migration was written for:

  class AddAttachmentImportCsvFileToTabClientProjectCommunities < ActiveRecord::Migration[4.2]
/home/joe/.rvm/gems/ruby-2.3.3/gems/activerecord-5.1.6/lib/active_record/migration.rb:525:in `inherited'
/home/joe/workspace/asb_base/db/migrate/20170829235908_add_attachment_import_csv_file_to_tab_client_project_communities.rb:1:in `<top (required)>'

首先我尝试了错误消息中的建议,并在迁移脚本中明确声明了 Rails 版本。我使用了ActiveRecord::Migration[4.2],因为那是错误消息中的内容,然后我尝试了ActiveRecord::Migration[5.1],因为那是最初编写迁移时使用的Rails版本;没有人在 Ubuntu 中工作过。

我想这可能是因为我在 Ubuntu 上的 RoR 版本(Ruby 2.5.1/Rails 5.2.0)与 Windows 上的版本(v2.3.3/v5.1.4)不同。我明确锁定了我的 RoR 版本以使用我在 Windows 中的版本,然后运行迁移 - 仍然是相同的错误消息。

我不明白为什么 rake 认为有问题的迁移脚本是使用 Rails v4.x 编写的。甚至我写的最早的源代码(甚至在此之前)也是使用 Rails v5.x。谷歌搜索没有给我任何可靠、可用的答案 - 以前有没有人解决过这个问题?

【问题讨论】:

    标签: ruby-on-rails windows ruby-on-rails-5 rake ubuntu-18.04


    【解决方案1】:

    尝试运行rake db:migrate:reset(仅在开发中执行此操作!)

    “我不明白为什么 rake 认为迁移脚本在 问题是使用 Rails v4.x 编写的?”

    它没有。版本 (4.2) 被硬编码为错误消息中的示例。

    来自活动记录源......

    def self.inherited(subclass) # :nodoc:
      super
      if subclass.superclass == Migration
        raise StandardError, "Directly inheriting from ActiveRecord::Migration is not supported. " \
          "Please specify the Rails release the migration was written for:\n" \
          "\n" \
          "  class #{subclass} < ActiveRecord::Migration[4.2]"
      end
    end
    

    【讨论】:

    • 我跑了rails db:migrate:reset,得到了Environment data not found in the schema. To resolve this issue, run: bin/rails db:environment:set RAILS_ENV=development。然后我运行了那个新命令并得到了rails aborted! Don't know how to build task 'db:migrate:set' (see --tasks)
    • 只是为了澄清...当您运行 bin/rails db:environment:set RAILS_ENV=development 时会得到 Don't know how to build task 'db:migrate:set' (see --tasks)?如果没有,你正在运行rails db:migrate:set....it should be rails db:migrate:reset
    【解决方案2】:

    您说您“尝试运行迁移”,但请注意您运行的命令:

    $ bundle exec rake db:migrate

    不仅会运行一次迁移,还会运行当前关闭的所有迁移(也就是尚未针对此环境运行)。很可能,因为您是第一次在 Ubuntu 上进行迁移,所以此命令正在尝试运行您的每一次迁移。如果您在 Rails 运行后将 Rails 升级到 5.1 或更高版本,那么过去在没有从显式版本继承的情况下进行的迁移将无法正常工作。

    确保您使用ActiveRecord::Migration[5.1](或任何正确的版本)更新每个迁移文件,如果它们都需要重新运行。如果您确实只想迁移一次迁移,命令是:bundle exec rake db:migrate:up VERSION=XXXXXXXX,其中XXXXXXXX 是相关迁移文件开头的时间戳。

    另外请注意,这就是为什么不建议从迁移文件中重建数据库的原因。 Here is a blog on that topic.

    【讨论】:

    • 我也有同样的想法,但如果迁移在 Rails 5.1.x 版本中运行,它们不应该在 Rails 5.2 中运行吗?我认为这就是对迁移进行版本控制的全部目的?
    猜你喜欢
    • 2020-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    • 1970-01-01
    • 1970-01-01
    • 2018-09-13
    相关资源
    最近更新 更多