gastownhall/beads · error

db: CommentSQLRepository.InsertRecord: %w

Error message

db: CommentSQLRepository.InsertRecord: %w

What it means

When the comment has no pre-assigned ID, InsertRecord delegates to issueops.InsertDerivedComment, which generates an ID and performs the INSERT. Any error from that helper (ID-generation conflict, insert failure, table missing, connection error) is wrapped verbatim with this prefix. The call site adds no extra context, so the underlying driver error text is the real diagnostic.

Source

Thrown at internal/storage/domain/db/comment.go:158

	if err := r.runner.QueryRowContext(ctx,
		fmt.Sprintf("SELECT EXISTS(SELECT 1 FROM %s WHERE id = ?)", issueTable), copy.IssueID).Scan(&exists); err != nil {
		return nil, fmt.Errorf("db: CommentSQLRepository.InsertRecord: check issue existence: %w", err)
	}
	if !exists {
		return nil, fmt.Errorf("db: CommentSQLRepository.InsertRecord: issue %s not found", copy.IssueID)
	}

	if copy.CreatedAt.IsZero() {
		copy.CreatedAt = time.Now().UTC()
	} else {
		copy.CreatedAt = copy.CreatedAt.UTC()
	}
	createdAtText := issueops.FormatAuxTime(copy.CreatedAt)
	commentTable := pickCommentTable(opts.UseWispsTable)
	if copy.ID == "" {
		id, _, err := issueops.InsertDerivedComment(ctx, r.runner, commentTable, copy.IssueID, copy.Author, copy.Text, createdAtText)
		if err != nil {
			return nil, fmt.Errorf("db: CommentSQLRepository.InsertRecord: %w", err)
		}
		copy.ID = id
	} else {
		//nolint:gosec // G201: commentTable is one of two hardcoded constants
		if _, err := r.runner.ExecContext(ctx, fmt.Sprintf(`
			INSERT INTO %s (id, issue_id, author, text, created_at)
			VALUES (?, ?, ?, ?, ?)
		`, commentTable), copy.ID, copy.IssueID, copy.Author, copy.Text, createdAtText); err != nil {
			return nil, fmt.Errorf("db: CommentSQLRepository.InsertRecord: %w", err)
		}
	}
	createdAt, err := issueops.ParseAuxTime(createdAtText)
	if err != nil {
		return nil, fmt.Errorf("db: CommentSQLRepository.InsertRecord: %w", err)
	}
	copy.CreatedAt = createdAt

	if err := issueops.RecordCommentEventInTx(ctx, r.runner, copy.IssueID, &issueops.EventComment{

View on GitHub (pinned to 71377f2769)

Solutions

  1. Read the wrapped driver error after the prefix to identify the concrete failure.
  2. Retry on transient lock/connection errors; investigate persistent uniqueness collisions.
  3. Run schema migration so pickCommentTable(opts.UseWispsTable) resolves to an existing table.
  4. Check for concurrent writers hammering the same database.

Example fix

// before
// insert fails with ambiguous wrapped error; cause unknown
repo.InsertRecord(ctx, c, opts)
// after
if _, err := repo.InsertRecord(ctx, c, opts); err != nil {
    log.Errorf("insert comment: %v", errors.Unwrap(err)) // inspect real cause
}
Defensive patterns

Strategy: retry

Validate before calling

// pre-check schema so derived inserts hit an existing table
var name string
_ = runner.QueryRowContext(ctx,
    "SELECT table_name FROM information_schema.tables WHERE table_name IN ('comments','comments_wisps')").Scan(&name)

Type guard

null

Try / catch

if _, err := repo.InsertRecord(ctx, c, opts); err != nil {
    if strings.Contains(err.Error(), "db: CommentSQLRepository.InsertRecord:") {
        cause := errors.Unwrap(err)
        if isTransient(cause) { // lock/connection
            return retryWithBackoff(ctx, func() error { _, err := repo.InsertRecord(ctx, c, opts); return err })
        }
        return fmt.Errorf("derived comment insert failed: %w", cause)
    }
}

Prevention

When it happens

Trigger: issueops.InsertDerivedComment fails: ID collision/uniqueness constraint on the comment table, issues_wisps/issues comment table missing, locked database, context cancellation, or disk/connection failure during INSERT.

Common situations: Concurrent inserts racing on derived IDs, corrupted or locked Dolt/SQLite file, importing into a database with an outdated schema lacking the comment table.

Related errors


AI-assisted analysis of gastownhall/beads@71377f2769 (2026-08-30). Data as JSON: /api/errors/392125c7c07be0e9. Report an issue: GitHub.