{"record":{"id":"fbe4ef6b53f512e5","repo":"gastownhall/beads","slug":"checking-dependencies-for-an-existing-primary-key","errorCode":null,"errorMessage":"checking dependencies for an existing primary key: %w","messagePattern":"checking dependencies for an existing primary key: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/storage/schema/migration_repairs.go","lineNumber":540,"sourceCode":"\t\t// NULL-id rows behind it.\n\t\treturn fmt.Errorf(\"migration 0053: %d dependencies row(s) have no depends_on_issue_id/depends_on_wisp_id/depends_on_external target and cannot be assigned an id (ck_dep_one_target should prevent this); repair manually before retrying\", remainingNull)\n\t}\n\n\tidIsPrimaryKey, err := schemaColumnInPrimaryKey(ctx, db, \"dependencies\", \"id\")\n\tif err != nil {\n\t\treturn fmt.Errorf(\"checking dependencies.id primary key: %w\", err)\n\t}\n\tif idIsPrimaryKey {\n\t\treturn nil\n\t}\n\n\tif _, err := db.ExecContext(ctx, \"ALTER TABLE dependencies MODIFY COLUMN id CHAR(36) NOT NULL\"); err != nil {\n\t\treturn fmt.Errorf(\"finalizing dependencies.id for migration 0053: %w\", err)\n\t}\n\n\thasAnyPrimaryKey, err := schemaHasPrimaryKey(ctx, db, \"dependencies\")\n\tif err != nil {\n\t\treturn fmt.Errorf(\"checking dependencies for an existing primary key: %w\", err)\n\t}\n\tif hasAnyPrimaryKey {\n\t\t// The #4690 drifted shape has dependencies keyed some other way (or\n\t\t// keyless): a table can carry only one PRIMARY KEY, so whatever is\n\t\t// there must go before id can become it. The uk_dep_* natural-identity\n\t\t// unique keys (0043) enforce the real uniqueness independently of\n\t\t// whatever this was, so dropping it is safe.\n\t\tif _, err := db.ExecContext(ctx, \"ALTER TABLE dependencies DROP PRIMARY KEY\"); err != nil {\n\t\t\treturn fmt.Errorf(\"dropping dependencies' existing primary key for migration 0053: %w\", err)\n\t\t}\n\t}\n\tif _, err := db.ExecContext(ctx, \"ALTER TABLE dependencies ADD PRIMARY KEY (id)\"); err != nil {\n\t\treturn fmt.Errorf(\"keying dependencies.id for migration 0053: %w\", err)\n\t}\n\treturn nil\n}\n\n// firstNonNullString returns the first valid (non-NULL) value among cols, or","sourceCodeStart":522,"sourceCodeEnd":558,"githubUrl":"https://github.com/gastownhall/beads/blob/71377f276968b452ee607177637970a4ff888584/internal/storage/schema/migration_repairs.go#L522-L558","documentation":"Wraps a failure of `schemaHasPrimaryKey(ctx, db, \"dependencies\")` — introspection asking whether the dependencies table has any PRIMARY KEY at all. Like error 3973, this is a schema-metadata probe failing, before the repair decides whether to DROP the existing key.","triggerScenarios":"ensureDependenciesIDPrimaryKey runs right after MODIFY COLUMN; the information_schema query behind schemaHasPrimaryKey errors out (server down, permissions, transient connection failure, corrupt metadata).","commonSituations":"Same environments as 3973: Dolt daemon crash during multi-step repair, restricted DB user, flaky network to a remote Dolt server.","solutions":["Rerun `bd`; the probe is idempotent and read-only","Fix the wrapped %w root cause (connectivity, credentials, server health)","Verify access to information_schema.TABLE_CONSTRAINTS for the database","If crashes recur mid-repair, run the migration against a local copy first to confirm it completes"],"exampleFix":"// before: remote server unreachable mid-repair\nbd dolt server start ; bd ready\n// after: completes and restores primary key\nbd ready","handlingStrategy":"retry","validationCode":"bd dolt sql -q \"SELECT COUNT(*) FROM information_schema.TABLE_CONSTRAINTS WHERE TABLE_NAME='dependencies' AND CONSTRAINT_TYPE='PRIMARY KEY'\" || echo 'introspection unavailable'","typeGuard":null,"tryCatchPattern":"if err := ensureSchema(ctx, db); err != nil {\n    if strings.Contains(err.Error(), \"checking dependencies for an existing primary key\") && transient(err) {\n        return retryWithBackoff(ensureSchema)\n    }\n    return err\n}","preventionTips":["Ensure stable connectivity to remote Dolt servers during upgrades","Grant metadata-read privileges to the bd account","Run migrations unattended-interrupt-free","Test the upgrade on a database copy first"],"tags":["database","migration","introspection","sql"],"backgroundTag":"schema-introspection-failed","analyzedSha":"71377f276968b452ee607177637970a4ff888584","analyzedAt":"2026-08-30T18:55:39.744Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}