首页 > 解决方案 > 如何从 rails6 中的 rake 文件迁移特定迁移

问题描述

在我的 rails 4.2 应用程序中,我使用了一个任务来执行迁移,如下所示:

ActiveRecord::Migrator.run(:up, ActiveRecord::Migrator.migrations_path, 20130306070257)

但是,在我将应用程序升级到 rails 6 后,这不起作用。显示的错误是:

NoMethodError: undefined method `migrations_path' for ActiveRecord::Migrator:Class
Did you mean?  migrations_paths
           migrations_paths=

然后,我尝试了以下功能:

ActiveRecord::Migrator.run(:up,20130306070257).

然后,我收到以下错误:

ActiveRecord::Migrator.run(:up, NoMethodError: ActiveRecord::Migrator:Class 的未定义方法“运行”

从应用程序路径,如果我给出: rake db:migrate:up VERSION=20130306070257 ,它将起作用。

请帮我纠正任务。谢谢。

标签: migrationruby-on-rails-6ruby-on-rails-4.2

解决方案


在 rails 6 run 是实例方法。(https://www.rubydoc.info/gems/activerecord/ActiveRecord/Migrator)所以你可以像这样使用它:

ActiveRecord::Migrator.new(:up, [ActiveRecord::MigrationProxy.new('CreateTenants', nil, 'db/migrate/20130306070257_create_tenants.rb', '')], ActiveRecord::SchemaMigration, nil).run

将“ClassName”替换为您的班级名称。例如,如果您的迁移以类 CreateUser < ActiveRecord::Migration[6.0 开始,则添加“CreateUser”而不是“ClassName”


推荐阅读