【问题标题】:Reproduce Production environment in a Rake Task in Rails在 Rails 的 Rake 任务中重现生产环境
【发布时间】:2016-11-13 17:37:43
【问题描述】:

我正在尝试将我的所有生产数据库(我在 Mongo 中的)复制到我的暂存环境。所以我正在尝试构建一个任务。首先,我需要连接到生产环境才能访问生产中的所有模型(Model.all.each ...),但我不知道如何重现生产环境。我知道在控制台中我可以执行“导出 RAILS_ENV=heroku_production”,但我不知道如何在 Rake 任务中执行此操作。这是我现在正在尝试的,但它不起作用,因为我打印 Rails.env 并且它打印“开发”......所以我有点迷路

namespace :db do
  namespace :sync_production_staging do
    desc "Copy production database to staging"
    task :staging => :environment do

      system "export RAILS_ENV=heroku_production"

      ap Rails.env

      ap User.all
    end
  end
end

【问题讨论】:

  • 您正在尝试将您的数据库从 Heroku 克隆到本地?

标签: database ruby-on-rails-4 mongoid production-environment


【解决方案1】:

我有一个脚本可以将我的数据库从 heroku 复制到我的本地,这是一个非常困难的过程,很抱歉这是 PG 而不是 mongo,但我相信这应该会有所帮助

#lib/tasks/db.rake
namespace :db do
  desc "Import most recent database dump"
  task :import_from_prod => :environment do
    puts 'heroku run pg:backups capture --app sushi-prod'
    restore_backup 'sushi-prod'
  end

  def path_to_heroku
    ['/usr/local/heroku/bin/heroku', '/usr/local/bin/heroku'].detect {|path| File.exists?(path)}
  end

  def heroku(command, site)
    `GEM_HOME='' BUNDLE_GEMFILE='' GEM_PATH='' RUBYOPT='' #{path_to_heroku} #{command} -a #{site}`
  end

  def restore_backup(site = 'sushi-prod')
    dump_file = "#{Rails.root}/tmp/postgres.dump"
    unless File.exists?(dump_file)
      pgbackups_url = heroku('pg:backups public-url -q', site).chomp
      puts "curl -o #{dump_file} #{pgbackups_url}"
      system "curl -o #{dump_file} '#{pgbackups_url}'"
    end
    database_config = YAML.load(File.open("#{Rails.root}/config/database.yml")).with_indifferent_access
    dev_db = database_config[Rails.env]
    system "pg_restore -d #{dev_db[:database]} -c #{dump_file}".gsub(/\s+/,' ')
    puts
    puts "'rm #{dump_file}' to redownload postgres dump."
    puts "Done!"
  end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-10
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-20
    • 2015-10-29
    • 2019-10-28
    相关资源
    最近更新 更多