fix: Handle NULL values in GetStatistics for empty databases (#37)

When GetStatistics is called on an empty database, SQL SUM() returns NULL
which causes a scan error when converting to int. Wrap SUM expressions with
COALESCE to return 0 instead of NULL.

Add TestGetStatistics to verify both empty and populated database cases.

Fixes issue where `bd stats` and MCP stats tool crash on fresh databases.

Signed-off-by: Joshua Shanks <jjshanks@gmail.com>
This commit is contained in:
Joshua Shanks
2025-10-15 00:30:36 -07:00
committed by GitHub
parent b8f0b5e71f
commit 3f42cab0d1
2 changed files with 73 additions and 3 deletions

View File

@@ -107,9 +107,9 @@ func (s *SQLiteStorage) GetStatistics(ctx context.Context) (*types.Statistics, e
err := s.db.QueryRowContext(ctx, `
SELECT
COUNT(*) as total,
SUM(CASE WHEN status = 'open' THEN 1 ELSE 0 END) as open,
SUM(CASE WHEN status = 'in_progress' THEN 1 ELSE 0 END) as in_progress,
SUM(CASE WHEN status = 'closed' THEN 1 ELSE 0 END) as closed
COALESCE(SUM(CASE WHEN status = 'open' THEN 1 ELSE 0 END), 0) as open,
COALESCE(SUM(CASE WHEN status = 'in_progress' THEN 1 ELSE 0 END), 0) as in_progress,
COALESCE(SUM(CASE WHEN status = 'closed' THEN 1 ELSE 0 END), 0) as closed
FROM issues
`).Scan(&stats.TotalIssues, &stats.OpenIssues, &stats.InProgressIssues, &stats.ClosedIssues)
if err != nil {