Class: ZipMeta::MigrationUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/zip_meta/migration_util.rb

Overview

Contains a static “load_zipcode_data” method that parses the “zipcodes.csv” file and loads each row into the “zip_meta” database table.

Class Method Summary collapse

Class Method Details

.load_zipcode_dataObject

Method to parse the “zipcodes.csv” file and load each row into the “zip_meta” database table.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/zip_meta/migration_util.rb', line 10

def self.load_zipcode_data
  require 'csv'

  csv_path = File.expand_path('../../data/zipcode.csv', __FILE__)
  csv_data = CSV.open(csv_path, 'r')
  rows = []
  csv_data.each do |row|
    rows << row
  end
  keys = rows[0]
  rows.delete_at(0)

  connection = ActiveRecord::Base.connection
        
  len = rows.length      
  rows.each_with_index do |row, index|
    vals = "('#{row[0]}', '#{row[1].sql_escape_single_quotes}', '#{row[2].sql_escape_single_quotes}', #{row[3]}, #{row[4]}, #{row[5]})"
    
    query = "INSERT INTO zip_meta(#{keys[0]}, #{keys[1]}, #{keys[2]}, #{keys[3]}, #{keys[4]}, #{keys[5]}) " + \
    "VALUES #{vals}"        
    connection.execute(query)

    puts "adding zip meta info row #{index} of #{len}\n"        
  end
end