-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make player names unique within a tournament. (#412)
* Make player names unique within a tournament. * Fix lint.
- Loading branch information
Showing
2 changed files
with
19 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
class MakePlayerNamesUnique < ActiveRecord::Migration[7.2] | ||
def change | ||
# Add a "(duplicate #N)"" suffix to any duplicate player names before applying the index. | ||
dupes = Player.select(:tournament_id, :name) | ||
.group(:tournament_id, :name) | ||
.having('COUNT(*) > 1') | ||
dupes.each do |dupe| | ||
players = Player.where(tournament_id: dupe.tournament_id, name: dupe.name) | ||
players.each_with_index do |player, index| | ||
player.update!(name: "#{player.name} (duplicate ##{index + 1})") | ||
end | ||
end | ||
add_index :players, %i[tournament_id name], unique: true, name: 'idx_uniq_players_tournament_id_name' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters