{"record":{"id":"5797474cd41ef171","repo":"gastownhall/beads","slug":"schema-read-database-name-w","errorCode":null,"errorMessage":"schema: read database name: %w","messagePattern":"schema: read database name: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/storage/dolt/store.go","lineNumber":2601,"sourceCode":"}\n\n// initSchemaOnDBWithBootstrapHeal threads one-shot, incarnation-bound reset\n// authority into the migration lock. A nil capability always fails closed.\nfunc initSchemaOnDBWithBootstrapHeal(\n\tctx context.Context,\n\tdb *sql.DB,\n\tbootstrapHeal *schema.FreshBootstrapHealCapability,\n\tendpoint string,\n) (int, error) {\n\tconn, err := db.Conn(ctx)\n\tif err != nil {\n\t\treturn 0, fmt.Errorf(\"schema: pin connection: %w\", err)\n\t}\n\tdefer conn.Close()\n\n\tvar dbName string\n\tif err := conn.QueryRowContext(ctx, \"SELECT DATABASE()\").Scan(&dbName); err != nil {\n\t\treturn 0, fmt.Errorf(\"schema: read database name: %w\", err)\n\t}\n\n\tvar opts []schema.MigrateLockOption\n\tif bootstrapHeal != nil {\n\t\topts = append(opts, schema.WithFreshBootstrapHeal(bootstrapHeal, endpoint))\n\t}\n\tapplied, err := schema.MigrateUpWithLock(ctx, conn, dbName, opts...)\n\tif err != nil {\n\t\treturn applied, fmt.Errorf(\"schema migration: %w\", err)\n\t}\n\treturn applied, nil\n}\n\nfunc initSchemaOnDBWithRetry(ctx context.Context, db *sql.DB) (int, error) {\n\treturn initSchemaOnDBWithRetryAndGate(ctx, db, nil)\n}\n\n// initSchemaOnDBWithRetryAndGate is initSchemaOnDBWithRetry with an optional","sourceCodeStart":2583,"sourceCodeEnd":2619,"githubUrl":"https://github.com/gastownhall/beads/blob/71377f276968b452ee607177637970a4ff888584/internal/storage/dolt/store.go#L2583-L2619","documentation":"After pinning a connection, the schema initializer runs SELECT DATABASE() to learn which database the connection is scoped to; this error wraps a failure to scan that result. It means the pinned connection exists but the trivial query failed or returned no row (NULL database, e.g. no default schema selected).","triggerScenarios":"initSchemaOnDBWithRetryAndGate executing conn.QueryRowContext(ctx, \"SELECT DATABASE()\").Scan(&dbName) when: the query errors (connection dropped, server gone, ctx canceled), or Scan fails because DATABASE() is NULL (connection without a database selected in the DSN).","commonSituations":"DSN missing the database name (e.g. 'user:pass@tcp(host:port)/' with empty dbname), Dolt server restarted between pin and query, context deadline exceeded, credentials revoked mid-session.","solutions":["Include the database name in the DSN so DATABASE() is non-NULL","Inspect the wrapped error: nil-row Scan (sql.ErrNoRows) means no default DB selected; driver errors mean check server health","Retry after reconnecting; a stale pinned connection is re-pinned on the next call","Verify ctx deadline is generous enough for pool pin + query"],"exampleFix":"// before\ndsn := \"user:pass@tcp(localhost:3306)/\"\n// after\ndsn := \"user:pass@tcp(localhost:3306)/mydb\"","handlingStrategy":"validation","validationCode":"// ensure DSN names a database so SELECT DATABASE() is non-NULL\ncfg, err := mysql.ParseDSN(connStr)\nif err != nil || cfg.DBName == \"\" {\n    return fmt.Errorf(\"DSN must include a database name\")\n}","typeGuard":null,"tryCatchPattern":"// Go: distinguish no-default-DB from driver failure\nif err != nil {\n    if errors.Is(err, sql.ErrNoRows) {\n        // DATABASE() NULL: DSN missing dbname\n    }\n    return fmt.Errorf(\"schema: read database name: %w\", err)\n}","preventionTips":["Always include the database name in the DSN","Keep migration contexts timeout-bounded but generous","Re-create pools after server restarts instead of reusing stale ones"],"tags":["go","database","mysql","schema-migration"],"backgroundTag":"database-name-resolution-failed","analyzedSha":"71377f276968b452ee607177637970a4ff888584","analyzedAt":"2026-08-30T18:55:39.744Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}