Skip to content

Commit

Permalink
Make player names unique within a tournament. (#412)
Browse files Browse the repository at this point in the history
* Make player names unique within a tournament.

* Fix lint.
  • Loading branch information
plural authored Feb 17, 2025
1 parent eee6494 commit 3f400b7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
17 changes: 17 additions & 0 deletions db/migrate/20250217194837_make_player_names_unique.rb
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
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.2].define(version: 2024_11_16_155531) do
ActiveRecord::Schema[7.2].define(version: 2025_02_17_194837) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -140,6 +140,7 @@
t.integer "fixed_table_number"
t.index ["corp_identity_ref_id"], name: "index_players_on_corp_identity_ref_id"
t.index ["runner_identity_ref_id"], name: "index_players_on_runner_identity_ref_id"
t.index ["tournament_id", "name"], name: "idx_uniq_players_tournament_id_name", unique: true
t.index ["tournament_id"], name: "index_players_on_tournament_id"
t.index ["user_id"], name: "index_players_on_user_id"
end
Expand Down

0 comments on commit 3f400b7

Please sign in to comment.