Skip to content

Fix migrations when restoring prod database, sort deterministically when ordering property|tax states#5261

Open
nllong wants to merge 6 commits into
developfrom
fix-migrations
Open

Fix migrations when restoring prod database, sort deterministically when ordering property|tax states#5261
nllong wants to merge 6 commits into
developfrom
fix-migrations

Conversation

@nllong

@nllong nllong commented Jul 7, 2026

Copy link
Copy Markdown
Member

Any background context you want to provide?

When trying to restore the production database, a couple issues appeared with the migrations around primary key constraints.

What's this PR do?

  • Fixed prod migrations by restoring missing primary key constraints on orgs_organization, orgs_organizationuser,
    seed_cycle, and seed_goal.
  • Restored the missing unique_user_for_organization constraint and added guarded repair migrations so existing prod DBs can migrate without failing on later foreign keys.
  • Also fixes a small edge case of non-determinism in tests/operation where two records that are created at the exact same time could return different ordering. Updated by including the 'id' in the order by ( list(StateClass.objects.filter(id__in=state_ids).order_by("updated", "id").values_list("id", flat=True))

How should this be manually tested?

ideally restore prod database and make sure things hold together.

What are the relevant tickets?

Screenshots (if appropriate)

@nllong nllong changed the title Fix migrations when restorting prod database Fix migrations when restoring prod database Jul 7, 2026
@nllong nllong added the Maintenance Tag as maintenance if the issue relates to general cleanup, maintenance, etc. Do not delete label. label Jul 7, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces guarded “repair” migrations to restore missing primary key and unique constraints in production databases where historical migrations were recorded as applied but constraints were not actually present, preventing later foreign key migrations from succeeding.

Changes:

  • Adds an orgs repair migration to restore orgs_organization / orgs_organizationuser primary keys and the unique_user_for_organization uniqueness constraint.
  • Updates seed migration 0250 to proactively repair missing constraints before adding fields/foreign keys.
  • Adds a follow-up seed repair migration 0254 to fix already-migrated databases past 0250.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
seed/migrations/0254_repair_referenced_primary_keys.py New seed repair migration to (idempotently) restore missing PKs needed by later FKs.
seed/migrations/0250_remove_goal_current_cycle_goal_partner_note_and_more.py Adds guarded constraint repair SQL ahead of schema changes to avoid FK failures on broken prod restores.
seed/lib/superperms/orgs/migrations/0046_repair_organizationuser_constraints.py New orgs repair migration restoring PKs and expected uniqueness constraint.
.spelling.dic Adds a handful of allowed dictionary words used in the repo/tooling.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread seed/migrations/0254_repair_referenced_primary_keys.py Outdated
Comment thread seed/migrations/0250_remove_goal_current_cycle_goal_partner_note_and_more.py Outdated
Comment thread seed/lib/superperms/orgs/migrations/0046_repair_organizationuser_constraints.py Outdated
nllong and others added 3 commits July 7, 2026 11:11
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@nllong nllong requested a review from kflemin July 7, 2026 17:13
@nllong nllong changed the title Fix migrations when restoring prod database Fix migrations when restoring prod database, sort deterministically when ordering property|tax states Jul 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Maintenance Tag as maintenance if the issue relates to general cleanup, maintenance, etc. Do not delete label.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants