From e828c8857622e5fc420a7ffc0629bf74a2e7b5a5 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Tue, 16 Feb 2021 12:52:34 +0100 Subject: [PATCH] Add rake task to remove trashed elements We removed the element trashed with Alchemy 6.0 This task helps to clean up the database with a Rake task. bin/rake alchemy:tidy:remove_trashed_elements --- lib/alchemy/tasks/tidy.rb | 17 +++++++++++++++++ lib/tasks/alchemy/tidy.rake | 6 ++++++ 2 files changed, 23 insertions(+) diff --git a/lib/alchemy/tasks/tidy.rb b/lib/alchemy/tasks/tidy.rb index e60c37547f..93c308cd2f 100644 --- a/lib/alchemy/tasks/tidy.rb +++ b/lib/alchemy/tasks/tidy.rb @@ -62,6 +62,23 @@ def remove_orphaned_elements end end + def remove_trashed_elements + puts "\n## Removing trashed elements" + elements = Alchemy::Element.unscoped.where(position: nil) + if elements.any? + log "Destroying #{elements.size} trashed elements" + nested_elements, parent_elements = elements.partition(&:parent_element_id) + (nested_elements + parent_elements).each do |element| + element.destroy + print "." + end + puts "\n" + log "Done", :message + else + log "No trashed elements found", :skip + end + end + def remove_orphaned_contents puts "\n## Removing orphaned contents" contents = Alchemy::Content.unscoped.all diff --git a/lib/tasks/alchemy/tidy.rake b/lib/tasks/alchemy/tidy.rake index 29b390d026..097a6828e0 100644 --- a/lib/tasks/alchemy/tidy.rake +++ b/lib/tasks/alchemy/tidy.rake @@ -8,6 +8,7 @@ namespace :alchemy do Rake::Task["alchemy:tidy:element_positions"].invoke Rake::Task["alchemy:tidy:content_positions"].invoke Rake::Task["alchemy:tidy:remove_orphaned_records"].invoke + Rake::Task["alchemy:tidy:remove_trashed_elements"].invoke end desc "Fixes element positions." @@ -36,6 +37,11 @@ namespace :alchemy do Alchemy::Tidy.remove_orphaned_contents end + desc "Remove trashed elements." + task remove_trashed_elements: [:environment] do + Alchemy::Tidy.remove_trashed_elements + end + desc "List Alchemy elements usage" task elements_usage: :environment do puts "\n"