Skip to content

Add a foreign key from officials to users - #215

Merged
russellporter merged 2 commits into
masterfrom
t3/officials-foreign-key
Nov 30, 2026
Merged

russellporter merged 2 commits into
masterfrom
t3/officials-foreign-key

Conversation

@russellporter

@russellporter russellporter commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

#213 is deployed, and the only orphaned official in production has been removed. The migration fails if there are any, which stops deploy.sh (including the weekly deploy) after it has started the new containers. Just before merging, check that this still returns an empty list in the production console:

Official.where.missing(:user).pluck(:id, :user_id, :official_classification_id, :date)
  • Convert officials to InnoDB and give user_id the same type as users.id. In production, officials uses MyISAM and its user_id is signed, but users.id is unsigned. schema.rb doesn't show either difference, so CI can't catch them. On MyISAM, MariaDB accepts the foreign key without creating it, so the migration would appear to succeed.
  • Add the foreign key with the default restrict behaviour, so deleting a user who still has certifications fails instead of deleting them. Account merges move these rows, and account deletion removes them first.
  • If there are orphaned rows, the migration lists their IDs and stops before changing anything. It doesn't delete them.
  • Remove the "Unknown user" fallback and its test from Fix PDF course maps, and officials left behind by account merges #213, because the constraint prevents that case.

Rolling back removes the foreign key, but leaves the table on InnoDB with the new column type.

Testing

  • All 325 non-system tests pass in an image built from this branch, with Ruby 3.3.14 and MariaDB 12.3.
  • I tested the migration on MariaDB 12.3 with the same engine and ID types as production:
    • Without the conversion, it's recorded as complete but adds no foreign key.
    • With the conversion, it changes nothing if there's an orphaned row. Once that row is removed, it adds the foreign key and keeps the remaining row.
    • MariaDB then refuses to add an official without a user, or delete a user who still has one.
    • Rolling it back and running it again works.
  • Rolling it back and running it again also works on a database created from schema.rb.

Base automatically changed from t3/fix-pdf-upload-officials to master November 30, 2026 18:56
@russellporter
russellporter force-pushed the t3/officials-foreign-key branch from 60119f7 to b4f6025 Compare November 30, 2026 19:15
Nothing stopped officials pointing at a deleted user, which broke the officials
page. Deleting a user who still has officials now fails. The migration lists
any officials without a user and stops, since they may be real certifications.

With the constraint in place, the page no longer needs its missing user
fallback.
@russellporter
russellporter force-pushed the t3/officials-foreign-key branch from b4f6025 to 56540a9 Compare November 30, 2026 22:25
@russellporter
russellporter marked this pull request as ready for review November 30, 2026 22:25
@russellporter
russellporter marked this pull request as draft November 30, 2026 22:31
Production's officials table is MyISAM, so MariaDB accepted the foreign
key without creating it. Its user_id is also signed, but users.id is
unsigned.
@russellporter
russellporter marked this pull request as ready for review November 30, 2026 22:41
@russellporter
russellporter merged commit dea201e into master Nov 30, 2026
8 checks passed
@russellporter
russellporter deleted the t3/officials-foreign-key branch November 30, 2026 22:41
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