Class: Capistrano::DBSync::Postgres::Exporter

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

Instance Method Summary collapse

Constructor Details

#initialize(working_dir, config) ⇒ Exporter

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/exporter.rb', line 13

def initialize(working_dir, config)
  @working_dir = working_dir
  @config      = config
  @cli         = Postgres::CLI.new(config)
end

Instance Method Details

#dump(db = config["database"], data_selection: {}) ⇒ Object

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

db (optional): Database name to dump data_selection (optional): A hash mapping a table name to a query or nil in case no data is wanted for a table.

Example:

dump(“/tmp/dump”,

data_selection:
  posts:    "SELECT * FROM posts    WHERE created_at > NOW() - interval '60 days'",
  comments: "SELECT * FROM comments WHERE created_at > NOW() - interval '30 days'",
  likes: nil
},
db: "faceburger_production")

This outputs commands that will generate dump files as:

/tmp/dump/0001-faceburger_production.schema -- will contain db schema and data except
                                               for tables posts, comments and likes
/tmp/dump/0002-posts.table    -- will contain partial data of table posts
/tmp/dump/0003.comments.table -- will contain partial data of table comments


41
42
43
44
45
46
47
48
49
# File 'lib/capistrano/db_sync/postgres/exporter.rb', line 41

def dump(db = config["database"], data_selection: {})
  file_namer = Postgres::FileNameGenerator.new(working_dir)
  exclude_tables_args = data_selection.keys.map { |table| %Q|--exclude-table-data="#{table}"| }

  [
    cli.dump(file_namer.next(db, :schema), db, [exclude_tables_args]),
    *dump_partial_selected_data(db, file_namer, data_selection)
  ]
end