fix(#1591): Fixes schema connection for RunPython migration steps#1678
fix(#1591): Fixes schema connection for RunPython migration steps#1678igor-petrik-invitae wants to merge 3 commits into
RunPython migration steps#1678Conversation
RunPython migration steps.
RunPython migration steps.RunPython migration steps
There was a problem hiding this comment.
Pull request overview
This PR addresses Django’s guidance for RunPython migrations by ensuring data migrations use the schema_editor connection (rather than implicitly using the default DB connection), which helps prevent multi-DB connection/locking issues like the hang reported in #1591.
Changes:
- Update
RunPythonmigration functions to query/save usingschema_editor.connection.alias. - Add a new tox environment to run migrations against a non-default database configuration.
- Add contributor to
AUTHORS.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
tox.ini |
Adds migrate_non_default tox env to exercise migrations on a non-default DB alias. |
tests/non_default_db_settings.py |
Introduces settings intended to validate migration behavior when using --database=secondary. |
oauth2_provider/migrations/0012_add_token_checksum.py |
Uses schema_editor DB alias for iteration and saves in RunPython. |
oauth2_provider/migrations/0006_alter_application_client_secret.py |
Uses schema_editor DB alias for queryset and saves in RunPython. |
AUTHORS |
Adds a new author entry. |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
@igor-petrik-invitae Thanks for the PR! I was giving it a review. When I check out the first commit, the migration is failing on 0006_.... Are we sure this is truly replicating the issue for migration 0012_... and not exposing a a different, but valid bug, in migrations handling of multiple databases? |
|
I want to point out that this is not multiple databases; the point of the test is to test different where the connection used for migrations is a different connection to the same database. You are right, that 0006 does not fail on my Postgres database, where 0012 does. I was not sure how to write a test in this repository that would use a postgres database, but if you take the same config as here and substitute a postgres database instead of a sqlite database, that should reproduce the original deadlock issue. |
|
If you're at all interested in setting up the tests with postgres we need to cover local and CI environmnts. in CI you can add postgres to the workflow with the services key. I set it up on another project I work on a while back wagtail-grapple. I would love to have testing on postgres.. there are quite a few issues that never surface when testing soley with sqlite. |
|
@igor-petrik-invitae I've added postgres primary/replica testing to our test environments. that should provide a better baseline for testing this feature. Do you have time to update this PR? |
5d65116 to
3d99b87
Compare
9ca1a53 to
c0596f8
Compare
Fixes #1591
Description of the Change
Adds use of
schema_editorconnection forRunPythonmigrations, as recommended by Django.Checklist
documentation updated(N/A?)(only for user relevant changes) (N/A?)CHANGELOG.mdupdatedAUTHORStests/app/idp updated to demonstrate new features(N/A?)tests/app/rp updated to demonstrate new features(N/A?)