用rake任务重置数据库 [英] Reset database with rake task

查看:174
本文介绍了用rake任务重置数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想每天使用Heroku的计划程序重置我的数据库。

I want to use Heroku's scheduler to reset my database once every day.

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

It's recommended to use rake tasks for the scheduler. This is what I've tried:

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 正常工作,在这里不起作用:

But how would I do this correctly, because putting the heroku commands within backticks, which with bash normally works, doesn't work here:

没有这样的文件或目录 - heroku

推荐答案

/ p>

Try this rake task:

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

这篇关于用rake任务重置数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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