beads/internal/storage/sqlite/comments.go
beads/crew/emma 774cc5bbd0 feat: add comment_count to issue JSON views (list, ready, search)
Add GetCommentCounts() batch query to all storage backends (SQLite, Dolt,
Memory) and wire it into bd list, bd ready, bd search JSON output and RPC
server responses. Uses single GROUP BY query to avoid N+1.

Based on PR #1354 by bjornslib - cherry-picked the Go backend changes,
fixed bugs, and skipped problematic JSONL/lockfile/MCP changes.

Co-Authored-By: bjornslib <bjornslib@users.noreply.github.com>

Executed-By: beads/crew/emma
Rig: beads
Role: crew
2026-01-30 22:51:03 -08:00

228 lines
7.1 KiB
Go

package sqlite
import (
"context"
"fmt"
"time"
"github.com/steveyegge/beads/internal/types"
)
// AddIssueComment adds a comment to an issue
func (s *SQLiteStorage) AddIssueComment(ctx context.Context, issueID, author, text string) (*types.Comment, error) {
// Verify issue exists
var exists bool
err := s.db.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM issues WHERE id = ?)`, issueID).Scan(&exists)
if err != nil {
return nil, fmt.Errorf("failed to check issue existence: %w", err)
}
if !exists {
return nil, fmt.Errorf("issue %s not found", issueID)
}
// Insert comment
result, err := s.db.ExecContext(ctx, `
INSERT INTO comments (issue_id, author, text, created_at)
VALUES (?, ?, ?, CURRENT_TIMESTAMP)
`, issueID, author, text)
if err != nil {
return nil, fmt.Errorf("failed to insert comment: %w", err)
}
// Get the inserted comment ID
commentID, err := result.LastInsertId()
if err != nil {
return nil, fmt.Errorf("failed to get comment ID: %w", err)
}
// Fetch the complete comment
comment := &types.Comment{}
err = s.db.QueryRowContext(ctx, `
SELECT id, issue_id, author, text, created_at
FROM comments WHERE id = ?
`, commentID).Scan(&comment.ID, &comment.IssueID, &comment.Author, &comment.Text, &comment.CreatedAt)
if err != nil {
return nil, fmt.Errorf("failed to fetch comment: %w", err)
}
// Mark issue as dirty for JSONL export
if err := s.MarkIssueDirty(ctx, issueID); err != nil {
return nil, fmt.Errorf("failed to mark issue dirty: %w", err)
}
return comment, nil
}
// ImportIssueComment adds a comment during import, preserving the original timestamp.
// Unlike AddIssueComment which uses CURRENT_TIMESTAMP, this method uses the provided
// createdAt time from the JSONL file. This prevents timestamp drift during sync cycles.
// GH#735: Comment created_at timestamps were being overwritten with current time during import.
func (s *SQLiteStorage) ImportIssueComment(ctx context.Context, issueID, author, text string, createdAt time.Time) (*types.Comment, error) {
// Verify issue exists
var exists bool
err := s.db.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM issues WHERE id = ?)`, issueID).Scan(&exists)
if err != nil {
return nil, fmt.Errorf("failed to check issue existence: %w", err)
}
if !exists {
return nil, fmt.Errorf("issue %s not found", issueID)
}
// Insert comment with provided timestamp
createdAtStr := createdAt.UTC().Format(time.RFC3339Nano)
result, err := s.db.ExecContext(ctx, `
INSERT INTO comments (issue_id, author, text, created_at)
VALUES (?, ?, ?, ?)
`, issueID, author, text, createdAtStr)
if err != nil {
return nil, fmt.Errorf("failed to insert comment: %w", err)
}
// Get the inserted comment ID
commentID, err := result.LastInsertId()
if err != nil {
return nil, fmt.Errorf("failed to get comment ID: %w", err)
}
// Fetch the complete comment
comment := &types.Comment{}
err = s.db.QueryRowContext(ctx, `
SELECT id, issue_id, author, text, created_at
FROM comments WHERE id = ?
`, commentID).Scan(&comment.ID, &comment.IssueID, &comment.Author, &comment.Text, &comment.CreatedAt)
if err != nil {
return nil, fmt.Errorf("failed to fetch comment: %w", err)
}
// Mark issue as dirty for JSONL export
if err := s.MarkIssueDirty(ctx, issueID); err != nil {
return nil, fmt.Errorf("failed to mark issue dirty: %w", err)
}
return comment, nil
}
// GetIssueComments retrieves all comments for an issue
func (s *SQLiteStorage) GetIssueComments(ctx context.Context, issueID string) ([]*types.Comment, error) {
// Hold read lock during database operations to prevent reconnect() from
// closing the connection mid-query (GH#607 race condition fix)
s.reconnectMu.RLock()
defer s.reconnectMu.RUnlock()
rows, err := s.db.QueryContext(ctx, `
SELECT id, issue_id, author, text, created_at
FROM comments
WHERE issue_id = ?
ORDER BY created_at ASC
`, issueID)
if err != nil {
return nil, fmt.Errorf("failed to query comments: %w", err)
}
defer func() { _ = rows.Close() }()
var comments []*types.Comment
for rows.Next() {
comment := &types.Comment{}
err := rows.Scan(&comment.ID, &comment.IssueID, &comment.Author, &comment.Text, &comment.CreatedAt)
if err != nil {
return nil, fmt.Errorf("failed to scan comment: %w", err)
}
comments = append(comments, comment)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("error iterating comments: %w", err)
}
return comments, nil
}
// GetCommentsForIssues fetches comments for multiple issues in a single query
// Returns a map of issue_id -> []*Comment
func (s *SQLiteStorage) GetCommentsForIssues(ctx context.Context, issueIDs []string) (map[string][]*types.Comment, error) {
if len(issueIDs) == 0 {
return make(map[string][]*types.Comment), nil
}
// Hold read lock during database operations to prevent reconnect() from
// closing the connection mid-query (GH#607 race condition fix)
s.reconnectMu.RLock()
defer s.reconnectMu.RUnlock()
// Build placeholders for IN clause
placeholders := make([]interface{}, len(issueIDs))
for i, id := range issueIDs {
placeholders[i] = id
}
query := fmt.Sprintf(`
SELECT id, issue_id, author, text, created_at
FROM comments
WHERE issue_id IN (%s)
ORDER BY issue_id, created_at ASC
`, buildPlaceholders(len(issueIDs))) // #nosec G201 -- placeholders are generated internally
rows, err := s.db.QueryContext(ctx, query, placeholders...)
if err != nil {
return nil, fmt.Errorf("failed to batch get comments: %w", err)
}
defer func() { _ = rows.Close() }()
result := make(map[string][]*types.Comment)
for rows.Next() {
comment := &types.Comment{}
err := rows.Scan(&comment.ID, &comment.IssueID, &comment.Author, &comment.Text, &comment.CreatedAt)
if err != nil {
return nil, fmt.Errorf("failed to scan comment: %w", err)
}
result[comment.IssueID] = append(result[comment.IssueID], comment)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("error iterating comments: %w", err)
}
return result, nil
}
// GetCommentCounts returns the number of comments for each issue in a single batch query.
// Returns a map of issue_id -> comment count. Issues with no comments have count 0.
func (s *SQLiteStorage) GetCommentCounts(ctx context.Context, issueIDs []string) (map[string]int, error) {
if len(issueIDs) == 0 {
return make(map[string]int), nil
}
placeholders := make([]interface{}, len(issueIDs))
for i, id := range issueIDs {
placeholders[i] = id
}
query := fmt.Sprintf(`
SELECT issue_id, COUNT(*) as comment_count
FROM comments
WHERE issue_id IN (%s)
GROUP BY issue_id
`, buildPlaceholders(len(issueIDs))) // #nosec G201 -- placeholders are generated internally
rows, err := s.db.QueryContext(ctx, query, placeholders...)
if err != nil {
return nil, fmt.Errorf("failed to get comment counts: %w", err)
}
defer func() { _ = rows.Close() }()
result := make(map[string]int)
for rows.Next() {
var issueID string
var count int
if err := rows.Scan(&issueID, &count); err != nil {
return nil, fmt.Errorf("failed to scan comment count: %w", err)
}
result[issueID] = count
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("error iterating comment counts: %w", err)
}
return result, nil
}