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



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/tapsoob/cli/data_stream.rb', line 20

def pull(database_url, dump_path = nil)
  opts = parse_opts(options)

  # Force serial mode when outputting to STDOUT (for piping)
  # Parallel mode would interleave output and corrupt the JSON stream
  if dump_path.nil? && opts[:parallel] && opts[:parallel] > 1
    STDERR.puts "Warning: Parallel mode disabled when outputting to STDOUT (for piping)"
    opts[:parallel] = 1
  end

  op = Tapsoob::Operation::Base.factory(:pull, database_url, dump_path, opts)
  op.pull_data
end

#push(database_url, dump_path = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/tapsoob/cli/data_stream.rb', line 44

def push(database_url, dump_path = nil)
  opts = parse_opts(options)

  # If dump_path is provided, use the Operation class for proper parallel support
  if dump_path && Dir.exist?(dump_path)
    op = Tapsoob::Operation::Base.factory(:push, database_url, dump_path, opts)
    op.push_data
  else
    # STDIN mode: read and import data directly (no parallel support for STDIN)
    if opts[:parallel] && opts[:parallel] > 1
      STDERR.puts "Warning: Parallel mode not supported when reading from STDIN"
    end

    data = []
    STDIN.each_line { |line| data << JSON.parse(line, symbolize_names: true) }

    # import data
    data.each do |table|
      table_name = table[:table_name]

      # Truncate table if purge option is enabled
      if opts[:purge]
        db(database_url, opts)[table_name.to_sym].truncate
      end

      stream = Tapsoob::DataStream::Base.factory(db(database_url, opts), {
        table_name: 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_name} : #{e.message}"
      end
    end
  end
end