fix(migrations): work around GinIndex+OpClass codegen regression in Django 5.x#926
Open
cmorinupgrade wants to merge 2 commits into
Open
Conversation
…jango 5.x GinIndex(OpClass(Upper(field), name=opclass)) generates invalid SQL under Django 5.x: the operator class ends up inside the expression parens instead of after them, causing a PostgreSQL syntax error on CREATE INDEX. Migrations 0056 and 0080 both hit this pattern. Fix both by switching to SeparateDatabaseAndState: database_operations runs the correct SQL via RiskyRunSQL (preserving the risky-skip behaviour), state_operations uses AddIndex to keep Django model state in sync.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit e7a8b64. Configure here.
Branch.name has db_column="branch", so the index must use UPPER("branch")
not UPPER("name"). Caught by Cursor Bugbot on PR review.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Problem
GinIndex(OpClass(Upper(field), name="gin_trgm_ops"))generates invalid SQL under Django 5.x. The operator class ends up inside the expression parens instead of after them:PostgreSQL rejects this with
syntax error at or near "gin_trgm_ops", causing the worker to crash-loop on startup when it tries to run migrations.Two migrations are affected:
core/0056_branch_name_trgm_idxcore/0080_repository_repos_name_trgm_idxBoth were generated against Django 4.2 and worked fine then, but fail on Django 5.x.
Fix
Replace
RiskyAddIndexwithSeparateDatabaseAndState:database_operations:RiskyRunSQLwith the correct hardcoded SQL (preserves the risky-skip behaviour viaSKIP_RISKY_MIGRATION_STEPS)state_operations: plainAddIndexto keep Django's model state in syncThe SQL used is exactly what the migration docstrings already document as the intended DDL.
IF NOT EXISTSmakes it safe to re-run.Testing
Verified against PostgreSQL 14 with Django 5.x — migrations apply cleanly.
Note
Medium Risk
Touches production database migrations by replacing ORM-generated index DDL with raw
RunSQL, so mistakes could break migration application or leave schema/state mismatched, though the change is limited to two index creations and usesIF NOT EXISTS/reversible SQL.Overview
Works around a Django 5.x SQL generation regression for
GinIndex(OpClass(...))by changing two trigram index migrations to useSeparateDatabaseAndState.The DB step now runs explicit
CREATE INDEX IF NOT EXISTS ... gin_trgm_opsviaRiskyRunSQL(preservingSKIP_RISKY_MIGRATION_STEPSbehavior), while the migration state still records the correspondingAddIndexso Django’s model state remains consistent.Reviewed by Cursor Bugbot for commit cc7d9f1. Bugbot is set up for automated code reviews on this repo. Configure here.