class Capistrano::DBSync::Postgres::Importer

Attributes

cli[R]
config[R]
working_dir[R]

Public Class Methods

new(working_dir, config) click to toggle source

working_dir: The location where the dump files will be stored for dump or read for restore. config: database configuration hash with following skeleton:

{
  "database" => "faceburger_production",
  "username" => "fb_prod",
  "password" => "BestBurger",
  "host"     => "10.20.30.40",
  "port"     => "5432"
}
# File lib/capistrano/db_sync/postgres/importer.rb, line 13
def initialize(working_dir, config)
  @working_dir = working_dir
  @config      = config
  @cli         = Postgres::CLI.new(config)
end

Public Instance Methods

restore(db = config["database"], jobs: 1) click to toggle source

Returns a set of commands to dump a database with table data selection support.

db (optional): Database name to restore data into. jobs (optional): Number of concurrent jobs that Postgres will run to restore.

The working_dir should contain files with following name and extension patterns:

/tmp/dump/0001-faceburger_production.schema -- contains db schema and data except
                                               for tables posts and comments
/tmp/dump/0002-posts.table    -- contains partial data of table posts
/tmp/dump/0003.comments.table -- contains partial data of table comments
# File lib/capistrano/db_sync/postgres/importer.rb, line 30
def restore(db = config["database"], jobs: 1)
  temp_db   = "#{db}_#{Time.now.strftime("%Y%m%d%H%M%S")}"

  [
    cli.kill_processes_for_db(temp_db),
                  cli.drop_db(temp_db),
                cli.create_db(temp_db),
            *restore_files_to(temp_db, jobs),
    cli.kill_processes_for_db(db),
                  cli.drop_db(db),
                cli.rename_db(temp_db, db)
  ]
end

Private Instance Methods

restore_files_to(to_db, jobs) click to toggle source

Commands to restore pg_dump output file and partial tables data files generated by Postgres COPY command.

# File lib/capistrano/db_sync/postgres/importer.rb, line 48
def restore_files_to(to_db, jobs)
  [
    restore_pg_dump_file(to_db, %w(pre-data data), jobs),
    *restore_table_copy_files(to_db),
    restore_pg_dump_file(to_db, %w(post-data), jobs)
  ]
end
restore_pg_dump_file(to_db, sections = %w(pre-data data post-data), jobs) click to toggle source

The sections argument must match one of the following supported sections on pg_dump command:

  • Pre-data: include all other data definition items.

  • Data: contains actual table data, large-object contents, and sequence values.

  • Post-data: include definitions of indexes, triggers, rules, and constraints other than validated check constraints.

# File lib/capistrano/db_sync/postgres/importer.rb, line 61
def restore_pg_dump_file(to_db, sections = %w(pre-data data post-data), jobs)
  files = Dir.glob(File.join(working_dir, "*.schema"))
  raise "Expected to have only one pg_dump file, but found these: #{files.inspect}" if files.size > 1

  sections_args = sections.map { |section| "--section=#{section}" }
  cli.restore(files.first, to_db, [*sections_args, "--jobs=#{jobs}"])
end
restore_table_copy_files(to_db) click to toggle source
# File lib/capistrano/db_sync/postgres/importer.rb, line 69
def restore_table_copy_files(to_db)
  files = Dir.glob(File.join(working_dir, "*.table"))
  files.map do |file|
    table_name = Postgres::FileNameGenerator.extract_name(file)
    cli.copy_from_compressed_file(file, to_db, table_name)
  end
end