Class: Capistrano::DBSync::Postgres::Importer

Inherits:
Object
  • Object
show all
Defined in:
lib/capistrano/db_sync/postgres/importer.rb

Instance Method Summary collapse

Constructor Details

#initialize(working_dir, config) ⇒ Importer

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"
}


13
14
15
16
17
# 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

Instance Method Details

#restore(db = config["database"], jobs: 1) ⇒ Object

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


30
31
32
33
34
35
36
37
38
39
40
41
42
# 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