Class: Tapsoob::CLI::DataStream

Inherits:
Thor
  • Object
show all
Defined in:
lib/tapsoob/cli/data_stream.rb

Instance Method Summary collapse

Instance Method Details

#pull(database_url, dump_path = nil) ⇒ Object



19
20
21
22
# File 'lib/tapsoob/cli/data_stream.rb', line 19

def pull(database_url, dump_path = nil)
  op = Tapsoob::Operation.factory(:pull, database_url, dump_path, parse_opts(options))
  op.pull_data
end

#push(database_url, dump_path = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/tapsoob/cli/data_stream.rb', line 33

def push(database_url, dump_path = nil)
  # instantiate stuff
  data = []
  opts = parse_opts(options)

  # read data from dump_path or from STDIN
  if dump_path && Dir.exist?(dump_path)
    files = Dir[Pathname.new(dump_path).join("*.json")]
    files.each { |file| data << JSON.parse(File.read(file), symbolize_names: true) }
  else
    STDIN.each_line { |line| data << JSON.parse(line, symbolize_names: true) }
  end

  # import data
  data.each do |table|
    stream = Tapsoob::DataStream.factory(db(database_url, opts), {
      table_name: table[:table_name],
      chunksize: opts[:default_chunksize]
    }, { :"discard-identity" => opts[:"discard-identity"] || false, :purge => opts[:purge] || false, :debug => opts[:debug] })

    begin
      stream.import_rows(table)
    rescue Exception => e
      stream.log.debug e.message
      STDERR.puts "Error loading data in #{table[:table_name]} : #{e.message}"
    end
  end
end