From 4233d2149f0caa243ba7299dd05496999d453bd3 Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 11 Apr 2018 20:49:38 -0500 Subject: [PATCH 1/2] Fix delete_all / destroy_all calls. --- app/logical/daily_maintenance.rb | 4 ++-- app/models/user_password_reset_nonce.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/logical/daily_maintenance.rb b/app/logical/daily_maintenance.rb index e5d66318c..7e8b02d32 100644 --- a/app/logical/daily_maintenance.rb +++ b/app/logical/daily_maintenance.rb @@ -3,8 +3,8 @@ class DailyMaintenance ActiveRecord::Base.connection.execute("set statement_timeout = 0") PostPruner.new.prune! TagPruner.new.prune! - Upload.delete_all(['created_at < ?', 1.day.ago]) - Delayed::Job.delete_all(['created_at < ?', 45.days.ago]) + Upload.where('created_at < ?', 1.day.ago).delete_all + Delayed::Job.where('created_at < ?', 45.days.ago).delete_all PostVote.prune! CommentVote.prune! ApiCacheGenerator.new.generate_tag_cache diff --git a/app/models/user_password_reset_nonce.rb b/app/models/user_password_reset_nonce.rb index fbdc0e151..012720a2d 100644 --- a/app/models/user_password_reset_nonce.rb +++ b/app/models/user_password_reset_nonce.rb @@ -5,7 +5,7 @@ class UserPasswordResetNonce < ApplicationRecord after_create :deliver_notice def self.prune! - destroy_all(["created_at < ?", 1.week.ago]) + where("created_at < ?", 1.week.ago).destroy_all end def deliver_notice From 159b0e8a22c7e7d2211396525b1e3903039c84cc Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 11 Apr 2018 20:50:27 -0500 Subject: [PATCH 2/2] tests: add daily & weekly maintenance tests. --- test/unit/daily_maintenance_test.rb | 33 ++++++++++++++++++++++++++++ test/unit/weekly_maintenance_test.rb | 13 +++++++++++ 2 files changed, 46 insertions(+) create mode 100644 test/unit/daily_maintenance_test.rb create mode 100644 test/unit/weekly_maintenance_test.rb diff --git a/test/unit/daily_maintenance_test.rb b/test/unit/daily_maintenance_test.rb new file mode 100644 index 000000000..13198deff --- /dev/null +++ b/test/unit/daily_maintenance_test.rb @@ -0,0 +1,33 @@ +require 'test_helper' + +class DailyMaintenanceTest < ActiveSupport::TestCase + context "daily maintenance" do + setup do + # have ApiCacheGenerator save files to a temp dir. + @temp_shared_dir_path = "/tmp/#{SecureRandom.uuid}" + Danbooru.config.stubs(:shared_dir_path).returns(@temp_shared_dir_path) + + FactoryBot.create(:tag, post_count: 1) # for ApiCacheGenerator + FactoryBot.create(:admin_user) # for SuperVoter.init! + end + + teardown do + FileUtils.rm_rf(@temp_shared_dir_path) + Danbooru.config.unstub(:shared_dir_path) + end + + should "work" do + assert_nothing_raised { DailyMaintenance.new.run } + end + + should "prune expired posts" do + @pending = FactoryBot.create(:post, is_pending: true, created_at: 4.days.ago) + @flagged = FactoryBot.create(:post, is_flagged: true, created_at: 4.days.ago) + + DailyMaintenance.new.run + + assert(true, @pending.reload.is_deleted) + assert(true, @flagged.reload.is_deleted) + end + end +end diff --git a/test/unit/weekly_maintenance_test.rb b/test/unit/weekly_maintenance_test.rb new file mode 100644 index 000000000..278c7184e --- /dev/null +++ b/test/unit/weekly_maintenance_test.rb @@ -0,0 +1,13 @@ +require 'test_helper' + +class WeeklyMaintenanceTest < ActiveSupport::TestCase + context "weekly maintenance" do + should "prune password resets" do + @user = FactoryBot.create(:user, email: "test@example.com") + @nonce = FactoryBot.create(:user_password_reset_nonce, email: "test@example.com", created_at: 1.month.ago) + + WeeklyMaintenance.new.run + assert_equal(0, UserPasswordResetNonce.count) + end + end +end