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



186
187
188
# File 'lib/duple/cli/helpers.rb', line 186

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



176
177
178
179
180
181
182
183
184
# File 'lib/duple/cli/helpers.rb', line 176

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_db_config(appname) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
# File 'lib/duple/cli/helpers.rb', line 138

def fetch_heroku_db_config(appname)
  # Run the heroku config command first, even if it's a dry run. So
  # that the command to get the config will show up in the dry run log.
  config_vars = heroku.capture(appname, "config")

  if config.dry_run?
    config.db_config(appname, dry_run: true)
  else
    parse_heroku_config(config_vars)
  end
end

#fetch_latest_snapshot_time(appname) ⇒ Object



159
160
161
162
163
164
# File 'lib/duple/cli/helpers.rb', line 159

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



190
191
192
193
194
195
196
# File 'lib/duple/cli/helpers.rb', line 190

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

#parse_heroku_config(config_vars) ⇒ Object

Raises:

  • (ArgumentError)


150
151
152
153
154
155
156
157
# File 'lib/duple/cli/helpers.rb', line 150

def parse_heroku_config(config_vars)
  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:\/\/(?<username>.*):(?<password>.*)@(?<host>.*):(?<port>\d*)\/(?<database>.*)/
  )
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



166
167
168
169
170
171
172
173
174
# File 'lib/duple/cli/helpers.rb', line 166

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_db_configObject



122
123
124
125
126
127
128
# File 'lib/duple/cli/helpers.rb', line 122

def source_db_config
  @source_db_config ||= if config.heroku_source?
                          fetch_heroku_db_config(source_appname)
                        else
                          config.db_config(config.source_name)
                        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_db_configObject



130
131
132
133
134
135
136
# File 'lib/duple/cli/helpers.rb', line 130

def target_db_config
  @target_db_config ||= if config.heroku_target?
                          fetch_heroku_db_config(target_appname)
                        else
                          config.db_config(config.target_name)
                        end
end