Class: DBA::Dump

Inherits:
Command show all
Defined in:
lib/dba/dump.rb

Constant Summary collapse

ADAPTERS =
{
  'csv' => :CSV,
  'jsonl' => :LDJSON,
  'ldjson' => :LDJSON,
  'ndjson' => :LDJSON,
  'yml' => :YAML,
  'yaml' => :YAML
}

Instance Attribute Summary

Attributes inherited from Command

#database, #table_name

Instance Method Summary collapse

Methods inherited from Command

arity_check, #initialize

Constructor Details

This class inherits a constructor from DBA::Command

Instance Method Details

#call(table, extension) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/dba/dump.rb', line 13

def call(table, extension)
  self.table_name = table

  output_path = "#{table_name}.#{extension}"

  adapter = ADAPTERS.fetch(extension) { raise DBA::Error, 'unsupported file extension' }

  adapter = DBA.const_get(adapter)

  rows = database[table_name].count

  return if rows.zero?

  adapter.dump(database, table_name, output_path)
end