Split migration to avoid issues with pending triggers#899
Conversation
Rather than make the original migration non-atomic, we split the. migration into three steps for the safest production path.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 34f7f2d713
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
Pull request overview
This PR restructures the api app’s v7-era Django migrations to reduce the risk of PostgreSQL migrations stalling on “pending trigger events” by separating data updates from subsequent schema/constraint changes (so the data updates can commit before adding a uniqueness constraint).
Changes:
- Moved API key data updates (blanking legacy tokens + backfilling UUID identifiers) into
0005asRunPythonsteps. - Moved schema changes (API key identifier uniqueness,
UserSessionmodel creation,last_used_atfield) into0006. - Relocated the
api_user_project_read_accessview optimization SQL from0005to0006to keep0005data-only.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
ghostwriter/api/migrations/0004_apikey_opaque_tokens_and_user_sessions.py |
Now only introduces the APIKey schema fields; removed data steps and UserSession creation. |
ghostwriter/api/migrations/0005_optimize_user_project_read_access_view.py |
Converted to a data-only migration to blank legacy tokens and populate missing APIKey identifiers before constraints are added. |
ghostwriter/api/migrations/0006_apikey_last_used_at.py |
Applies identifier uniqueness, creates UserSession, adds last_used_at, and applies the optimized project read-access view. |
Comments suppressed due to low confidence (1)
ghostwriter/api/migrations/0004_apikey_opaque_tokens_and_user_sessions.py:10
settingsand the swappable user dependency are no longer used in this migration after movingUserSessioncreation to 0006. Keeping them adds dead imports/dependencies and makes the migration harder to reason about.
# Django Imports
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("api", "0003_service_token_project_access_views"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
🤖 Augment PR SummarySummary: This PR splits the API v7 migration steps to avoid PostgreSQL migrations stalling on pending triggers by separating data updates from the later uniqueness constraint. Changes:
Technical Notes: The intent is that 🤖 Was this summary useful? React with 👍 or 👎 |
In case someone has applied 004 and it failed in the narrow window it was available before this hotfix, this adjusts it so the migrations will run instead of being marked as already migrated.
While it should be impossible for something else to write to the db during a migration, it doesn't hurt to enforce that.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #899 +/- ##
==========================================
- Coverage 93.63% 93.61% -0.03%
==========================================
Files 404 404
Lines 27638 27679 +41
==========================================
+ Hits 25880 25912 +32
- Misses 1758 1767 +9 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
The API migration for v7 can get stuck waiting for pending triggers on the PostgreSQL side. Rather than mark the migration as non-atomic, this splits the migration into multiple steps. This is the safest path that handles the potential issue without marking the transaction as non-atomic.