Skip to content

Commit 17bcd89

Browse files
committed
allow skipping individual steps
1 parent 8969591 commit 17bcd89

1 file changed

Lines changed: 12 additions & 9 deletions

File tree

app/models/archiving/exporter.rb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,25 @@ def initialize(relations: nil, dont_implicitly_expand: [])
2626
end
2727

2828
# Verbose will log SQL queries.
29-
def export(verbose: false)
29+
# Skip is an array of steps to skip ("relations", "forms", "hints", "responses", "attachments").
30+
def export(verbose: false, skip: [])
31+
skip = skip.map(&:to_s)
3032
elapsed = Benchmark.realtime do
3133
if verbose
32-
perform_export
34+
perform_export(skip: skip)
3335
else
34-
silence_verbose_logs { perform_export }
36+
silence_verbose_logs { perform_export(skip: skip) }
3537
end
3638
end
3739
puts "Export took #{elapsed.round(2)}s"
3840
end
3941

40-
def perform_export
42+
def perform_export(skip: [])
4143
warnings = []
4244

4345
FileUtils.mkdir_p(export_dir)
4446
Zip::OutputStream.open(zipfile_path) do |out|
47+
self.relations = [] if skip.include?("relations")
4548
puts "Exporting relations: #{relations.map(&:klass).join(', ')}..."
4649
expander = RelationExpander.new(relations, dont_implicitly_expand: dont_implicitly_expand)
4750
expander.expanded.each do |klass, relations|
@@ -60,7 +63,7 @@ def perform_export
6063
end
6164
end
6265

63-
items = Form.with_attached_odk_xml
66+
items = skip.include?("forms") ? Form.none : Form.with_attached_odk_xml
6467
total_count = items.count
6568
curr_count = 0
6669
items.find_each do |form|
@@ -75,7 +78,7 @@ def perform_export
7578
out.write(form.odk_xml.download)
7679
end
7780

78-
items = Question.joins(:media_prompt_attachment).with_attached_media_prompt
81+
items = skip.include?("hints") ? Question.none : Question.joins(:media_prompt_attachment).with_attached_media_prompt
7982
total_count = items.count
8083
curr_count = 0
8184
items.find_each do |question|
@@ -86,7 +89,7 @@ def perform_export
8689
out.write(mp.download)
8790
end
8891

89-
items = Response
92+
items = skip.include?("responses") ? Response.none : Response.all
9093
total_count = items.count
9194
curr_count = 0
9295
items.find_each do |response|
@@ -96,7 +99,7 @@ def perform_export
9699
warn(warnings, "Response #{response.id} was dirty") if response.dirty_json
97100
end
98101

99-
items = Media::Object.joins(:item_attachment).with_attached_item
102+
items = skip.include?("attachments") ? Media::Object.none : Media::Object.joins(:item_attachment).with_attached_item
100103
total_count = items.count
101104
curr_count = 0
102105
items.find_each do |obj|
@@ -119,7 +122,7 @@ def perform_export
119122
end
120123

121124
puts
122-
Rails.logger.warn("Warnings:\n#{warnings.join("\n")}\n")
125+
Rails.logger.warn("Warnings:\n#{warnings.join("\n")}\n") unless warnings.empty?
123126
puts "Exported #{zipfile_path}"
124127
puts "Encountered #{warnings.count} warnings."
125128
end

0 commit comments

Comments
 (0)