benbjohnson/litestream · error

count rows: %w

Error message

count rows: %w

What it means

This error wraps the failure of a `SELECT COUNT(*)` query against a table whose name was interpolated directly into SQL by the shrink test tool. It means the underlying SQLite driver could not execute the count query — usually a syntax error (bad table name) or the table does not exist. It is thrown by `ShrinkCommand.deleteFromTable` so the caller (`shrinkDatabase`) can abort before attempting row deletion.

Source

Thrown at cmd/litestream-test/shrink.go:175

	defer rows.Close()

	var tables []string
	for rows.Next() {
		var table string
		if err := rows.Scan(&table); err != nil {
			return nil, err
		}
		tables = append(tables, table)
	}

	return tables, nil
}

func (c *ShrinkCommand) deleteFromTable(db *sql.DB, table string) (int64, error) {
	var totalRows int
	countQuery := fmt.Sprintf("SELECT COUNT(*) FROM %s", table)
	if err := db.QueryRow(countQuery).Scan(&totalRows); err != nil {
		return 0, fmt.Errorf("count rows: %w", err)
	}

	if totalRows == 0 {
		return 0, nil
	}

	rowsToDelete := int(float64(totalRows) * (c.DeletePercentage / 100))
	if rowsToDelete == 0 {
		return 0, nil
	}

	var hasID bool
	columnQuery := fmt.Sprintf("PRAGMA table_info(%s)", table)
	rows, err := db.Query(columnQuery)
	if err != nil {
		return 0, fmt.Errorf("get table info: %w", err)
	}
	defer rows.Close()

View on GitHub (pinned to 4ed7a308f6)

Solutions

  1. Verify the table name exists: `sqlite3 <db> '.tables'` and pass the exact name
  2. Run the shrink with an unlocked database (close other litestream/SQLite connections)
  3. Check the error wrapped by %w (`sqlite: no such table: ...`) for the actual SQL failure cause
  4. Re-create or restore the database if corruption is reported

Example fix

// before
countQuery := fmt.Sprintf("SELECT COUNT(*) FROM %s", table)
// after
// verify table exists first, e.g. via PRAGMA table_list, and use a quoted identifier
countQuery := fmt.Sprintf("SELECT COUNT(*) FROM %q", table)
Defensive patterns

Strategy: validation

Validate before calling

var exists int
err := db.QueryRow("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name=?", table).Scan(&exists)
if err != nil || exists == 0 { return fmt.Errorf("table %q not found", table) }

Prevention

When it happens

Trigger: Calling `litestream-test shrink` with a table name that does not exist in the source database; table name containing characters that break SQL interpolation; database file locked or unreadable by another process; database corrupted so the count scan fails.

Common situations: Testing shrink on a database where the target table was dropped or renamed; passing a quoted or schema-qualified table name (e.g. `main.t`) that the naive `fmt.Sprintf("SELECT COUNT(*) FROM %s")` mishandles; the DB file path points to an empty/nonexistent file where SQLite creates an empty DB with no tables.

Understand the failure class

Background: Database query failed: Internal Server Error 500s wrapping SQL, Prisma, and connection failures — what to check first — this error's family across 16 libraries.

Related errors


AI-assisted analysis of benbjohnson/litestream@4ed7a308f6 (2026-09-06). Data as JSON: /api/errors/0873455a7aa2c310. Report an issue: GitHub.