Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 779766e

Browse files
committed
Add unique index on summaries and dedup existing records
1 parent 175db51 commit 779766e

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

app/models/ai_summary.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,6 @@ def outdated
5151
#
5252
# Indexes
5353
#
54-
# index_ai_summaries_on_target_type_and_target_id (target_type,target_id)
54+
# idx_on_target_id_target_type_summary_type_3355609fbb (target_id,target_type,summary_type) UNIQUE
55+
# index_ai_summaries_on_target_type_and_target_id (target_type,target_id)
5556
#
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
class UniqueAiSummaries < ActiveRecord::Migration[7.1]
3+
def up
4+
execute <<~SQL
5+
DELETE FROM ai_summaries ais1
6+
USING ai_summaries ais2
7+
WHERE ais1.id < ais2.id
8+
AND ais1.target_id = ais2.target_id
9+
AND ais1.target_type = ais2.target_type
10+
AND ais1.summary_type = ais2.summary_type
11+
SQL
12+
13+
add_index :ai_summaries, %i[target_id target_type summary_type], unique: true
14+
end
15+
16+
def down
17+
remove_index :ai_summaries, column: %i[target_id target_type summary_type]
18+
end
19+
end

0 commit comments

Comments
 (0)