{"record":{"id":"155bd9dbad1c1546","repo":"gastownhall/beads","slug":"embeddeddolt-switching-to-database-w","errorCode":null,"errorMessage":"embeddeddolt: switching to database: %w","messagePattern":"embeddeddolt: switching to database: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/storage/embeddeddolt/store.go","lineNumber":334,"sourceCode":"\tconn, err := db.Conn(ctx)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"embeddeddolt: pin connection: %w\", err)\n\t}\n\tdefer conn.Close()\n\n\tif s.database != \"\" {\n\t\tif !validIdentifier.MatchString(s.database) {\n\t\t\tmsg := fmt.Sprintf(\"embeddeddolt: invalid database name: %q\", s.database)\n\t\t\tif strings.ContainsRune(s.database, '-') {\n\t\t\t\tmsg += \"; hyphens are not allowed in embedded mode — replace with underscores in .beads/metadata.json dolt_database field, or run 'bd doctor'\"\n\t\t\t}\n\t\t\treturn errors.New(msg)\n\t\t}\n\t\tif _, err := conn.ExecContext(ctx, \"CREATE DATABASE IF NOT EXISTS `\"+s.database+\"`\"); err != nil {\n\t\t\treturn fmt.Errorf(\"embeddeddolt: creating database: %w\", err)\n\t\t}\n\t\tif _, err := conn.ExecContext(ctx, \"USE `\"+s.database+\"`\"); err != nil {\n\t\t\treturn fmt.Errorf(\"embeddeddolt: switching to database: %w\", err)\n\t\t}\n\t\tif s.branch != \"\" {\n\t\t\tif _, err := conn.ExecContext(ctx, fmt.Sprintf(\"SET @@%s_head_ref = %s\", s.database, sqlStringLiteral(s.branch))); err != nil {\n\t\t\t\treturn fmt.Errorf(\"embeddeddolt: setting branch: %w\", err)\n\t\t\t}\n\t\t}\n\t}\n\n\t// Forward-drift guard: if this database's schema is AHEAD of the binary,\n\t// fail fast with a clear \"upgrade bd\" message before MigrateUp no-ops and a\n\t// later query dies on a dropped/renamed column. Embedded mode is the mode\n\t// the stale-binary incident (#4135/#4137) was observed in. The read-only\n\t// embedded open (OpenReadOnly) already guards this; the writable open did\n\t// not. Runs after the USE switch so the version read resolves against the\n\t// target database.\n\tif err := schema.CheckForwardDrift(ctx, conn); err != nil {\n\t\treturn err\n\t}","sourceCodeStart":316,"sourceCodeEnd":352,"githubUrl":"https://github.com/gastownhall/beads/blob/71377f276968b452ee607177637970a4ff888584/internal/storage/embeddeddolt/store.go#L316-L352","documentation":"Wraps the SQL error from 'USE `<database>`' after the database was created in initSchema. The database exists (CREATE succeeded) but switching the connection's default database failed — typically a lost connection, context cancellation, or an engine-level error resolving the just-created database. The rest of schema setup needs the default database selected, so it aborts.","triggerScenarios":"newStore -> initSchema with s.database != \"\" where the USE statement fails: (1) ctx cancelled/timed out between CREATE DATABASE and USE; (2) engine dropped the pinned connection after the CREATE; (3) metadata inconsistency where the engine cannot resolve the database it just created.","commonSituations":"Slow or flaky environments (containers, network filesystems) where engine operations intermittently fail; very long init exceeding the caller's context deadline; partial state from an earlier crashed init.","solutions":["Retry the command; if it recurs, run 'bd doctor' to check database state.","Inspect the wrapped inner error for the exact engine message.","Raise the context timeout for initialization on large/slow setups.","As a last resort, re-initialize the data directory and pull from the Dolt remote."],"exampleFix":"// before\nstore, err := newStore(shortCtx, cfg) // embeddeddolt: switching to database: context deadline exceeded\n\n// after\nstore, err := newStore(context.Background(), cfg)","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := store.Init(ctx); err != nil && strings.Contains(err.Error(), \"switching to database\") {\n    select {\n    case <-ctx.Done():\n        // deadline: retry with longer timeout\n    default:\n        // transient engine error: retry once\n    }\n    return store.Init(initCtxLong)\n}","preventionTips":["Use generous timeouts for embedded store initialization","Don't cancel the context between store open steps","Re-run 'bd doctor' after any crash during initialization","Keep the engine and bd versions matched"],"tags":["embedded-dolt","sql","database-switch"],"backgroundTag":"sql-execution-failed","analyzedSha":"71377f276968b452ee607177637970a4ff888584","analyzedAt":"2026-08-30T18:55:39.744Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}