Skip to content

Reference the voter's latest CastVote instead of copying the vote - #556

Draft
devin-ai-integration[bot] wants to merge 3 commits into
masterfrom
devin/1794190838-voter-cast-vote-ref
Draft

devin-ai-integration[bot] wants to merge 3 commits into
masterfrom
devin/1794190838-voter-cast-vote-ref

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Voter.vote holds a full copy of the latest CastVote.vote, which doubles the vote storage. This is step 1 of removing that copy: it adds Voter.cast_vote (FK to the latest CastVote, SET_NULL, related_name='+'), and reads go through it. Voter.vote is still written, so rolling back is safe.

  • store_vote sets cast_vote in addition to the copy.
  • last_cast_vote() returns the referenced row, or, for voters not backfilled yet, falls back to building one from Voter.vote.
  • compute_tally and num_cast_votes select voters with cast_vote or vote set, which is the same set as before for unlinked voters. compute_tally iterates select_related('cast_vote') in chunks of 500 instead of loading every voter's vote into memory at once.
  • ballot_list no longer loads any vote JSON (.defer('vote'), short cast votes only need vote_hash and cast_at).

Migration 0013_voter_cast_vote (non-atomic, so it doesn't block cast votes on the large voter table):

ALTER TABLE helios_voter ADD COLUMN cast_vote_id integer NULL;  -- metadata only
CREATE INDEX CONCURRENTLY helios_voter_cast_vote_idx ...;
ALTER TABLE helios_voter ADD CONSTRAINT ... FOREIGN KEY ... NOT VALID;
ALTER TABLE helios_voter VALIDATE CONSTRAINT ...;  -- no write lock while scanning

Backfill (manage.py backfill_voter_cast_votes [--batch-size 1000] [-v 2]), run after deploy. It goes through unlinked voters that have a vote_hash or a Voter.vote copy, in id-ordered batches, and links each one to its latest CastVote with the same vote_hash, but only if that row's vote equals the voter's copy (as text, falling back to parsed JSON). That way the linked ballot is always the one that was being tallied. Everyone else is counted, not linked, and listed with -v 2:

  • no cast vote with that hash, or a vote_hash but no copy
  • a cast vote whose vote differs from the copy
  • a copy but no vote_hash (still tallied today, so these must be handled before the copy is dropped)

The update re-checks cast_vote_id IS NULL AND vote_hash = ..., so a voter who recasts during the backfill isn't overwritten. It can be re-run and ends with a still unlinked count.

Later (not in this PR): once still unlinked is 0 (or the leftover legacy voters are handled), stop writing Voter.vote, drop the fallback, RemoveField('voter', 'vote'), then pg_repack/VACUUM FULL helios_voter to get the space back.

#554 and #555 are stacked on this PR.

Link to Devin session: https://app.devin.ai/sessions/83534237e8474452b51f8e49103c543c
Open in Devin Desktop: https://app.devin.ai/desktop/session/83534237e8474452b51f8e49103c543c?variant=devin
Requested by: @benadida


🟢 Devin fixed the finding · Follow Devin's work

Devin Review

….vote copy

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

…a vote copy

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Devin Review found 1 finding. Devin automatically fixed it.

Fixed by Devin (1)
  • Hash-only voters break or change tallies — Tally/num_cast_votes now select voters with cast_vote or vote set (old semantics for unlinked voters), and the backfill only links voters whose vote copy equals the cast vote, so hash-only voters are reported instead of linked. Test added.

View findings in Devin Review · Follow Devin's work

Devin Review

…fill

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

This branch has not been deployed

No deployments
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.

1 participant