{"record":{"id":"6710545c485dcc3f","repo":"charmbracelet/crush","slug":"failed-to-connect-to-database-w-671054","errorCode":null,"errorMessage":"failed to connect to database: %w","messagePattern":"failed to connect to database: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cmd/session.go","lineNumber":124,"sourceCode":"\nfunc sessionSetup(cmd *cobra.Command) (context.Context, *sessionServices, func(), error) {\n\tdataDir, _ := cmd.Flags().GetString(\"data-dir\")\n\tctx := cmd.Context()\n\n\tcfg, err := config.Init(\"\", dataDir, false)\n\tif err != nil {\n\t\treturn nil, nil, nil, fmt.Errorf(\"failed to initialize config: %w\", err)\n\t}\n\tif dataDir == \"\" {\n\t\tdataDir = cfg.Config().Options.DataDirectory\n\t}\n\tif shouldEnableMetrics(cfg.Config()) {\n\t\tevent.Init()\n\t}\n\n\tconn, err := db.Connect(ctx, dataDir)\n\tif err != nil {\n\t\treturn nil, nil, nil, fmt.Errorf(\"failed to connect to database: %w\", err)\n\t}\n\n\tqueries := db.New(conn)\n\tsvc := &sessionServices{\n\t\tsessions: session.NewService(queries, conn),\n\t\tmessages: message.NewService(queries),\n\t\tcfg:      cfg,\n\t}\n\treturn ctx, svc, func() { conn.Close() }, nil\n}\n\nfunc runSessionList(cmd *cobra.Command, _ []string) error {\n\tevent.SetNonInteractive(true)\n\n\tctx, svc, cleanup, err := sessionSetup(cmd)\n\tif err != nil {\n\t\treturn err\n\t}","sourceCodeStart":106,"sourceCodeEnd":142,"githubUrl":"https://github.com/charmbracelet/crush/blob/7944b8e52225d8805e31eacbf7ef24856b0dfb7a/internal/cmd/session.go#L106-L142","documentation":"After config init, sessionSetup calls db.Connect to open (or create) the SQLite database in the data directory. Any connection failure is wrapped as this error and aborts the session subcommand. This is the DB availability gate for all session commands.","triggerScenarios":"db.Connect(ctx, dataDir) errors: data directory does not exist or is not writable, database file corrupted/locked, or the SQLite driver fails to initialize (CGO_ENABLED=0 modernc driver issue).","commonSituations":"Running with a --data-dir flag pointing at a non-existent or read-only path; another crush process holds a stale lock; disk full; database file corrupted after a crash.","solutions":["Verify the data directory exists and is writable (ls -ld, touch a test file)","Check for stale SQLite lock/journal files (-wal, -shm) and that no other crush process is running","If the DB is corrupted, back up and remove/recreate the database file in the data dir","Check free disk space on the volume holding the data directory"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// ensure data dir is usable before connecting\nif info, err := os.Stat(dataDir); err != nil || !info.IsDir() {\n    if err := os.MkdirAll(dataDir, 0o755); err != nil {\n        return fmt.Errorf(\"cannot create data dir: %w\", err)\n    }\n}\nprobe := filepath.Join(dataDir, \".write-probe\")\nif err := os.WriteFile(probe, nil, 0o644); err != nil {\n    return fmt.Errorf(\"data dir not writable: %w\", err)\n}\nos.Remove(probe)","typeGuard":null,"tryCatchPattern":"conn, err := db.Connect(ctx, dataDir)\nif err != nil {\n    slog.Error(\"Cannot open database\", \"dataDir\", dataDir, \"error\", err)\n    os.Exit(1)\n}\ndefer conn.Close()","preventionTips":["Verify the data dir exists and is writable before running session commands","Check disk space on the volume holding the database","Avoid running multiple crush instances concurrently on the same data dir","Back up the database before force-killing processes that write to it"],"tags":["go","sqlite","database","cli"],"backgroundTag":"database-connection-failed","analyzedSha":"7944b8e52225d8805e31eacbf7ef24856b0dfb7a","analyzedAt":"2026-08-29T12:48:59.079Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}