From b2acc7dbb855b1a3ac2e0e9eb027ab0f5f016678 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?KMY=EF=BC=88=E9=9B=AA=E3=81=82=E3=81=99=E3=81=8B=EF=BC=89?= Date: Wed, 28 Feb 2024 08:34:47 +0900 Subject: [PATCH] =?UTF-8?q?Add:=20#581=20NG=E3=83=AF=E3=83=BC=E3=83=89?= =?UTF-8?q?=E6=A4=9C=E5=87=BA=E5=B1=A5=E6=AD=B4=E3=82=92=E5=AE=9A=E6=9C=9F?= =?UTF-8?q?=E7=9A=84=E3=81=AB=E5=89=8A=E9=99=A4=E3=81=99=E3=82=8B=E5=87=A6?= =?UTF-8?q?=E7=90=86=20(#619)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add: #581 NGワード検出履歴を定期的に削除する処理 * マイグレーションコードを修正 --- app/lib/vacuum/ng_histories_vacuum.rb | 1 + ...40227222450_index_to_sort_for_ng_word_created_date.rb | 9 +++++++++ db/schema.rb | 3 ++- lib/tasks/dangerous.rake | 1 + spec/fabricators/ngword_history_fabricator.rb | 9 +++++++++ spec/lib/vacuum/ng_histories_vacuum_spec.rb | 4 ++++ 6 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 db/post_migrate/20240227222450_index_to_sort_for_ng_word_created_date.rb create mode 100644 spec/fabricators/ngword_history_fabricator.rb diff --git a/app/lib/vacuum/ng_histories_vacuum.rb b/app/lib/vacuum/ng_histories_vacuum.rb index 204e6de23f..f70558f116 100644 --- a/app/lib/vacuum/ng_histories_vacuum.rb +++ b/app/lib/vacuum/ng_histories_vacuum.rb @@ -12,6 +12,7 @@ class Vacuum::NgHistoriesVacuum private def vacuum_histories! + NgwordHistory.where('created_at < ?', HISTORY_LIFE_DURATION.ago).in_batches.destroy_all NgRuleHistory.where('created_at < ?', HISTORY_LIFE_DURATION.ago).in_batches.destroy_all end end diff --git a/db/post_migrate/20240227222450_index_to_sort_for_ng_word_created_date.rb b/db/post_migrate/20240227222450_index_to_sort_for_ng_word_created_date.rb new file mode 100644 index 0000000000..672eb3c8a7 --- /dev/null +++ b/db/post_migrate/20240227222450_index_to_sort_for_ng_word_created_date.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class IndexToSortForNgWordCreatedDate < ActiveRecord::Migration[7.1] + disable_ddl_transaction! + + def change + add_index :ngword_histories, :created_at, algorithm: :concurrently + end +end diff --git a/db/schema.rb b/db/schema.rb index aeb81e046e..88760ee1ea 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_02_27_033337) do +ActiveRecord::Schema[7.1].define(version: 2024_02_27_222450) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -936,6 +936,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_02_27_033337) do t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "count", default: 0, null: false + t.index ["created_at"], name: "index_ngword_histories_on_created_at" t.index ["uri", "keyword", "created_at"], name: "index_ngword_histories_on_uri_and_keyword_and_created_at" t.index ["uri", "reason", "created_at"], name: "index_ngword_histories_on_uri_and_reason_and_created_at" end diff --git a/lib/tasks/dangerous.rake b/lib/tasks/dangerous.rake index ac3895811a..9d3e04a369 100644 --- a/lib/tasks/dangerous.rake +++ b/lib/tasks/dangerous.rake @@ -90,6 +90,7 @@ namespace :dangerous do 20240217230006 20240218233621 20240227033337 + 20240227222450 ) # Removed: account_groups target_tables = %w( diff --git a/spec/fabricators/ngword_history_fabricator.rb b/spec/fabricators/ngword_history_fabricator.rb new file mode 100644 index 0000000000..35d3b02669 --- /dev/null +++ b/spec/fabricators/ngword_history_fabricator.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +Fabricator(:ngword_history) do + uri 'https://test.com/' + target_type 0 + reason 0 + text 'this is an invalid text' + keyword 'invalid' +end diff --git a/spec/lib/vacuum/ng_histories_vacuum_spec.rb b/spec/lib/vacuum/ng_histories_vacuum_spec.rb index d75d7f353b..31f10f1ffd 100644 --- a/spec/lib/vacuum/ng_histories_vacuum_spec.rb +++ b/spec/lib/vacuum/ng_histories_vacuum_spec.rb @@ -6,6 +6,8 @@ RSpec.describe Vacuum::NgHistoriesVacuum do subject { described_class.new } describe '#perform' do + let!(:word_history_old) { Fabricate(:ngword_history, created_at: 30.days.ago) } + let!(:word_history_recent) { Fabricate(:ngword_history, created_at: 2.days.ago) } let!(:rule_history_old) { Fabricate(:ng_rule_history, created_at: 30.days.ago) } let!(:rule_history_recent) { Fabricate(:ng_rule_history, created_at: 2.days.ago) } @@ -14,10 +16,12 @@ RSpec.describe Vacuum::NgHistoriesVacuum do end it 'deletes old history' do + expect { word_history_old.reload }.to raise_error ActiveRecord::RecordNotFound expect { rule_history_old.reload }.to raise_error ActiveRecord::RecordNotFound end it 'does not delete recent history' do + expect { word_history_recent.reload }.to_not raise_error expect { rule_history_recent.reload }.to_not raise_error end end