class DumpTruck::Truck

Attributes

client[R]
schema_config[R]
translator[R]

Public Class Methods

new(schema_config, client, translator) click to toggle source
# File lib/dump_truck/truck.rb, line 5
def initialize(schema_config, client, translator)
  @schema_config = schema_config
  @client = client
  @translator = translator
end

Public Instance Methods

dump() click to toggle source
# File lib/dump_truck/truck.rb, line 11
def dump
  tables = extract_tables
  dump_schema(tables)
end

Protected Instance Methods

dump_data(config, table, target) click to toggle source
# File lib/dump_truck/truck.rb, line 38
def dump_data(config, table, target)
  client.data_dump(config, table) do |io|
    row_num = 0
    io.each do |line|
      line, row_num = translator.insert?(line) ? obfuscate(config, table, line, row_num) : [line, row_num]

      target.write(line)
    end
  end
end
dump_schema(tables) click to toggle source
# File lib/dump_truck/truck.rb, line 28
def dump_schema(tables)
  Target.new(schema_config.target).open do |target|
    tables.each do |table|
      config = schema_config.table_config_for(table)

      dump_data(config, table, target) unless config.mode == :ignore
    end
  end
end
extract_insert(line) click to toggle source
# File lib/dump_truck/truck.rb, line 66
def extract_insert(line)
  translator.extract_insert(line)
end
extract_table(line) click to toggle source
# File lib/dump_truck/truck.rb, line 62
def extract_table(line)
  translator.extract_table(line)
end
extract_tables() click to toggle source
# File lib/dump_truck/truck.rb, line 20
def extract_tables
  client.tables_dump do |io|
    io.each.map do |line|
      extract_table(line) if translator.table?(line)
    end.compact
  end
end
obfuscate(config, table, line, row_num) click to toggle source
# File lib/dump_truck/truck.rb, line 49
def obfuscate(config, table, line, row_num)
  return '' if config.mode == :none

  fields, data = extract_insert(line)

  data = data.map do |datum|
    row_num += 1
    Hash[datum.map{|field, value| [field, config.obfuscate_value(field, value, row_num)]}]
  end

  [translator.generate_insert(table, fields, data), row_num]
end