ruby-on-rails - 覆盖 postgresql 数据库

标签 ruby-on-rails database postgresql

有时我们会从生产数据库复制到质量数据库。发生这种情况时,我只需创建一个新数据库并将其链接到 Rails 应用程序中。

问题:您能否清空包括关系在内的整个 postgresql 数据库并导入新数据库?

问题:导入数据库时​​它不会覆盖当前数据/结构/关系...

信息:

我这样导出生产数据库:

## Dump without user privileges
pg_dump -x -U database1 -h localhost -O database1 > database1.sql

通常我会像这样导入导出的数据库:

## Import
psql -U database2 database2 < database1.sql  

最佳答案

多年来,我一直在使用以下 rake 任务,它对我来说效果很好。 local:db:abort_if_active_connections 依赖项不是绝对必要的,但它很好,否则备份会失败,因为您无法删除当前正在使用的数据库。

您需要将 local:backup:production 任务的系统命令调整为获取数据库副本所需的任何命令。然后你可以运行:

bin/rake local:backup:production

lib/tasks/local/backup.rake

namespace :local do

  namespace :backup do

    desc 'Backup production and restore to development'
    task :production => ['production:db']

    namespace :production do
      desc 'Backup production database and restore to development'
      task :db => ['local:db:abort_if_active_connections', 'db:drop', 'db:create'] do
        config = ActiveRecord::Base.configurations[Rails.env]
        gz_file = "#{ActiveRecord::Base.configurations['production']['database']}.gz"
        puts "Copying production database to temporary file on this machine..."
        system "scp user@example.com:/tmp/#{gz_file} /tmp/#{gz_file}"
        puts "Recreating local database..."
        system "gunzip -c /tmp/#{gz_file} | psql #{config['database']}"
        puts "Removing temporary file..."
        File.unlink "/tmp/#{gz_file}"
      end
    end

  end

end

lib/tasks/local.rake

namespace :local do

  namespace :db do
    task :abort_if_active_connections => ['db:load_config'] do

      config = ActiveRecord::Base.configurations[Rails.env]
      ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))

      version = ActiveRecord::Base.connection.send(:postgresql_version)
      if version >= 90200
        pid = 'pid'
      else
        pid = 'procpid'
      end

      connections = ActiveRecord::Base.connection.select_all("SELECT * FROM pg_stat_activity WHERE pg_stat_activity.datname = '#{config['database']}' AND #{pid} <> #{$$}")

      unless connections.empty?
        puts
        puts "There are active connections to the database '#{config['database']}':"
        puts
        puts "%-7s %-20s %-16s %-20s %s" % %w[pid usename client_addr application_name backend_start]
        connections.each do |c|
          puts "%-7s %-20s %-16s %-20s %s" % [c[pid], c['usename'], c['client_addr'], c['application_name'], c['backend_start']]
        end
        puts
        exit 1
      end

      ActiveRecord::Base.clear_all_connections!
    end
  end

end

关于ruby-on-rails - 覆盖 postgresql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29278506/

相关文章:

ruby-on-rails - 无法在 Linux 上安装 rabbitmq-server(Oracle Linux Server 版本 6.7/RHEL)

ruby-on-rails - Rails4 order by column alias in joins group

ruby-on-rails - 从 GitHub 运行 Ruby on Rails 应用程序

python - 如何在 Python 中获取 Riak 对象、更改其值并将其与所有索引一起存储回来

mysql - 德鲁帕尔 6 : using too many Views module causing site to go down cos of too many mysql connection

java - 如何使用 Hibernate 将 Java 对象映射到 H2 中的 clob 和 Postgres 中的 json

postgresql - 自动化 postgresql 数据迁移,最好是 fabric 任务

ruby-on-rails - 在 1 个操作中选择和拒绝 2 个数组的 ruby​​ 数组(可枚举)方法

mysql - 将行转换为列的查询

php - 跨 2 列的 PostgreSQL 10 约束?