从迁移中执行 Rake 任务? [英] Execute a Rake task from within migration?
问题描述
我有一个将配置数据从文件加载到数据库中的 Rake 任务,是否有正确的 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 放在方括号内,只需将任务的 名称.你应该在运行这个时设置一个 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']
这篇关于从迁移中执行 Rake 任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!