Feature #22701 » 0003-Mulit-pass-CSV-import.patch
| app/models/import.rb | ||
|---|---|---|
|
end
|
||
|
# Imports items and returns the position of the last processed item
|
||
|
def run(options={})
|
||
|
def run(options = {}, current_pass = completed_passes + 1)
|
||
|
if current_pass > required_passes
|
||
|
# Abort recursion
|
||
|
update_attribute :finished, true
|
||
|
remove_file
|
||
|
return total_items
|
||
|
end
|
||
|
max_items = options[:max_items]
|
||
|
max_time = options[:max_time]
|
||
|
current = 0
|
||
|
imported = 0
|
||
|
resume_after = items.maximum(:position) || 0
|
||
|
interrupted = false
|
||
|
started_on = Time.now
|
||
|
read_items do |row, position|
|
||
|
if (max_items && imported >= max_items) || (max_time && Time.now >= started_on + max_time)
|
||
|
current = run_pass(options, current_pass) do |position|
|
||
|
if (max_items && imported >= max_items) || (max_time && Time.now - started_on >= max_time)
|
||
|
# interrupt import
|
||
|
interrupted = true
|
||
|
break
|
||
|
false
|
||
|
else
|
||
|
# continue importing item
|
||
|
imported += 1
|
||
|
true
|
||
|
end
|
||
|
if position > resume_after
|
||
|
item = items.build
|
||
|
item.position = position
|
||
|
if object = build_object(row)
|
||
|
if object.save
|
||
|
item.obj_id = object.id
|
||
|
else
|
||
|
item.message = object.errors.full_messages.join("\n")
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
item.save!
|
||
|
imported += 1
|
||
|
if !interrupted
|
||
|
update_attribute(:total_items, current) if total_items.nil?
|
||
|
if max_items
|
||
|
options = options.merge(:max_items => max_items - imported)
|
||
|
end
|
||
|
current = position
|
||
|
if max_time
|
||
|
options = options.merge(:max_time => max_time - (Time.now - started_on))
|
||
|
end
|
||
|
run(options, current_pass + 1)
|
||
|
else
|
||
|
current
|
||
|
end
|
||
|
end
|
||
|
def run_pass(options={}, current_pass)
|
||
|
resume_after = items.where(:completed_passes => current_pass).maximum(:position) || 0
|
||
|
if imported == 0 || interrupted == false
|
||
|
if total_items.nil?
|
||
|
update_attribute :total_items, current
|
||
|
current = 0
|
||
|
read_items do |row, position|
|
||
|
next unless position > resume_after
|
||
|
break unless yield(position)
|
||
|
current = position
|
||
|
item = items.where(:position => position).first_or_initialize
|
||
|
if object = build_object(row, item, current_pass)
|
||
|
if object.save
|
||
|
item.obj_id = object.id
|
||
|
else
|
||
|
item.message = object.errors.full_messages.join("\n")
|
||
|
end
|
||
|
end
|
||
|
update_attribute :finished, true
|
||
|
remove_file
|
||
|
item.completed_passes = current_pass
|
||
|
item.save!
|
||
|
end
|
||
|
current
|
||
| ... | ... | |
|
items.where("obj_id IS NOT NULL")
|
||
|
end
|
||
|
# Should be overridden in sub class to implement multi-pass import
|
||
|
def required_passes
|
||
|
1
|
||
|
end
|
||
|
def completed_passes
|
||
|
if total_items.present? && items.count == total_items
|
||
|
items.minimum(:completed_passes)
|
||
|
else
|
||
|
0
|
||
|
end
|
||
|
end
|
||
|
private
|
||
|
def read_rows
|
||
| ... | ... | |
|
# Builds a record for the given row and returns it
|
||
|
# To be implemented by subclasses
|
||
|
def build_object(row)
|
||
|
def build_object(row, item, pass)
|
||
|
raise NotImplementedError, "Subclass responsibility"
|
||
|
end
|
||
|
# Generates a filename used to store the import file
|
||
| app/models/import_item.rb | ||
|---|---|---|
|
belongs_to :import
|
||
|
validates_presence_of :import_id, :position
|
||
|
validates_numericality_of :completed_passes, :only_integer => true,
|
||
|
:greater_than_or_equal_to => 0
|
||
|
end
|
||
| app/models/issue_import.rb | ||
|---|---|---|
|
mapping['create_versions'] == '1'
|
||
|
end
|
||
|
def required_passes
|
||
|
if mapping['parent_issue_id']
|
||
|
2
|
||
|
else
|
||
|
1
|
||
|
end
|
||
|
end
|
||
|
private
|
||
|
def build_object(row)
|
||
|
issue = Issue.new
|
||
|
issue.author = user
|
||
|
def build_object(row, item, pass)
|
||
|
if (pass == 1)
|
||
|
issue = Issue.new
|
||
|
issue.author = user
|
||
|
build_issue(row, issue)
|
||
|
else
|
||
|
issue = Issue.find(item.obj_id)
|
||
|
build_relations(row, issue)
|
||
|
end
|
||
|
issue.notify = false
|
||
|
issue
|
||
|
end
|
||
|
def build_issue(row, issue)
|
||
|
attributes = {
|
||
|
'project_id' => mapping['project_id'],
|
||
|
'tracker_id' => mapping['tracker_id'],
|
||
| ... | ... | |
|
attributes['is_private'] = '1'
|
||
|
end
|
||
|
end
|
||
|
if parent_issue_id = row_value(row, 'parent_issue_id')
|
||
|
if parent_issue_id =~ /\A(#)?(\d+)\z/
|
||
|
parent_issue_id = $2
|
||
|
if $1
|
||
|
attributes['parent_issue_id'] = parent_issue_id
|
||
|
elsif issue_id = items.where(:position => parent_issue_id).first.try(:obj_id)
|
||
|
attributes['parent_issue_id'] = issue_id
|
||
|
end
|
||
|
else
|
||
|
attributes['parent_issue_id'] = parent_issue_id
|
||
|
end
|
||
|
end
|
||
|
if start_date = row_date(row, 'start_date')
|
||
|
attributes['start_date'] = start_date
|
||
|
end
|
||
| ... | ... | |
|
issue.send :safe_attributes=, attributes, user
|
||
|
issue
|
||
|
end
|
||
|
def build_relations(row, issue)
|
||
|
attributes = {}
|
||
|
if parent_issue_id = row_value(row, 'parent_issue_id')
|
||
|
if parent_issue_id =~ /\A(#)?(\d+)\z/
|
||
|
parent_issue_id = $2
|
||
|
if $1
|
||
|
attributes['parent_issue_id'] = parent_issue_id
|
||
|
elsif issue_id = items.where(:position => parent_issue_id).first.try(:obj_id)
|
||
|
attributes['parent_issue_id'] = issue_id
|
||
|
end
|
||
|
else
|
||
|
attributes['parent_issue_id'] = parent_issue_id
|
||
|
end
|
||
|
end
|
||
|
issue.send :safe_attributes=, attributes, user
|
||
|
issue
|
||
|
end
|
||
|
end
|
||
| db/migrate/20160426125940_add_completed_passes_to_import_items.rb | ||
|---|---|---|
|
class AddCompletedPassesToImportItems < ActiveRecord::Migration
|
||
|
def self.up
|
||
|
add_column :import_items, :completed_passes, :integer, :default => 0, :null => false
|
||
|
end
|
||
|
def self.donw
|
||
|
remove_column :imports_item, :completed_passes
|
||
|
end
|
||
|
end
|
||
| test/unit/issue_import_test.rb | ||
|---|---|---|
|
import.run
|
||
|
assert !File.exists?(file_path)
|
||
|
end
|
||
|
def test_multi_step_run
|
||
|
# max_items < total_items
|
||
|
import = generate_import_with_mapping
|
||
|
assert_difference 'Issue.count', 5 do
|
||
|
assert_equal 2, import.run(:max_items => 2)
|
||
|
assert_not import.finished
|
||
|
assert_equal 4, import.run(:max_items => 2)
|
||
|
assert_not import.finished
|
||
|
assert_equal 5, import.run(:max_items => 2)
|
||
|
assert import.finished
|
||
|
end
|
||
|
# max_items > total_items
|
||
|
import = generate_import_with_mapping
|
||
|
assert_difference 'Issue.count', 5 do
|
||
|
assert_equal 5, import.run(:max_items => 6)
|
||
|
assert import.finished
|
||
|
end
|
||
|
# max_items == total_items
|
||
|
import = generate_import_with_mapping
|
||
|
assert_difference 'Issue.count', 5 do
|
||
|
assert_equal 5, import.run(:max_items => 5)
|
||
|
assert import.finished
|
||
|
end
|
||
|
end
|
||
|
def test_multi_step_multi_pass_run
|
||
|
# max_items < total_items
|
||
|
import = generate_import_with_mapping
|
||
|
import.mapping.merge!('parent_issue_id' => '5')
|
||
|
assert_difference 'Issue.count', 5 do
|
||
|
assert_equal 2, import.run(:max_items => 2)
|
||
|
assert_not import.finished
|
||
|
assert_equal 4, import.run(:max_items => 2)
|
||
|
assert_not import.finished
|
||
|
assert_equal 1, import.run(:max_items => 2)
|
||
|
assert_not import.finished
|
||
|
assert_equal 3, import.run(:max_items => 2)
|
||
|
assert_not import.finished
|
||
|
assert_equal 5, import.run(:max_items => 2)
|
||
|
assert import.finished
|
||
|
end
|
||
|
# max_items > total_items
|
||
|
import = generate_import_with_mapping
|
||
|
import.mapping.merge!('parent_issue_id' => '5')
|
||
|
assert_difference 'Issue.count', 5 do
|
||
|
assert_equal 1, import.run(:max_items => 6)
|
||
|
assert_not import.finished
|
||
|
assert_equal 5, import.run(:max_items => 6)
|
||
|
assert import.finished
|
||
|
end
|
||
|
# max_items == total_items
|
||
|
import = generate_import_with_mapping
|
||
|
import.mapping.merge!('parent_issue_id' => '5')
|
||
|
assert_difference 'Issue.count', 5 do
|
||
|
assert_equal 0, import.run(:max_items => 5)
|
||
|
assert_not import.finished
|
||
|
assert_equal 5, import.run(:max_items => 5)
|
||
|
assert import.finished
|
||
|
end
|
||
|
end
|
||
|
def test_required_passes
|
||
|
# Imports w/o relation mappings need just a single pass
|
||
|
import = generate_import_with_mapping
|
||
|
assert_equal 1, import.required_passes
|
||
|
import.run
|
||
|
assert_equal 1, import.completed_passes
|
||
|
# Imports w/ references to other rows need 2 passes
|
||
|
import = generate_import_with_mapping
|
||
|
import.mapping.merge!('parent_issue_id' => '5')
|
||
|
assert_equal 2, import.required_passes
|
||
|
import.run
|
||
|
assert_equal 2, import.completed_passes
|
||
|
end
|
||
|
end
|
||
- « Previous
- 1
- 2
- 3
- Next »