migration - 如何从 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 ,它将起作用。
请帮我纠正任务。谢谢。
解决方案
在 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”
推荐阅读
- python - PyInstaller:无法为未注册的加载程序类型执行此操作
- laravel - Laravel 碳日期添加 7 分钟
- php - PHP Spreadsheet / Laravel Excel - CSV 中的日期格式在编辑后转换为用户本地格式
- mule - 访问变量中的属性 json (flowVars) Mulesoft
- wcf - 如何手动创建 wcf 服务故障异常?
- java - 无法解析查询 - 在 graphql.GraphQL.execute()
- javascript - iOS 移动浏览器上的切换选项卡无法通过 href 目标甚至 window.open 窗口焦点工作
- bash - 多个whois查询
- reactjs - 带有 React 的 Material-Table:如何在单元格中使用星级?
- python - 如何有效地使用 2d 坐标分配 3d numpy 数组