Class: Geordi::DumpLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/geordi/dump_loader.rb

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ DumpLoader

Returns a new instance of DumpLoader.



9
10
11
# File 'lib/geordi/dump_loader.rb', line 9

def initialize(file)
  @dump_file = file
end

Instance Method Details

#development_database_configObject Also known as: config



13
14
15
16
17
18
# File 'lib/geordi/dump_loader.rb', line 13

def development_database_config
  require 'yaml'

  @config ||= YAML.safe_load(ERB.new(File.read('config/database.yml')).result)
  @config['development']
end

#dump_fileObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/geordi/dump_loader.rb', line 43

def dump_file
  @dump_file ||= begin
    dumps_glob = File.join(File.expand_path('~'), 'dumps', '*.dump')
    available_dumps = Dir.glob(dumps_glob).sort

    HighLine.new.choose(*available_dumps) do |menu|
      menu.hidden('') { Interaction.fail 'Abort.' }
    end
  end
end

#loadObject



54
55
56
57
58
59
# File 'lib/geordi/dump_loader.rb', line 54

def load
  Interaction.note 'Source file: ' + dump_file

  source_command = send("#{config['adapter']}_command")
  Util.system! source_command, fail_message: "An error occured loading #{File.basename(dump_file)}"
end

#mysql_commandObject Also known as: mysql2_command



21
22
23
24
25
26
27
28
29
30
# File 'lib/geordi/dump_loader.rb', line 21

def mysql_command
  command = 'mysql --silent'
  command << ' -p' << config['password'].to_s if config['password']
  command << ' -u' << config['username'].to_s if config['username']
  command << ' --port=' << config['port'].to_s if config['port']
  command << ' --host=' << config['host'].to_s if config['host']
  command << ' --default-character-set=utf8'
  command << ' ' << config['database'].to_s
  command << ' < ' << dump_file
end

#postgresql_commandObject



33
34
35
36
37
38
39
40
41
# File 'lib/geordi/dump_loader.rb', line 33

def postgresql_command
  ENV['PGPASSWORD'] = config['password']
  command = 'pg_restore --no-owner --clean'
  command << ' --username=' << config['username'].to_s if config['username']
  command << ' --port=' << config['port'].to_s if config['port']
  command << ' --host=' << config['host'].to_s if config['host']
  command << ' --dbname=' << config['database'].to_s
  command << ' ' << dump_file
end