Skip to content

Fix upgrade failure when existing OAuth2 tokens hit oauth2_provider 0012 before main 0198 - #1185

Closed
ssimpson89 wants to merge 2 commits into
ctrliq:mainfrom
ssimpson89:fix/oauth2-token-checksum-migration-order
Closed

ssimpson89 wants to merge 2 commits into
ctrliq:mainfrom
ssimpson89:fix/oauth2-token-checksum-migration-order

Conversation

@ssimpson89

Copy link
Copy Markdown
Contributor
SUMMARY

Upgrading from a pre-25.6 release to 25.6.x fails during the migration Job when the database has any existing OAuth2 access tokens:

Applying oauth2_provider.0012_add_token_checksum...
ValueError: The following fields do not exist in this model, are m2m fields, primary keys, or are non-concrete fields: token_checksum

The access token model is swapped to main.OAuth2AccessToken, so the backfill in oauth2_provider.0012_add_token_checksum loads our model and calls save(update_fields=['token_checksum']) on every existing token. The token_checksum column on our model is added by main.0198_oauth2accesstoken_token_checksum_and_more, but nothing in the graph orders 0198 ahead of 0012. Django can schedule 0012 first, and when it does, the field is not in the historical model state yet. Fresh installs pass because the loop has no rows to save, so this only shows up on upgrades (or restores followed by migrate) with existing tokens.

The fix adds run_before = [('oauth2_provider', '0012_add_token_checksum')] to main.0198, the same approach main.0025 already uses for oauth2_provider.0001_initial. A test asserts that main.0198 is in the forwards plan of oauth2_provider.0012.

Design notes:

  • No dependency cycle. The swappable dependency in 0012 resolves to main.__first__, and no main migration up to 0197 depends on an oauth2_provider migration past 0005.
  • Safe for existing databases. On any install that reached 25.6.x both migrations are applied, and on older installs neither is, so the new edge cannot trigger InconsistentMigrationHistory. A failed upgrade like the one above leaves 0012 unapplied, so it also stays consistent.
  • django-oauth-toolkit 3.4.1 skips this backfill for swapped models, but the published ghcr.io/ctrliq/ascender:25.6.3 image ships django-oauth-toolkit 3.3.0, which still runs the save. The ordering fix makes the migration correct regardless of which toolkit version is installed.

Testing:

  • Reproduced on k3s: installed 25.4.0, created two personal access tokens, upgraded to 25.6.3. ascender-app-migration-25.6.3 failed at oauth2_provider.0012 with the error above.
  • Restored the same 25.4.0 database and ran migrate in the 25.6.3 image with the patched 0198. main.0198 applied before oauth2_provider.0012, both succeeded, no unapplied migrations remained, and both tokens had token_checksum populated.

Workaround for affected installs until this ships: ascender-manage migrate main 0198 --noinput, then ascender-manage migrate --noinput.

ISSUE TYPE
  • Bug, Docs Fix or other nominal change
COMPONENT NAME
  • API
ASCENDER VERSION
25.6.3-23-gf8240942

…fill

Signed-off-by: Stephen Simpson <ssimpson89@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 5, 2026 21:35

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.

🟡 Changes recommended

The migration-ordering test fails under the API suite’s --nomigrations setting because the loader cannot find oauth2_provider.0012_add_token_checksum, causing NodeNotFoundError instead of validating ordering.

1 open issue
What changed in this PR

This PR fixes OAuth2 token upgrade failures by ensuring Ascender adds the token checksum field before the OAuth provider migration attempts to backfill it.

Changes:

  • Adds an explicit migration ordering constraint to main.0198.
  • Adds a regression test asserting that ordering in Django’s migration graph.
File Description
ascender/​main/​tests/​functional/​test_migrations.py Adds a migration-order regression test.
ascender/​main/​migrations/​0198_oauth2accesstoken_token_checksum_and_more.py Orders checksum field creation before the OAuth provider backfill.

Review effort: Balanced

…ed without connection

Signed-off-by: Stephen Simpson <ssimpson89@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 5, 2026 21:56

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.

🟢 Approval recommended

No active review issues are recorded for the migration-ordering fix.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@cigamit

cigamit commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator

Duplicate of #1151 ?

@ssimpson89

Copy link
Copy Markdown
Contributor Author

@cigamit yep, looks like it! I'll close this

@ssimpson89 ssimpson89 closed this Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants