从迁移中执行 Rake 任务?
我有一个 Rake 任务,它将配置数据从文件加载到数据库中,是否有正确的 ruby/rails 方法在迁移时调用它?
我的目标是同步我的团队数据库配置,无需广播即可运行任务 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
更新 我现在的做法和工作方式:
system('rake sistema:load_data file=faturamento/cfop')
这是来自@Ryan Bigg的建议,它是例外:
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'
哪里出了问题?
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?
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')
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'
Where it went wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的!有一种方法可以做到这一点。
运行以下命令。
更新的解决方案
不要将 Rake 放在括号内,只需将任务的名称即可。我建议您在控制台中运行以下命令时设置
ENV
变量。您可以使用以下示例单独调用它。
它将在您的任务中以
ENV['file']
形式提供。Yes! There is a way to do that.
Run the following command.
Updated solution
Do not put Rake inside parenthesis, just the name of the task. I recommend that you set an
ENV
variable when running the following in the console.You can call it separately using the following example.
It will be available in your tasks as
ENV['file']
.请注意,如果您使用“system”调用 Rake 任务,则需要随后检查进程状态,并在 Rake 任务失败时引发异常。否则,即使 Rake 任务失败,迁移也会成功。
您可以这样检查进程状态:
调用 rake 任务是一个更好的选择 - 如果 Rake 任务失败,它将导致迁移失败。
Note that if you call the Rake task with 'system', you need to check the process status afterwards and raise an exception if the Rake task failed. Otherwise the migration will succeed even if the Rake task fails.
You can check the process status like this:
Invoking the rake task is a nicer option - it will cause the migration to fail if the Rake task fails.
您可以使用
Rake::Task['namespace:task'].invoke
或Rake::Task['namespace:task'] 在已加载的 Rails 环境中执行 rake 任务。执行
。您可以将数据传递到
invoke
或execute
方法内部的任务。示例:此参数可以在 rake 任务中处理,如下所示:
这可以在控制台上执行为:
更多信息:https://medium.com/@sampatbadhe/rake-task-invoke-or-execute-419cd689c3bd
You can execute a rake task from within a loaded Rails environment with either
Rake::Task['namespace:task'].invoke
orRake::Task['namespace:task'].execute
.You can pass data to the task inside of the
invoke
orexecute
method. Example:This param can be handled in the rake task as follows:
This can be executed on the console as:
More info: https://medium.com/@sampatbadhe/rake-task-invoke-or-execute-419cd689c3bd
这个决定更合适,恕我直言。
在你的情况下,它会是这样的:
This decision fits better, IMHO.
In your case it would be smth like this: