-
Notifications
You must be signed in to change notification settings - Fork 3
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
base: master
Are you sure you want to change the base?
Changes from all commits
bb669d4
6948ca2
3a4745d
48bd31a
2a52f24
dbb12cd
06450c8
845ce0a
1f2be59
1d13c35
0dc0587
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
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' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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 = {} | ||
|
@@ -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 | ||
|
@@ -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| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(/'/, "\\'") }'" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we want to keep There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method is also changed in this PR https://github.com/primeroIMS/primero-v2-migration/pull/4/files |
||
end | ||
|
||
def parse_date_and_location_fields(object, data_hash) | ||
|
There was a problem hiding this comment.
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