Skip to content

Commit

Permalink
Add rake task to remove trashed elements
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tvdeyen committed Feb 16, 2021
1 parent 619e9cf commit e828c88
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/alchemy/tasks/tidy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions lib/tasks/alchemy/tidy.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit e828c88

Please sign in to comment.