从迁移内部执行Rake任务? [英] Execute a Rake task from within migration?

查看:44
本文介绍了从迁移内部执行Rake任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rake任务,可从文件将配置数据加载到DB中,是否有正确的ruby/rails方式在迁移时调用它?

I have a Rake task that loads configuration data into the DB from a file, is there a correct ruby/rails way to call it on a migration up?

我的目标是同步我的团队数据库配置,无需广播即可运行任务 lalala

My objective is to sync my team DB configs, without have to broadcast then to run the task lalala

  def self.up
    change_table :fis_situacao_fiscal do |t|
      t.remove :mostrar_endereco
      t.rename :serie, :modelo 
    end

    Faturamento::Cfop.destroy_all()
    #perform rake here !
  end

更新 我现在的工作方式和工作方式:

UPDATE How I do now, and works:

system('rake sistema:load_data file=faturamento/cfop')

这是 @Ryan Bigg 的建议,但有例外:

And this is the suggestion from @Ryan Bigg, and it's exception:

Rake::Task['rake sistema:load_data file=faturamento/cfop'].invoke()

.

==  AlterSituacaoFiscalModeloEndereco: migrating ====================
-- change_table(:fis_situacao_fiscal)
   -> 0.0014s

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

Don't know how to build task 'rake sistema:load_data file=faturamento/cfop'

哪里出错了?

推荐答案

是的,有一种方法可以做到:

Yes there's a way to do that:

Rake::Task['your_task'].invoke

更新

不要将rake放在括号内,而应该将任务的 name 放在括号内.运行此代码时,您应该设置一个ENV变量:

Do not put rake inside the brackets, just the name of the task. You should set an ENV variable when running this:

在控制台中

FILE=somefile.text rake db:sistema:load_data

单独调用

FILE=somefile.text rake some:other:task:that:calls:it

这将在您的任务中显示为ENV['file']

This will be available in your tasks as ENV['file']

这篇关于从迁移内部执行Rake任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆