lib/loaders/excel_loader.rb in datashift-0.12.0 vs lib/loaders/excel_loader.rb in datashift-0.12.1

- old
+ new

@@ -40,11 +40,10 @@ @excel = Excel.new @excel.open(file_name) - #if(options[:verbose]) puts "\n\n\nLoading from Excel file: #{file_name}" sheet_number = options[:sheet_number] || 0 @sheet = @excel.worksheet( sheet_number ) @@ -70,11 +69,12 @@ # Create a method_mapper which maps list of headers into suitable calls on the Active Record class # For example if model has an attribute 'price' will map columns called Price, price, PRICE etc to this attribute populate_method_mapper_from_headers( @headers, options ) - logger.info "Excel Loader processing #{@sheet.num_rows} rows" + # currently pointless num_rows rubbish i.e inaccurate! + #logger.info "Excel Loader processing #{@sheet.num_rows} rows" @reporter.reset begin puts "Dummy Run - Changes will be rolled back" if options[:dummy] @@ -127,11 +127,18 @@ rescue => e @reporter.processed_object_count += 1 failure(@current_row, true) + + if(verbose) + puts "Failed to process row [#{i}] (#{@current_row})" + puts e.inspect + end + logger.error "Failed to process row [#{i}] (#{@current_row})" + # don't forget to reset the load object new_load_object next end @@ -146,28 +153,28 @@ failure logger.error "Failed to save row [#{@current_row}]" logger.error load_object.errors.inspect if(load_object) else logger.info "Row #{@current_row} succesfully SAVED : ID #{load_object.id}" - @reporter.add_loaded_object(@load_object) + @reporter.add_loaded_object(@load_object) end # don't forget to reset the object or we'll update rather than create new_load_object end - - raise ActiveRecord::Rollback if(options[:dummy]) # Don't actually create/upload to DB if we are doing dummy run - end + + if(options[:dummy]) + puts "Excel loading stage complete - Dummy run so Rolling Back." + raise ActiveRecord::Rollback # Don't actually create/upload to DB if we are doing dummy run + end + + end # TRANSACTION N.B ActiveRecord::Rollback does not propagate outside of the containing transaction block - rescue => e - - if e.is_a?(ActiveRecord::Rollback) && options[:dummy] - puts "Excel loading stage complete - Dummy run so Rolling Back." - else - raise e - end - ensure + rescue => e + puts "ERROR: Excel loading failed : #{e.inspect}" + raise e + ensure report end end \ No newline at end of file