Skip to content

Commit

Permalink
Add: #581 NGワード検出履歴を定期的に削除する処理
Browse files Browse the repository at this point in the history
  • Loading branch information
kmycode committed Feb 27, 2024
1 parent aec832e commit 64070d8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/lib/vacuum/ng_histories_vacuum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions spec/fabricators/ngword_history_fabricator.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions spec/lib/vacuum/ng_histories_vacuum_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand All @@ -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
Expand Down

0 comments on commit 64070d8

Please sign in to comment.