Skip to content

Split migration to avoid issues with pending triggers#899

Merged
chrismaddalena merged 7 commits into
masterfrom
hotfix/api-migration-split
Jun 4, 2026
Merged

Split migration to avoid issues with pending triggers#899
chrismaddalena merged 7 commits into
masterfrom
hotfix/api-migration-split

Conversation

@chrismaddalena
Copy link
Copy Markdown
Collaborator

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.

Rather than make the original migration non-atomic, we split the. migration into three steps for the safest production path.
Copilot AI review requested due to automatic review settings June 3, 2026 23:33
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 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".

Comment thread ghostwriter/api/migrations/0006_apikey_last_used_at.py Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 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 0005 as RunPython steps.
  • Moved schema changes (API key identifier uniqueness, UserSession model creation, last_used_at field) into 0006.
  • Relocated the api_user_project_read_access view optimization SQL from 0005 to 0006 to keep 0005 data-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

  • settings and the swappable user dependency are no longer used in this migration after moving UserSession creation 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),
    ]

@augmentcode
Copy link
Copy Markdown

augmentcode Bot commented Jun 3, 2026

🤖 Augment PR Summary

Summary: 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:

  • Removes the API key data backfill and session-table creation steps from 0004, leaving only the initial APIKey schema additions (identifier/secret hash/token prefix) and token field tweak.
  • Replaces 0005’s previous view optimization with data-only operations that (a) blank legacy API key tokens and (b) backfill missing API key identifiers.
  • Adds a new 0006 migration that enforces APIKey.identifier uniqueness/default, creates the UserSession model, and adds APIKey.last_used_at.
  • Moves the api_user_project_read_access view optimization SQL from 0005 into 0006, with a reverse SQL definition retained.

Technical Notes: The intent is that 0005 commits the row updates before 0006 adds the unique constraint, reducing the chance of PostgreSQL lock/trigger-related migration hangs.

🤖 Was this summary useful? React with 👍 or 👎

Copy link
Copy Markdown

@augmentcode augmentcode Bot left a comment

Choose a reason for hiding this comment

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

Review completed. 2 suggestions posted.

Fix All in Augment

Comment augment review to trigger a new review at any time.

Comment thread ghostwriter/api/migrations/0006_apikey_last_used_at.py Outdated
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
Copy link
Copy Markdown

codecov Bot commented Jun 4, 2026

Codecov Report

❌ Patch coverage is 79.24528% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.61%. Comparing base (ea2ecdc) to head (a306208).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...ons/0005_optimize_user_project_read_access_view.py 68.42% 6 Missing ⚠️
...twriter/api/migrations/0006_apikey_last_used_at.py 85.29% 5 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Comment thread ghostwriter/api/migrations/0006_apikey_last_used_at.py
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

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

Comment thread ghostwriter/api/migrations/0006_apikey_last_used_at.py
Comment thread ghostwriter/api/migrations/0005_optimize_user_project_read_access_view.py Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Comment thread ghostwriter/api/migrations/0006_apikey_last_used_at.py
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

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

@chrismaddalena chrismaddalena merged commit a1e0be5 into master Jun 4, 2026
10 of 12 checks passed
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.

2 participants