@@ -9,11 +9,24 @@ class AiChampionMatrix < ApplicationRecord
99 scope :with_sufficient_sample , -> { where ( 'total_games >= ?' , 10 ) }
1010
1111 def self . upsert_win ( winner , loser , patch : nil , league : nil )
12- matrix = find_or_initialize_by ( champion_a : winner , champion_b : loser , patch : patch , league : league )
13- matrix . wins_a = matrix . wins_a . to_i + 1
14- matrix . total_games = matrix . total_games . to_i + 1
15- matrix . updated_at = Time . current
16- matrix . save!
12+ # Two separate partial indexes cover the two cases:
13+ # - both null → index_ai_champion_matrices_null_pair
14+ # - both present → index_ai_champion_matrices_unique
15+ index = if patch . nil? && league . nil?
16+ :index_ai_champion_matrices_null_pair
17+ else
18+ :index_ai_champion_matrices_unique
19+ end
20+ upsert (
21+ { champion_a : winner , champion_b : loser , patch : patch , league : league ,
22+ wins_a : 1 , total_games : 1 , updated_at : Time . current } ,
23+ unique_by : index ,
24+ on_duplicate : Arel . sql (
25+ 'wins_a = ai_champion_matrices.wins_a + 1, ' \
26+ 'total_games = ai_champion_matrices.total_games + 1, ' \
27+ 'updated_at = excluded.updated_at'
28+ )
29+ )
1730 end
1831
1932 def win_rate
0 commit comments