从迁移中执行 Rake 任务?

发布于 2024-08-29 10:20:17 字数 961 浏览 7 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暗地喜欢 2024-09-05 10:20:17

是的!有一种方法可以做到这一点。

运行以下命令。

Rake::Task['your_task'].invoke

更新的解决方案

不要将 Rake 放在括号内,只需将任务的名称即可。我建议您在控制台中运行以下命令时设置 ENV 变量。

FILE=somefile.text rake db:sistema:load_data

您可以使用以下示例单独调用它。

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

它将在您的任务中以 ENV['file'] 形式提供。

Yes! There is a way to do that.

Run the following command.

Rake::Task['your_task'].invoke

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.

FILE=somefile.text rake db:sistema:load_data

You can call it separately using the following example.

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

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

天生の放荡 2024-09-05 10:20:17

请注意,如果您使用“system”调用 Rake 任务,则需要随后检查进程状态,并在 Rake 任务失败时引发异常。否则,即使 Rake 任务失败,迁移也会成功。

您可以这样检查进程状态:

if !($?.success?)
  raise "Rake task failed"
end

调用 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:

if !($?.success?)
  raise "Rake task failed"
end

Invoking the rake task is a nicer option - it will cause the migration to fail if the Rake task fails.

猫九 2024-09-05 10:20:17

您可以使用 Rake::Task['namespace:task'].invokeRake::Task['namespace:task'] 在已加载的 Rails 环境中执行 rake 任务。执行

您可以将数据传递到 invokeexecute 方法内部的任务。示例:

Rake::Task['namespace:task'].invoke(paramValue)

此参数可以在 rake 任务中处理,如下所示:

namespace :namespace do
  desc "Example description."
  task :task, [:param] => :environment do |t, args|
    puts args[:param]
    ...
  end
end

这可以在控制台上执行为:

bundle exec rake namespace:task[paramValue]

更多信息: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 or Rake::Task['namespace:task'].execute.

You can pass data to the task inside of the invoke or execute method. Example:

Rake::Task['namespace:task'].invoke(paramValue)

This param can be handled in the rake task as follows:

namespace :namespace do
  desc "Example description."
  task :task, [:param] => :environment do |t, args|
    puts args[:param]
    ...
  end
end

This can be executed on the console as:

bundle exec rake namespace:task[paramValue]

More info: https://medium.com/@sampatbadhe/rake-task-invoke-or-execute-419cd689c3bd

童话里做英雄 2024-09-05 10:20:17

这个决定更合适,恕我直言。

在你的情况下,它会是这样的:

backup_env = ENV.slice('file') if ENV.key?('file')
ENV['file'] = 'faturamento/cfop'
Rake::Task['sistema:load_data'].invoke
ENV.delete 'file'
ENV.merge!(backup_env) if backup_env

This decision fits better, IMHO.

In your case it would be smth like this:

backup_env = ENV.slice('file') if ENV.key?('file')
ENV['file'] = 'faturamento/cfop'
Rake::Task['sistema:load_data'].invoke
ENV.delete 'file'
ENV.merge!(backup_env) if backup_env
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文