Class: DbBackupTool::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/db_backup_tool/loader.rb

Instance Method Summary collapse

Constructor Details

#initialize(table, options = {}) ⇒ Loader

Returns a new instance of Loader.



4
5
6
# File 'lib/db_backup_tool/loader.rb', line 4

def initialize(table, options = {})
  @table, @options = table, options.dup
end

Instance Method Details

#batch_sizeObject



17
18
19
# File 'lib/db_backup_tool/loader.rb', line 17

def batch_size
  @options[:batch_size] || 1000
end

#load(io) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/db_backup_tool/loader.rb', line 8

def load(io)
  csv = CSV.new(io, headers: true)
  
  @table.truncate
  csv.each_slice(batch_size) do |rows|
    @table.update rows.map(&:to_hash)
  end
end