class Seedster::DataLoader

Attributes

file_manager[R]
load_database[R]
load_host[R]
load_password[R]
load_username[R]
remote_host_path[R]
ssh_host[R]
ssh_user[R]

Public Class Methods

new(ssh_user:, ssh_host:, remote_host_path:, load_host:, load_database:, load_username:, load_password:) click to toggle source
# File lib/seedster/data_loader.rb, line 22
def initialize(ssh_user:, ssh_host:, remote_host_path:,
               load_host:, load_database:, load_username:, load_password:)
  @ssh_user = ssh_user
  @ssh_host = ssh_host
  @remote_host_path = remote_host_path
  @load_host = load_host
  @load_database = load_database
  @load_username = load_username
  @load_password = load_password
  @file_manager = FileManager.new(app_root: Rails.root)
  print_greeting
end

Public Instance Methods

load!() click to toggle source
# File lib/seedster/data_loader.rb, line 35
def load!
  download_and_extract_file unless Seedster.configuration.skip_download

  Seedster.configuration.tables.each do |item|
    load_data(table_name: item[:name])
  end
end

Private Instance Methods

download_and_extract_file() click to toggle source
# File lib/seedster/data_loader.rb, line 45
def download_and_extract_file
  remote_host_path = Seedster.configuration.remote_host_path
  remote_file = "#{remote_host_path}/#{file_manager.consolidated_dump_file_name}"
  scp_command = "scp -r #{ssh_user}@#{ssh_host}:#{remote_file} ."
  puts "Downloading file: '#{scp_command}'"
  system(scp_command)

  untar_command = "tar -zxvf #{FileManager.dump_file_name} -C #{file_manager.seed_file_dir}"
  puts "Extracting file: '#{untar_command}'"
  system(untar_command)
end
load_data(table_name:) click to toggle source
# File lib/seedster/data_loader.rb, line 57
def load_data(table_name:)
  filename = file_manager.get_filename(table_name: table_name)
  psql_load(filename: filename, table_name: table_name)
end
print_greeting() click to toggle source
psql_load(filename:, table_name:) click to toggle source

TODO: this could be swapped out for MySQL equivalent by a motivated individual :)

# File lib/seedster/data_loader.rb, line 63
def psql_load(filename:, table_name:)
  load_command = "COPY #{table_name} FROM '#{filename}' DELIMITERS ',' CSV"
  puts "Loading '#{table_name}' from '#{filename}'"
  psql_cmd = %{PG_PASSWORD=#{load_password} psql --host #{load_host} --dbname #{load_database} --username #{load_username} -c "#{load_command}"}
  system(psql_cmd)
end