Skip to content

Fix record history export #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion migration/v1_to_v2/data/export_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def exporters
timestamp = DateTime.now.strftime('%Y%m%d%H%M%S')
export_dir = "record-data-files-#{timestamp}"
exporters.each do |exporter|
data_exporter = Object.const_get(exporter).new(export_dir: export_dir, batch_size: 250)
data_exporter = Object.const_get(exporter).new(export_dir: export_dir)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep 250 batch_size and overwrite in attachment_exporter

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[this change is not related with record history export, please create a new pr]

data_exporter.export
end

83 changes: 54 additions & 29 deletions migration/v1_to_v2/data/exporters/attachment_exporter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require_relative('data_exporter.rb')
require 'erb'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[these changes is not related with record history export, please create a new pr]


# rubocop:disable Metrics/ClassLength
# Class that get record's attachments and generate files to be inserted
Expand All @@ -18,7 +19,7 @@ class AttachmentExporter < DataExporter
'other_documents' => 'other_documents'
}.freeze

def initialize(options = {})
def initialize(options = { batch_size: 50 })
super(options)
@indent = 0
@json_to_export = {}
Expand Down Expand Up @@ -94,9 +95,14 @@ def build_data_to_attach(files, folder, type, form)
name = value.is_a?(String) ? value : value.file_name

@json_to_export[type][@record_id][form] << {
record_type: @record_type.to_s, record_id: @record_id, field_name: form,
file_name: name, date: value.try(:date), comments: value.try(:comments),
is_current: value.try(:is_current) || false, description: value.try(:document_description),
record_type: @record_type.to_s,
record_id: @record_id,
field_name: form,
file_name: name,
date: value.try(:date),
comments: value.try(:comments),
is_current: value.try(:is_current) || false,
description: value.try(:document_description),
path: "/#{@record_id}/#{form}/#{name}"
}
end
Expand Down Expand Up @@ -152,34 +158,53 @@ def add_date_to_file_for_attachment(date)
@output.puts "attachement.date = \"#{date.strftime('%Y-%m-%d')}\""
end

def write_script_for_attachment(form_name, files)
files.each do |data|
@output.puts "puts 'Inserting \"#{get_attachment_type(data[:path])}\" to #{data[:record_id]}'"
@output.puts "attachement = Attachment.new(#{data.except(:path, :field_name, :date)})"
add_date_to_file_for_attachment(data[:date])
@output.puts "attachement.record_type = #{data[:record_type]}"
add_attachment_type_to_file_for_attachment(data[:path])
@output.puts "attachement.field_name = '#{get_field_name(form_name)}'"
@output.puts "attachement.file.attach(io: File.open(\"\#{File.dirname(__FILE__)}#{data[:path]}\"), filename: '#{data[:file_name]}')"
@output.puts "begin"
@output.puts " attachement.save!"
@output.puts "rescue StandardError => e"
@output.puts " puts \"Cannot attach #{data[:file_name]}. Error \#{e.message}\""
@output.puts "end\n\n\n"
ATTACHMENT_IMPORTER_TEMPLATE = ERB.new(<<~RUBY)
puts "Inserting <%= get_attachment_type(data[:path]) %> to <%= data[:record_id] >"
attachement = Attachment.new(<%= data.except(:path, :field_name, :date) %>)
<% if date.present? %>attachement.date = "<%= date.strftime('%Y-%m-%d') %>"<% end %>
attachement.record_type = <%= data[:record_type] %>
attachement.attachment_type = "<%= get_attachment_type(path) %>"
attachement.field_name = "<%= get_field_name(form_name) %>"
attachement.file.attach(io: File.open("\#{File.dirname(__FILE__)}<%= data[:path] %>"), filename: <%= data[:file_name].inspect %>)
begin
attachement.save!
rescue StandardError => e
puts "Cannot attach <%= data[:file_name].inspect %>. Error \#{e.message}"
end
end

def build_file(folder_to_save, type, sufix)
initialize_script_for_attachment(folder_to_save, type, sufix)
data_object_names.each do |object_name|
next if @json_to_export[object_name].blank?
RUBY

@json_to_export[object_name].values.each do |value|
value.each do |form_name, files|
write_script_for_attachment(form_name, files)
end
end
end
def render_attachment_importer(form_name, data)
@output.puts ATTACHMENT_IMPORTER_TEMPLATE.result(binding)
end

def build_file(folder_to_save, type, sufix)
initialize_script_for_attachment(folder_to_save, type, sufix)

attachments = []
data_object_names.each do |type|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a reduce/inject to avoid initialize array, please create a new method

records = @json_to_export[type]

next if records.blank?

records.values.each do |form|
form.each do |form_name, files|
next if files.nil?

attachments.push(*files.map { |file| [form_name, file] })
end
end
end


attachments.each_slice(10) do |slice|
slice.each do |form, file|
render_attachment_importer(form, file)
end

@output.puts "# force ruby to gargabe collect here as attachmented files can build up in memory!"
@output.puts "GC.start"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GC will be enable for every iteration. is not need to stop manually?

end
end
end
# rubocop:enable Metrics/ClassLength
8 changes: 1 addition & 7 deletions migration/v1_to_v2/data/exporters/data_exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,8 @@ def value_to_ruby_string(value, include_blank = false)
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/AbcSize

def valid_key?(key)
return false if key.is_a?(Integer) || key.include?('-') || key.include?(' ') || !key.match?('^[a-zA-Z]')

true
end

def key_to_ruby(key)
valid_key?(key) ? key : "'#{key}'"
"'#{ key.to_s.gsub(/'/, "\\'") }'"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we want to keep valid_key? method call here, in order to avoid unnecesary conversion of a valid key

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

end

def parse_date_and_location_fields(object, data_hash)
Expand Down