Module: Duple::CLI::Helpers::InstanceMethods

Defined in:
lib/duple/cli/helpers.rb

Instance Method Summary collapse

Instance Method Details

#app_config_path(verify_file = true) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/duple/cli/helpers.rb', line 76

def app_config_path(verify_file = true)
  config_path = options[:config] || default_config_path
  if verify_file && !File.exists?(config_path)
    raise ArgumentError.new("Missing config file: #{config_path}")
  end
  config_path
end

#configObject



178
179
180
# File 'lib/duple/cli/helpers.rb', line 178

def config
  @config ||= parse_config
end

#data_file_pathObject



108
109
110
# File 'lib/duple/cli/helpers.rb', line 108

def data_file_path
  @data_file_path ||= File.join(dump_dir_path, "#{config.source_name}-data.dump")
end

#default_config_pathObject



72
73
74
# File 'lib/duple/cli/helpers.rb', line 72

def default_config_path
  File.join('config', 'duple.yml')
end

#dump_dir_pathObject



104
105
106
# File 'lib/duple/cli/helpers.rb', line 104

def dump_dir_path
  File.join('tmp', 'duple')
end

#dump_flagsObject



152
153
154
155
156
157
158
159
160
# File 'lib/duple/cli/helpers.rb', line 152

def dump_flags
  include_tables = config.included_tables
  include_flags = include_tables.map { |t| "-t #{t}" }

  exclude_tables = config.excluded_tables
  exclude_flags = exclude_tables.map { |t| "-T #{t}" }

  flags = [ '-Fc -a', include_flags, exclude_flags ].flatten.compact.join(' ')
end

#fetch_heroku_credentials(appname) ⇒ Object

Raises:

  • (ArgumentError)


122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/duple/cli/helpers.rb', line 122

def fetch_heroku_credentials(appname)
  config_vars = heroku.capture(appname, "config")

  return config.dry_run_credentials(appname) if config.dry_run?

  db_url = config_vars.split("\n").detect { |l| l =~ /DATABASE_URL/ }
  raise ArgumentError.new("Missing DATABASE_URL variable for #{appname}") if db_url.nil?

  db_url.match(
    /postgres:\/\/(?<user>.*):(?<password>.*)@(?<host>.*):(?<port>\d*)\/(?<db>.*)/
  )
end

#fetch_latest_snapshot_time(appname) ⇒ Object



135
136
137
138
139
140
# File 'lib/duple/cli/helpers.rb', line 135

def fetch_latest_snapshot_time(appname)
  response = heroku.capture(appname, 'pgbackups')
  last_line = response.split("\n").last
  timestring = last_line.match(/\w+\s+(?<timestamp>[\d\s\/\:\.]+)\s+.*/)[:timestamp]
  DateTime.strptime(timestring, '%Y/%m/%d %H:%M.%S')
end

#herokuObject



92
93
94
# File 'lib/duple/cli/helpers.rb', line 92

def heroku
  @heroku ||= Duple::HerokuRunner.new(runner)
end

#parse_configObject



182
183
184
185
186
187
188
# File 'lib/duple/cli/helpers.rb', line 182

def parse_config
  config_path = File.join(destination_root, app_config_path)
  config_data = File.read(config_path)
  erbed = ERB.new(config_data).result
  config_hash = YAML.load(erbed) || {}
  Duple::Configuration.new(config_hash, options)
end

#postgresObject



88
89
90
# File 'lib/duple/cli/helpers.rb', line 88

def postgres
  @pg_runner ||= Duple::PGRunner.new(runner)
end

#reset_database(env) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/duple/cli/helpers.rb', line 142

def reset_database(env)
  if config.heroku?(env)
    appname = config.heroku_name(env)
    heroku.run(appname, 'pg:reset')
  else
    # if yes?("Are you sure you want to reset the #{config.target_name} database?", :red)
    runner.run('bundle exec rake db:drop db:create')
  end
end

#runnerObject



84
85
86
# File 'lib/duple/cli/helpers.rb', line 84

def runner
  @runner ||= Duple::Runner.new(dry_run: config.dry_run?)
end

#snapshot_file_path(timestamp) ⇒ Object



117
118
119
120
# File 'lib/duple/cli/helpers.rb', line 117

def snapshot_file_path(timestamp)
  filename = "#{config.source_name}-#{timestamp}.dump"
  @structure_file_path ||= File.join(dump_dir_path, filename)
end

#source_appnameObject



96
97
98
# File 'lib/duple/cli/helpers.rb', line 96

def source_appname
  @source_appname ||= config.heroku_name(config.source_environment)
end

#source_credentialsObject



162
163
164
165
166
167
168
# File 'lib/duple/cli/helpers.rb', line 162

def source_credentials
  @source_credentials ||= if config.heroku_source?
                            fetch_heroku_credentials(source_appname)
                          else
                            config.local_credentials
                          end
end

#structure_file_pathObject



112
113
114
115
# File 'lib/duple/cli/helpers.rb', line 112

def structure_file_path
  filename = "#{config.source_name}-structure.dump"
  @structure_file_path ||= File.join(dump_dir_path, filename)
end

#target_appnameObject



100
101
102
# File 'lib/duple/cli/helpers.rb', line 100

def target_appname
  @target_appname ||= config.heroku_name(config.target_environment)
end

#target_credentialsObject



170
171
172
173
174
175
176
# File 'lib/duple/cli/helpers.rb', line 170

def target_credentials
  @target_credentials ||= if config.heroku_target?
                            fetch_heroku_credentials(target_appname)
                          else
                            config.local_credentials
                          end
end