diff --git a/app/lib/vacuum/ng_histories_vacuum.rb b/app/lib/vacuum/ng_histories_vacuum.rb index 204e6de23fa0e6..f70558f1160085 100644 --- a/app/lib/vacuum/ng_histories_vacuum.rb +++ b/app/lib/vacuum/ng_histories_vacuum.rb @@ -12,6 +12,7 @@ def perform 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/migrate/20240227222450_index_to_sort_for_ng_word_created_date.rb b/db/migrate/20240227222450_index_to_sort_for_ng_word_created_date.rb new file mode 100644 index 00000000000000..672eb3c8a796a8 --- /dev/null +++ b/db/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 aeb81e046ecfee..88760ee1eaaa0f 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 @@ 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/spec/fabricators/ngword_history_fabricator.rb b/spec/fabricators/ngword_history_fabricator.rb new file mode 100644 index 00000000000000..35d3b02669de65 --- /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 d75d7f353b3dad..31f10f1ffd5b40 100644 --- a/spec/lib/vacuum/ng_histories_vacuum_spec.rb +++ b/spec/lib/vacuum/ng_histories_vacuum_spec.rb @@ -6,6 +6,8 @@ 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 @@ 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