fix(BA-6914): give the three drifting FKs one name on both install paths#12910
Draft
jopemachine wants to merge 2 commits into
Draft
fix(BA-6914): give the three drifting FKs one name on both install paths#12910jopemachine wants to merge 2 commits into
jopemachine wants to merge 2 commits into
Conversation
Three migrations create a foreign key under a hardcoded name while the models left it unnamed, so the metadata naming convention generated a different one. The key is created and enforced either way -- only the name differed, depending on whether the database came from `mgr schema oneshot` or from replaying migrations. That is not cosmetic: each of those downgrades drops the constraint by the hardcoded name and aborted on a fresh install. Converge on the hardcoded names rather than the convention ones. The migrations' downgrades already expect them and work today on migrated databases, so this direction needs no edit to released migrations, and it sidesteps the convention name for prometheus_query_presets, which exceeds PostgreSQL's 63-character limit and comes back truncated with a hash suffix. Naming a constraint explicitly is not novel here -- see fk_deployment_policies_endpoint. The rename looks the current name up rather than hardcoding it, so it does not depend on SQLAlchemy's truncation algorithm and is a no-op where the name already matches. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
📚 Stacked PRs
BA-6913fix: indexes that only exist on migrated databasesBA-6914fix: one FK constraint name on both install paths ← you are hereBA-6915fix: drop the column default before retyping sessions.resultMerge in order, bottom-up. Each PR is based on the one above it, so its diff shows only its own layer.
Problem
Three migrations create a foreign key under a hardcoded name; the models left it unnamed and let the metadata naming convention generate one. Same key, same enforcement — different name depending on how the DB was built:
cd067180a8b1fk_kernels_image_idfk_kernels_image_id_imagesc7f2a8e31b04fk_login_sessions_login_client_type_idfk_login_sessions_login_client_type_id_login_client_typesa3b4c5d6e7f8fk_prometheus_query_presets_category_idfk_prometheus_query_presets_category_id_prometheus_quer_8a51¹¹ the convention name exceeds PostgreSQL's 63-char identifier limit, so SQLAlchemy truncates it and appends a hash.
Not cosmetic: each downgrade drops the constraint by the hardcoded name and aborts with
UndefinedObjectErroron a fresh install. Whether a downgrade works depends on where the DB came from.Which name is correct?
The convention (
fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s) is the house style — 81 of 82 model FKs are unnamed. By that count the hardcoded three are the deviation, which argues for renaming migrated DBs to the convention names.This PR goes the other way, for three reasons:
..._8a51, tying us to SQLAlchemy's truncation algorithm.fk_deployment_policies_endpointindeployment_policy/row.py.So: name the three explicitly on the models, and rename on the databases built from the convention. Nothing outside their own migration files references these names.
This is the reviewable decision in this PR — if you'd rather converge on the convention, the cost is editing those three downgrades plus hardcoding the truncated name.
Fix
name=on the three model FKs, matching what the migrations create and their downgrades drop.c8d1e5b73f92renames the constraint where it differs. It looks the current name up via the inspector rather than hardcoding the convention name — that keeps it independent of SQLAlchemy's truncation and makes it a no-op where the name already matches. Downgrade is a no-op: undoing it would only put the drift back, and the convention name is not reconstructible.Verification
Real databases throughout; the dev DB was only ever read.
Fresh install with the fixed models — all three come out with the short names.
Existing fresh install (schema built from
main's models, so convention names), afterupgrade head:kernelsfk_kernels_image_id_imagesfk_kernels_image_idlogin_sessionsfk_login_sessions_login_client_type_id_login_client_typesfk_login_sessions_login_client_type_idprometheus_query_presetsfk_prometheus_query_presets_category_id_prometheus_quer_8a51fk_prometheus_query_presets_category_idThe three walls, on a fresh install (stamp the revision, run only its downgrade) — all now pass:
a3b4c5d6e7f8constraint "fk_prometheus_query_presets_category_id" does not existcd067180a8b1constraint "fk_kernels_image_id" ... does not existc7f2a8e31b04constraint "fk_login_sessions_login_client_type_id" ... does not existFound while surveying the downgrade chain for #12881.
🤖 Generated with Claude Code