Class: Redata::DataBase

Inherits:
Object
  • Object
show all
Defined in:
lib/redata/database.rb

Constant Summary collapse

REDSHIFT_PORT =
5439

Instance Method Summary collapse

Constructor Details

#initializeDataBase

Returns a new instance of DataBase.



5
6
7
8
9
10
11
12
13
14
# File 'lib/redata/database.rb', line 5

def initialize
    unless RED.root.join('config', 'red_access.yml').exist?
      puts "Redata loading error: config/red_access.yml not found" 
      return nil
    end
  @config = YAML.load(ERB.new(File.read(RED.root.join 'config', 'red_access.yml')).result(binding))[RED.env]
  Log.error! "ERROR: Database of #{RED.env} was not declared in config/red_access.yml" unless @config

  @ssh = Ssh.new
end

Instance Method Details

#connect_mysql(query_file, category, stage) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/redata/database.rb', line 50

def connect_mysql(query_file, category, stage)
  target_config = @config['deploy'][category.to_s]
  Log.error! "ERROR: Export config of #{config.category} was not found" unless target_config

  target_config = target_config[stage] if stage
  Log.error! "ERROR: Export config of #{config.category} for stage #{stage} was not found" unless target_config

  cmd = "mysql #{make_mysql_cmd_params(target_config)} < #{query_file}"
  @ssh.local_command cmd
end

#connect_redshift(config) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/redata/database.rb', line 16

def connect_redshift(config)
  cmd = make_redshift_cmd
  if @ssh.run_with_ssh?
    @ssh.upload_dir config.tmp_file_dir
    @ssh.run_command "export PGPASSWORD='#{ENV['PGPASSWORD']}';#{cmd} -f ~/tmp/#{config.tmp_file_dir.basename}/exec.sql"
    @ssh.remove_dir "~/tmp/#{config.tmp_file_dir.basename}"
  else
    @ssh.local_command "#{cmd} -f #{config.tmp_exec_file}"
  end
end

#inject_data(config, stage) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/redata/database.rb', line 27

def inject_data(config, stage)
  target_config = @config['deploy'][config.category.to_s]
  Log.error! "ERROR: Export config of #{config.category} was not found" unless target_config

  target_config = target_config[stage] if stage
  Log.error! "ERROR: Export config of #{config.category} for stage #{stage} was not found" unless target_config

  if target_config['local_dir']
    if Pathname.new(target_config['local_dir']).absolute?
      local_dir = Pathname.new(target_config['local_dir'])
    else
      local_dir = RED.root.join target_config['local_dir']
    end
    cmd = "mv #{config.tmp_data_file} #{local_dir}/#{config.source_name}.tsv"
  elsif target_config['database']
    import_params = "--local #{RED.is_append ? '' : '--delete'} --fields-terminated-by='\\t' --fields-enclosed-by='\\\"' --lines-terminated-by='\\n'"
    cmd = "mysqlimport #{make_mysql_cmd_params(target_config)} #{config.tmp_data_file} #{import_params}"
  else
    Log.error! "ERROR: Export config of #{config.category} was not found" unless target_config
  end
  @ssh.local_command cmd
end