无法运行迁移脚本

时间:2018-07-08 02:37:48

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

我已经在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)>'

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

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

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

2 个答案:

答案 0 :(得分:0)

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

  

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

不是。错误消息中以版本(4.2)为例进行了硬编码。

从activerecord来源。...

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

答案 1 :(得分:0)

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

$ bundle exec rake db:migrate

不仅将运行一个迁移,还将运行当前已关闭的所有迁移(对于该环境,尚未运行)。可能是因为您是第一次在Ubuntu上进行迁移,所以此命令正在尝试运行每个迁移。如果您已经将Rails从运行开始升级到5.1或更高版本,那么过去无法从显式版本继承而进行的迁移这次将无法正常工作。

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

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