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



84
85
86
87
88
89
90
# File 'lib/duple/cli/helpers.rb', line 84

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



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

def config
  @config ||= parse_config
end

#data_file_pathObject



128
129
130
131
# File 'lib/duple/cli/helpers.rb', line 128

def data_file_path
  # TODO: This should go on the configuration and be configurable.
  @data_file_path ||= File.join(dump_dir_path, "#{config.source_name}-data.dump")
end

#default_config_pathObject



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

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

#dump_dir_pathObject



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

def dump_dir_path
  # TODO: This should go on the configuration and be configurable.
  File.join('tmp', 'duple')
end

#dump_flagsObject



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

def dump_flags
  # TODO: This should be moved to the endpoint classes.
  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

#herokuObject



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

def heroku
  # TODO: This should go away as the methods are moved into the endpoint classes.
  @heroku ||= Duple::HerokuRunner.new(runner)
end

#parse_configObject



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

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



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

def postgres
  # TODO: This should go away as the methods are moved into the endpoint classes.
  @pg_runner ||= Duple::PGRunner.new(runner)
end

#reset_database(env) ⇒ Object



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

def reset_database(env)
  # TODO: This should be moved to the endpoint classes.
  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



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

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

#snapshot_file_path(timestamp) ⇒ Object



139
140
141
142
143
# File 'lib/duple/cli/helpers.rb', line 139

def snapshot_file_path(timestamp)
  # TODO: This should go on the configuration and be configurable.
  filename = "#{config.source_name}-#{timestamp}.dump"
  @structure_file_path ||= File.join(dump_dir_path, filename)
end

#sourceObject



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

def source
  @source ||= Duple::Source.new(config, runner)
end

#source_db_configObject



145
146
147
148
# File 'lib/duple/cli/helpers.rb', line 145

def source_db_config
  # TODO: This should go away as the methods are moved into the endpoint classes.
  @source_db_config ||= source.db_config
end

#structure_file_pathObject



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

def structure_file_path
  # TODO: This should go on the configuration and be configurable.
  filename = "#{config.source_name}-structure.dump"
  @structure_file_path ||= File.join(dump_dir_path, filename)
end

#targetObject



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

def target
  @target ||= Duple::Target.new(config, runner)
end

#target_appnameObject



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

def target_appname
  # TODO: This should go away as the methods are moved into the endpoint classes.
  target.appname
end

#target_db_configObject



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

def target_db_config
  # TODO: This should go away as the methods are moved into the endpoint classes.
  @target_db_config ||= target.db_config
end