Class: Duple::CLI::Refresh

Inherits:
Thor::Group
  • Object
show all
Includes:
Helpers
Defined in:
lib/duple/cli/refresh.rb

Overview

Usage:

duple refresh

Options:

-c, [--config=CONFIG]         # The location of the config file.
-s, [--source=SOURCE]         # The name of the source environment.
-t, [--target=TARGET]         # The name of the target environment.
-g, [--group=GROUP]           # The group configuration to use when dumping source data.
    [--capture]               # Capture a new source snapshot before refreshing.
-t, [--tables=one two three]  # A list of tables to include when dumping source data.

Resets and copies schema and data from a source to a target database

Instance Method Summary collapse

Methods included from Helpers

included

Instance Method Details

#capture_snapshotObject



44
45
46
47
48
# File 'lib/duple/cli/refresh.rb', line 44

def capture_snapshot
  return unless capture_snapshot?

  heroku.run(source_appname, 'pgbackups:capture')
end

#download_snapshotObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/duple/cli/refresh.rb', line 56

def download_snapshot
  return unless download_snapshot?

  timestamp = fetch_latest_snapshot_time(source_appname)

  @snapshot_path = snapshot_file_path(timestamp.strftime('%Y-%m-%d-%H-%M-%S'))
  unless File.exists?(@snapshot_path)
    runner.run("curl -o #{@snapshot_path} #{@source_snapshot_url}")
  end
end

#dump_dataObject



67
68
69
70
71
# File 'lib/duple/cli/refresh.rb', line 67

def dump_data
  return unless dump_data?

  postgres.pg_dump(dump_flags, data_file_path, source_db_config)
end

#fetch_snapshot_urlObject



50
51
52
53
54
# File 'lib/duple/cli/refresh.rb', line 50

def fetch_snapshot_url
  return unless fetch_snapshot_url?

  @source_snapshot_url = heroku.capture(source_appname, 'pgbackups:url').strip
end

#reset_targetObject



73
74
75
# File 'lib/duple/cli/refresh.rb', line 73

def reset_target
  reset_database(config.target_environment)
end

#restore_databaseObject



77
78
79
80
81
82
83
84
85
# File 'lib/duple/cli/refresh.rb', line 77

def restore_database
  if download_snapshot?
    postgres.pg_restore('-e -v --no-acl -O -a', @snapshot_path, target_db_config)
  elsif dump_data?
    postgres.pg_restore('-e -v --no-acl -O -a', data_file_path, target_db_config)
  else
    heroku.run(target_appname, "pgbackups:restore DATABASE #{@source_snapshot_url}")
  end
end