Skip to content

Conversation

@Luke-Oldenburg
Copy link
Contributor

Summary of the problem

This is the prerequisite to dropping logins.referral_program_id

Describe your changes

@Luke-Oldenburg Luke-Oldenburg requested a review from a team December 11, 2025 20:36
class BackfillLinkIdOnLogin < ApplicationJob
def perform
Login.where.not(referral_program_id: nil).find_each do |login|
login.update!(referral_link_id: login.referral_program.links.first.id)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The backfill job can crash with a NoMethodError by calling .id on login.referral_program.links.first when a program has no associated links, resulting in nil.
Severity: CRITICAL | Confidence: High

🔍 Detailed Analysis

The backfill job at app/jobs/one_time_jobs/backfill_link_id_on_login.rb updates a login record using login.referral_program.links.first.id. However, a Referral::Program can exist without any associated Referral::Link objects, especially for programs created before the link functionality was added. When a program has no links, links.first returns nil. Calling .id on nil raises a NoMethodError, which will crash the entire backfill job because it lacks error handling for this case, leaving the data migration incomplete.

💡 Suggested Fix

Use safe navigation (&.) to prevent the error when no link is found. Update the line to login.update!(referral_link_id: login.referral_program.links.first&.id). This will safely assign nil to referral_link_id if no link exists, preventing the job from crashing.

🤖 Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: app/jobs/one_time_jobs/backfill_link_id_on_login.rb#L7

Potential issue: The backfill job at
`app/jobs/one_time_jobs/backfill_link_id_on_login.rb` updates a `login` record using
`login.referral_program.links.first.id`. However, a `Referral::Program` can exist
without any associated `Referral::Link` objects, especially for programs created before
the link functionality was added. When a program has no links, `links.first` returns
`nil`. Calling `.id` on `nil` raises a `NoMethodError`, which will crash the entire
backfill job because it lacks error handling for this case, leaving the data migration
incomplete.

Did we get this right? 👍 / 👎 to inform future reviews.
Reference ID: 7234613

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All programs have a link right now so this should be fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants