使用 rake 任务重置数据库

2024-04-07

我想使用 Heroku 的调度程序 https://devcenter.heroku.com/articles/scheduler每天重置我的数据库一次。

建议对调度程序使用 rake 任务。这是我尝试过的:

task :reset_database => :environment do
  `heroku pg:reset MY_DB:URL`
  `heroku run rake db:migrate db:seed`
  # some other ruby commands
end

但是我该如何正确地做到这一点,因为将heroku命令放在反引号内,这与bash正常工作 https://stackoverflow.com/questions/2232/calling-bash-commands-from-ruby,在这里不起作用:

No such file or directory - heroku


尝试这个 rake 任务:

namespace :reset_database do
  desc "Destroy all table entries."
  task :all => :environment do
    ActiveRecord::Base.connection.tables.each do |table|
      if table != 'schema_migrations'
        table.singularize.camelize.constantize.destroy_all
      end
      # Use this if you want to use the normal seeds:
      # Rails.application.load_seed

      # Use this if you want to run another rake task:
      Rake::Task["foo:bar"].invoke
    end
  end
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 rake 任务重置数据库 的相关文章

随机推荐