Module: DlJusyoJp::Import

Defined in:
lib/dl_jusyo_jp/import.rb

Class Method Summary collapse

Class Method Details

.chunk_sizeObject



17
18
19
# File 'lib/dl_jusyo_jp/import.rb', line 17

def self.chunk_size
  1000
end

.executeObject



6
7
8
9
10
11
12
13
14
15
# File 'lib/dl_jusyo_jp/import.rb', line 6

def self.execute
  CSV.foreach(Download.csv.path, encoding: "Shift_JIS:UTF-8", write_headers: false).each_slice(chunk_size) do |rows|
    insert = "INSERT INTO #{DlJusyoJp::ADDRESS_DOWNLOAD_TABLE} VALUES "
    values = rows.map do |row|
      next unless row[0].match(/^\d+$/) # skip header
      '(NULL,' + row.map{|col| "'#{col}'"}.join(',') + ')'
    end.compact.join(',')
    ActiveRecord::Base.connection.exec_insert("#{insert}#{values}", 'jusyo.jp bulk insert', [])
  end
end