{"record":{"id":"fe8abe7d4b3fbd7e","repo":"charmbracelet/crush","slug":"failed-to-gather-stats-w","errorCode":null,"errorMessage":"failed to gather stats: %w","messagePattern":"failed to gather stats: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cmd/stats.go","lineNumber":180,"sourceCode":"\t\t}\n\t\tif dataDir == \"\" {\n\t\t\tdataDir = cfg.Config().Options.DataDirectory\n\t\t}\n\t\tif shouldEnableMetrics(cfg.Config()) {\n\t\t\tevent.Init()\n\t\t}\n\n\t\tevent.StatsViewed()\n\n\t\tconn, err := db.Connect(ctx, dataDir)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"failed to connect to database: %w\", err)\n\t\t}\n\t\tdefer conn.Close()\n\n\t\tstats, err := gatherStats(ctx, conn)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"failed to gather stats: %w\", err)\n\t\t}\n\n\t\tprojectStats = []ProjectStats{{ProjectPath: \"\", Stats: stats}}\n\t}\n\n\tif len(projectStats) == 0 {\n\t\treturn fmt.Errorf(\"no data available: no projects found\")\n\t}\n\n\t// Merge stats from all projects.\n\tmergedStats := mergeStats(projectStats)\n\n\tif mergedStats.Total.TotalSessions == 0 {\n\t\treturn fmt.Errorf(\"no data available: no sessions found in database\")\n\t}\n\n\tcurrentUser, err := user.Current()\n\tif err != nil {","sourceCodeStart":162,"sourceCodeEnd":198,"githubUrl":"https://github.com/charmbracelet/crush/blob/7944b8e52225d8805e31eacbf7ef24856b0dfb7a/internal/cmd/stats.go#L162-L198","documentation":"runStats wraps errors from gatherStats, which runs SQL aggregate queries against the connected database to compute token/cost/session statistics. This error means the stats queries themselves failed — typically a SQL error, a scan/row-conversion error, or a context cancellation mid-query — not that the connection is bad.","triggerScenarios":"`crush stats` with a database whose schema predates current migrations (missing columns/tables queried by gatherStats), a corrupted SQLite file returning I/O or database-disk-image errors mid-query, a cancelled context (Ctrl-C) during long aggregate queries, or NULL/type-mismatched rows that fail sql.Scan into int64/float64 fields.","commonSituations":"Upgrading crush and running stats against an old database if migrations partially failed; a crush.db damaged by a prior WAL desync (SQLITE_NOTADB/11 disk I/O error); very large databases where the query outlives a cancelled context; hand-edited or third-party-modified databases with unexpected row types.","solutions":["Read the wrapped cause in the error output and run `sqlite3 <dataDir>/crush.db 'PRAGMA integrity_check;'` to detect corruption.","Ensure crush has run once normally so migrations applied (`crush` interactively) before running stats against the same data dir.","Restore crush.db from backup if integrity check fails; alternatively back up and delete the db so a fresh one is created.","Retry with an uninterrupted context (don't Ctrl-C during aggregation) on very large databases.","Update crush to the latest version if the schema and query versions were mismatched."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"out, err := exec.Command(\"sqlite3\", filepath.Join(dataDir, \"crush.db\"), \"PRAGMA integrity_check;\").Output()\nif err != nil || !strings.Contains(string(out), \"ok\") {\n\treturn fmt.Errorf(\"database integrity check failed\")\n}","typeGuard":null,"tryCatchPattern":"stats, err := gatherStats(ctx, conn)\nif err != nil {\n\tif errors.Is(err, context.Canceled) {\n\t\treturn fmt.Errorf(\"stats query cancelled\")\n\t}\n\treturn fmt.Errorf(\"failed to gather stats: %w\", err)\n}","preventionTips":["Let migrations complete before running stats; update crush rather than downgrading against a newer schema.","Never edit session rows manually; use the CLI.","Avoid Ctrl-C mid-aggregation on large databases.","Run PRAGMA integrity_check after copying databases between machines."],"tags":["database","sqlite","query","go"],"backgroundTag":"sqlite-query-failed","analyzedSha":"7944b8e52225d8805e31eacbf7ef24856b0dfb7a","analyzedAt":"2026-08-29T12:48:59.079Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}