From 5204e9ab7be1b884ce8b2e85b2e239fcf3126c6c Mon Sep 17 00:00:00 2001 From: Mateusz Grotek Date: Thu, 25 Jul 2024 16:28:57 +0100 Subject: [PATCH] Rake task for filling in content_id column --- lib/tasks/fill_in_content_id_in_list_items.rake | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 lib/tasks/fill_in_content_id_in_list_items.rake diff --git a/lib/tasks/fill_in_content_id_in_list_items.rake b/lib/tasks/fill_in_content_id_in_list_items.rake new file mode 100644 index 000000000..7ac0fbe60 --- /dev/null +++ b/lib/tasks/fill_in_content_id_in_list_items.rake @@ -0,0 +1,15 @@ +desc "Fill in content ids in list items after adding content_id database column." +task fill_in_content_id_in_list_items: :environment do + ListItem.find_each do |list_item| + next if list_item.read_attribute(:content_id).present? + + base_path = list_item.base_path + list_item.content_id = list_item.list.tag.tagged_document_for_base_path(base_path)&.content_id + + unless list_item.read_attribute(:content_id).present? && list_item.save + puts "Could not set content_id of the following list item: #{list_item.id} #{list_item.base_path} #{list_item.title}" + end + rescue StandardError => e + puts "Exception #{e} raised when trying to set content_id of the following list item: #{list_item.id} #{list_item.base_path} #{list_item.title}" + end +end