diff --git a/lib/alchemy/tasks/tidy.rb b/lib/alchemy/tasks/tidy.rb index 61051c45a1..d5c4bf084a 100644 --- a/lib/alchemy/tasks/tidy.rb +++ b/lib/alchemy/tasks/tidy.rb @@ -117,12 +117,20 @@ def remove_orphaned_contents end end + def remove_duplicate_folded_pages + Alchemy::FoldedPage.select(:page_id, :user_id).group(:page_id, :user_id).having("count(*) > 1").each do |duplicate_page| + duplicate_page_ids = Alchemy::FoldedPage.where(duplicate_page.attributes.except("id")).pluck(:id) + duplicate_page_ids_except_oldest = duplicate_page_ids.reject { |i| i == duplicate_page_ids.min } + Alchemy::FoldedPage.where(id: duplicate_page_ids_except_oldest).delete_all + end + end + private def destroy_orphaned_records(records, class_name) records.each do |record| log "Destroy orphaned #{class_name}: #{record.inspect}" - record.destroy + record.reload.try(:destroy) end end end diff --git a/lib/tasks/alchemy/tidy.rake b/lib/tasks/alchemy/tidy.rake index e052b7993a..df90880dd9 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_duplicate_folded_pages'].invoke end desc "Creates missing cells for pages." @@ -56,5 +57,10 @@ namespace :alchemy do task remove_orphaned_contents: [:environment] do Alchemy::Tidy.remove_orphaned_contents end + + desc "Remove duplicate folded pages." + task remove_duplicate_folded_pages: [:environment] do + Alchemy::Tidy.remove_duplicate_folded_pages + end end end