Skip to content

Commit

Permalink
Testing import from arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbeedle committed Apr 18, 2016
1 parent 76e716f commit 08853f9
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 18 deletions.
6 changes: 1 addition & 5 deletions lib/usda_nutrient_database/import/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ def extract_row(row)
end

def build_object(row)
find_or_initialize(row).tap do |object|
columns.each_with_index do |column, index|
object.send("#{column}=", row[index])
end
end
columns.map.with_index { |_, index| row[index] }
end

def columns
Expand Down
8 changes: 6 additions & 2 deletions lib/usda_nutrient_database/import/food_groups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ def log_import_started
end

def save_objects
UsdaNutrientDatabase::FoodGroup.import(objects_to_import, {
validate: false
UsdaNutrientDatabase::FoodGroup.import(columns, objects_to_import, {
validate: false,
on_duplicate_key_update: {
conflict_target: :code,
columns: %i(description)
}
})
end
end
Expand Down
8 changes: 6 additions & 2 deletions lib/usda_nutrient_database/import/foods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ def columns
end

def save_objects
UsdaNutrientDatabase::Food.import(objects_to_import, {
validate: false
UsdaNutrientDatabase::Food.import(columns, objects_to_import, {
validate: false,
on_duplicate_key_update: {
conflict_target: :nutrient_databank_number,
columns: columns
}
})
end
end
Expand Down
17 changes: 14 additions & 3 deletions lib/usda_nutrient_database/import/foods_nutrients.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ class FoodsNutrients < Base

private

def apply_typecasts(row)
row[8] = row[8] == 'Y'
row
end

def columns
[
:nutrient_databank_number, :nutrient_number, :nutrient_value,
Expand All @@ -30,9 +35,15 @@ def log_import_started
end

def save_objects
UsdaNutrientDatabase::FoodsNutrient.import(objects_to_import, {
validate: false
})
objects_to_import.each_slice(10000) do |to_import|
UsdaNutrientDatabase::FoodsNutrient.import(columns, to_import, {
validate: false,
on_dupliate_key_update: {
conflict_target: %i(nutrient_databank_numner nutrient_number),
columns: columns
}
})
end
end
end
end
Expand Down
8 changes: 6 additions & 2 deletions lib/usda_nutrient_database/import/nutrients.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ def log_import_started
end

def save_objects
UsdaNutrientDatabase::Nutrient.import(objects_to_import, {
validate: false
UsdaNutrientDatabase::Nutrient.import(columns, objects_to_import, {
validate: false,
on_duplicate_key_update: {
conflict_target: :nutrient_number,
columns: columns
}
})
end
end
Expand Down
8 changes: 6 additions & 2 deletions lib/usda_nutrient_database/import/source_codes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ def filename
end

def save_objects
UsdaNutrientDatabase::SourceCode.import(objects_to_import, {
validate: false
UsdaNutrientDatabase::SourceCode.import(columns, objects_to_import, {
validate: false,
on_duplicate_key_update: {
conflict_target: :code,
columns: columns
}
})
end
end
Expand Down
8 changes: 6 additions & 2 deletions lib/usda_nutrient_database/import/weights.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ def log_import_started
end

def save_objects
UsdaNutrientDatabase::Weight.import(objects_to_import, {
validate: false
UsdaNutrientDatabase::Weight.import(columns, objects_to_import, {
validate: false,
on_duplicate_key_update: {
conflict_target: %i(nutrient_databank_number sequence_number),
columns: columns
}
})
end
end
Expand Down

0 comments on commit 08853f9

Please sign in to comment.