-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_one_time.rake
More file actions
186 lines (150 loc) · 6.6 KB
/
fix_one_time.rake
File metadata and controls
186 lines (150 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
namespace :fix_one_time do
desc "Fix translation duplications 08.2021"
task translation_duplicates: :environment do
for_real = ENV["FOR_REAL"] == "true"
puts "RUNNING FOR REAL" if for_real
puts "DRY RUN" unless for_real
# this is one time script to fix translation duplications
# First time I ran the scripts that removed only duplicates that had same values
# using that kind of script
# delete from operator_translations a
# using operator_translations b
# where a.updated_at < b.updated_at
# and a.operator_id = b.operator_id
# and a.locale = b.locale
# and coalesce(a.name, '') = coalesce(b.name, '')
# and coalesce(a.details, '') = coalesce(b.details, '')
# after that this list of potentially problematic entities left
# {"fmu_id"=>46}
# {"fmu_id"=>47}
# {"fmu_id"=>49}
# {"fmu_id"=>92}
# {"observer_id"=>1}
# {"observer_id"=>8}
# {"operator_id"=>179}
# {"operator_id"=>197}
# {"operator_id"=>10589}
# {"operator_id"=>10604}
# {"operator_id"=>10615}
# {"operator_id"=>100090}
# {"operator_id"=>100091}
# {"operator_id"=>100115}
# {"operator_id"=>100151}
# {"operator_id"=>120128}
# {"operator_id"=>120133}
# easier to keep the last updated_at translation value and remove the rest, then
# check all problematic entities manually in active admin to see if everything is fine
ActiveRecord::Base.transaction do
# I care only about those here, use other rake task to find duplicates
%w[operator fmu observer observation country].each do |model|
remove_query = <<~SQL
delete from #{model}_translations a
using #{model}_translations b
where a.updated_at < b.updated_at
and a.#{model}_id = b.#{model}_id
and a.locale = b.locale
SQL
puts "Removing #{model} translations duplicates: #{ActiveRecord::Base.connection.delete(remove_query)}"
end
raise ActiveRecord::Rollback unless for_real
end
end
desc "Re-assign uploaded attachment to observation reports"
task observation_report_attachments: :environment do
# context
# some reports were renamed couple months ago, files were moved, but after that DB was recreated from backup
# files now have different name then what is in attachment column
# I'm going to just check what are the files in uploads directory
# and update attachment column in DB with the real filename
for_real = ENV["FOR_REAL"] == "true"
puts "RUNNING FOR REAL" if for_real
puts "DRY RUN" unless for_real
path = Rails.public_path.join("uploads", "observation_report", "attachment")
report_filename_hash = Dir.glob("#{path}/**/*")
.reject { |fn| File.directory?(fn) }
.map { |file| file.gsub(path.to_s + "/", "") }
.map { |file| file.split("/") }
.sort_by { |report_id, filename| report_id.to_i }
.to_h
report_filename_hash.transform_keys!(&:to_i)
puts "COUNT with not existing attachments: #{ObservationReport.all.count { |r| r.read_attribute(:attachment).present? && !r.attachment.exists? }}"
ActiveRecord::Base.transaction do
ObservationReport.find_each do |report|
next if report.attachment.nil?
filename_in_storage = report_filename_hash[report.id]
next if filename_in_storage.nil?
next if filename_in_storage == report.read_attribute(:attachment)
puts "WRONG attachment name for report #{report.id}: is: #{report.read_attribute(:attachment)}, should be: #{filename_in_storage}"
report.update_columns(attachment: filename_in_storage)
end
still_without = ObservationReport.all.select { |r| r.read_attribute(:attachment).present? && !r.attachment.exists? }
puts "COUNT with not existing attachments: #{still_without.count}"
still_without.each do |report|
puts "REPORT #{report.id} still without attachment: #{report.attachment}"
end
raise ActiveRecord::Rollback unless for_real
end
end
desc "Tasks to move already deleted reports attachments and evidences to private directory"
task make_deleted_evidences_private: :environment do
for_real = ENV["FOR_REAL"] == "true"
puts "RUNNING FOR REAL" if for_real
puts "DRY RUN" unless for_real
puts "Deleted Observation documents/evidence: #{ObservationDocument.deleted.count}"
puts "With attachment: #{ObservationDocument.deleted.where.not(attachment: ["", nil]).count}"
missing = 0
moved = 0
ObservationDocument.deleted.where.not(attachment: ["", nil]).find_each do |doc|
next if doc.attachment.exists?
public_filepath = File.dirname(doc.attachment.file.file.gsub("/private/", "/public/"))
unless File.exist?(public_filepath)
puts "missing file: #{public_filepath}"
missing += 1
next
end
puts "moving file: #{public_filepath}"
doc.send(:move_attachment_to_private_directory) if for_real
moved += 1
end
puts "Missing files: #{missing}"
puts "Moved files: #{moved}"
end
desc "Mark expired documents there were expired from not required state as not provided"
task not_required_expired_move_to_not_provided: :environment do
for_real = ENV["FOR_REAL"] == "true"
puts "RUNNING FOR REAL" if for_real
puts "DRY RUN" unless for_real
ActiveRecord::Base.transaction do
docs = OperatorDocument
.where(status: "doc_expired", updated_at: 1.month.ago..Time.zone.today)
.select do |o|
prev_version_not_required = false
o.versions.reverse.each do |version|
next if version.reify.status == "doc_expired"
prev_version_not_required = version.reify.status == "doc_not_required"
break
end
prev_version_not_required
end
docs_count = docs.count
puts "FOUND #{docs.count} docs expired that were not required before"
puts "DOC_IDS: #{docs.map(&:id).join(",")}"
operators = docs.map(&:operator).uniq
puts "OPERATORS:"
operators.each { |op| puts "#{op.name} (#{op.id})" }
# that will regenerate documents to not provided state
puts "REGENERATING DOCS..."
docs.each do |doc|
doc.skip_score_recalculation = true # just do it once for each operator at the end
doc.destroy!
end
operators.each { |operator| ScoreOperatorDocument.recalculate!(operator) }
if docs.each(&:reload).count { |d| d.status == "doc_not_provided" } == docs_count
puts "ALL GOOD :)"
else
puts "ERROR: doc count after docs regenerate does not match"
end
raise ActiveRecord::Rollback unless for_real
end
end
end