Sha256: 44ca2ace19924c74e5b36bbd46e10792dca0cbb13f8fd3becd512b56617fdd06
Contents?: true
Size: 1.79 KB
Versions: 2
Compression:
Stored size: 1.79 KB
Contents
require_relative 'utilities' module Ferry class Importer < Utilities def row_sql_format(hash, columns, adapter) values = hash.values_at(*columns) values.map! do |value| if(adapter=="mysql2" && (value=='t' || value =='f')) value=='t' ? value=1 : value=0 #attempt to convert 't' and 'f' to int in mysql end value = ActiveRecord::Base::sanitize(value) end "(#{values.join(",")})" end def insert_sql(model, columns, values) col_names_sql = "(#{columns.join(",")})" model_sql = model.downcase sql_insert_beg = "INSERT INTO #{model_sql} #{col_names_sql} VALUES " ActiveRecord::Base.transaction do values.each_slice(500) do |records| sql_statement = sql_insert_beg + records.join(",") + ";" ActiveRecord::Base.connection.execute(sql_statement) end end end def import(environment, model, filename) db_connect(environment) adapter = YAML::load(IO.read("config/database.yml"))[environment]["adapter"] if(File.extname(filename) != ".csv") raise "Only csv file types are supported" return false end lines = CSV.read(filename)#encoding option here? certain characters might break db if(lines.nil?) raise "#{filename} file not found" return false end import_bar = ProgressBar.new("import", lines.length-1) col_names = lines.shift records = [] lines.each do |line| record = Hash[col_names.zip line] records << record end values = [] records.map do |record| values << row_sql_format(record, col_names, adapter) import_bar.inc end insert_sql(model, col_names, values) puts "" puts "csv imported to #{model} table" end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
ferry-1.1.1 | lib/ferry/importer.rb |
ferry-1.1.0 | lib/ferry/importer.rb |