From 43593404f05225c32f99d1ec0a7a2d829646d750 Mon Sep 17 00:00:00 2001 From: Laura Ghiorghisor Date: Tue, 19 Nov 2024 23:43:49 +0000 Subject: [PATCH] Add temproary soft delete bulk rake task This is part of running a clean-up on assets that should be deleted, according to whitehall. We discovered a set of assets that have an invalid deleted state, so we're resetting them to "uploaded". --- lib/tasks/assets.rake | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/tasks/assets.rake b/lib/tasks/assets.rake index ee81be3e..76a325d7 100644 --- a/lib/tasks/assets.rake +++ b/lib/tasks/assets.rake @@ -32,4 +32,22 @@ namespace :assets do asset = WhitehallAsset.find_by!(legacy_url_path:) puts "Asset ID for #{legacy_url_path} is #{asset.id}." end + + desc "Soft delete assets and check deleted invalid state" + task :bulk_soft_delete, %i[csv_path] => :environment do |_t, args| + csv_path = args.fetch(:csv_path) + + CSV.foreach(csv_path, headers: false) do |row| + asset_id = row[0] + asset = Asset.find(asset_id) + asset.state = "uploaded" if asset.state == "deleted" + + begin + asset.destroy! + print "." + rescue Mongoid::Errors::Validations + puts "Failed to delete asset of ID #{asset_id}: #{asset.errors.full_messages}" + end + end + end end