Skip to content

Commit

Permalink
migration to remove duplicated github handles
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienpoly committed Feb 22, 2025
1 parent 486fdea commit bfa3644
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
5 changes: 3 additions & 2 deletions app/models/speaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# bio :text default(""), not null
# bsky :string default(""), not null
# bsky_metadata :json not null
# github :string default(""), not null
# github :string default(""), not null, indexed
# github_metadata :json not null
# linkedin :string default(""), not null
# mastodon :string default(""), not null
Expand All @@ -26,6 +26,7 @@
# Indexes
#
# index_speakers_on_canonical_id (canonical_id)
# index_speakers_on_github (github) UNIQUE WHERE github IS NOT NULL AND github != ''
# index_speakers_on_name (name)
# index_speakers_on_slug (slug) UNIQUE
#
Expand Down Expand Up @@ -66,7 +67,7 @@ class Speaker < ApplicationRecord

# validations
validates :canonical, exclusion: {in: ->(speaker) { [speaker] }, message: "can't be itself"}
validates :github, uniqueness: {case_sensitive: false}
validates :github, uniqueness: {case_sensitive: false}, allow_blank: true

# scope
scope :with_talks, -> { where.not(talks_count: 0) }
Expand Down
32 changes: 27 additions & 5 deletions db/migrate/20250128070756_add_unique_index_on_github_in_speaker.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
class AddUniqueIndexOnGitHubInSpeaker < ActiveRecord::Migration[8.0]
def change
# remove all github handles for speakers having a canonical speaker
Speaker.where.not(canonical_id: nil).update_all(github: "")
puts Speaker.group(:github).having("count(*) > 1").pluck(:github)
add_index :speakers, :github, unique: true
def up
# remove duplicate with no talks
Speaker.group(:github).having("count(*) > 1").pluck(:github).each do |github|
Speaker.where(github: github, talks_count: 0).update_all(github: "")
end

# some speaker with a canonical speaker still have talks attached to them let's reasign them
Speaker.not_canonical.where.not(talks_count: 0).each do |speaker|
speaker.assign_canonical_speaker!(canonical_speaker: speaker.canonical)
end

# fix one by one
Speaker.find_by(name: "Andrew").assign_canonical_speaker!(canonical_speaker: Speaker.find_by(name: "Andrew Nesbitt"))
Speaker.find_by(name: "HASUMI Hitoshi").assign_canonical_speaker!(canonical_speaker: Speaker.find_by(name: "Hitoshi Hasumi"))
Speaker.find_by(name: "hogelog").assign_canonical_speaker!(canonical_speaker: Speaker.find_by(name: "Sunao Hogelog Komuro"))
Speaker.find_by(name: "Jônatas Paganini").assign_canonical_speaker!(canonical_speaker: Speaker.find_by(name: "Jônatas Davi Paganini"))
Speaker.find_by(name: "Sutou Kouhei").assign_canonical_speaker!(canonical_speaker: Speaker.find_by(name: "Kouhei Sutou"))
Speaker.find_by(slug: "maciek-rzasa").assign_canonical_speaker!(canonical_speaker: Speaker.find_by(slug: "maciej-rzasa"))
Speaker.find_by(slug: "mario-alberto-chavez").assign_canonical_speaker!(canonical_speaker: Speaker.find_by(slug: "mario-chavez"))
Speaker.find_by(slug: "enrique-morellon").assign_canonical_speaker!(canonical_speaker: Speaker.find_by(slug: "enrique-mogollan"))
Speaker.find_by(slug: "masafumi-okura").assign_canonical_speaker!(canonical_speaker: Speaker.find_by(slug: "okura-masafumi"))
Speaker.find_by(slug: "oliver-lacan").assign_canonical_speaker!(canonical_speaker: Speaker.find_by(slug: "olivier-lacan"))
add_index :speakers, :github, unique: true, where: "github IS NOT NULL AND github != ''"
end

def down
remove_index :speakers, :github
end
end
1 change: 1 addition & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion test/fixtures/speakers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# bio :text default(""), not null
# bsky :string default(""), not null
# bsky_metadata :json not null
# github :string default(""), not null
# github :string default(""), not null, indexed
# github_metadata :json not null
# linkedin :string default(""), not null
# mastodon :string default(""), not null
Expand All @@ -26,6 +26,7 @@
# Indexes
#
# index_speakers_on_canonical_id (canonical_id)
# index_speakers_on_github (github) UNIQUE WHERE github IS NOT NULL AND github != ''
# index_speakers_on_name (name)
# index_speakers_on_slug (slug) UNIQUE
#
Expand Down

0 comments on commit bfa3644

Please sign in to comment.