diff --git a/lib/tasks/deployment/20220506105510_fix_wrong_parent.rake b/lib/tasks/deployment/20220506105510_fix_wrong_parent.rake new file mode 100644 index 000000000..e2e9893ad --- /dev/null +++ b/lib/tasks/deployment/20220506105510_fix_wrong_parent.rake @@ -0,0 +1,32 @@ +namespace :after_party do + desc 'Deployment task: fix_wrong_parent' + task fix_wrong_parent: :environment do + children = ProcedureRevisionTypeDeChamp.where.not(parent_id: nil).includes(:parent) + + rake_puts "#{children.count} children to check" + + progress = ProgressReport.new(children.count) + + misconfigured = children.filter do |child| + progress.inc + child.revision_id != child.parent.revision_id + end + progress.finish + + rake_puts "#{misconfigured.count} children to fix" + + progress = ProgressReport.new(misconfigured.count) + + misconfigured.each do |child| + new_parent = ProcedureRevisionTypeDeChamp.find_by(revision: child.revision, type_de_champ_id: child.parent.type_de_champ_id) + child.update(parent_id: new_parent.id) + progress.inc + end + progress.finish + + # Update task as completed. If you remove the line below, the task will + # run with every deploy (or every time you call after_party:run). + AfterParty::TaskRecord + .create version: AfterParty::TaskRecorder.new(__FILE__).timestamp + end +end