fix: backfill dividend investment_amount_cents and enforce NOT NULL #1603
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes #1596
Problem
Historical dividend records have
NULLforinvestment_amount_cents, causing "N/A" to appear in the "Investment amount" column on/equity/dividendsand/equity/dividend_rounds/round/:idpages.The column was added as nullable in PR #796, but existing dividend records were never backfilled.
Solution
1. One-time backfill script (
Onetime::BackfillDividendInvestmentAmounts)Follows the existing
Onetime::service pattern (likePopulateCompanyInvestorEntities). Run via Rails console before deploying the migration:Backfill strategy:
number_of_shares IS NOT NULL): Usescompany_investor.investment_amount_in_centsnumber_of_shares IS NULL): UsesSUM(convertible_securities.principal_value_in_cents)for that investorNULLvalues to02. Migration to enforce NOT NULL
After backfill, adds
NOT NULLconstraint todividends.investment_amount_cents. Includes a safety net that sets any remainingNULLvalues to0before applying the constraint.3. Model validation
Adds
validates :investment_amount_cents, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }to theDividendmodel, matching the pattern used onCompanyInvestorEntity.4. Frontend cleanup
/equity/dividendspagehasInvestmentAmountscheck on/equity/dividend_rounds/round/:id— the "Investment amount" column is now always showninvestmentAmountCentsas.notNull()Deployment steps
Onetime::BackfillDividendInvestmentAmounts.new.performin production consolerails db:migrate)AI Disclosure
I have used Claude Code (Opus 4.6) to investigate the codebase and implement the fix.