{"record":{"id":"b796f384db731e7f","repo":"gastownhall/beads","slug":"failed-to-query-existing-issues-for-prefix-q-w","errorCode":null,"errorMessage":"failed to query existing issues for prefix %q: %w","messagePattern":"failed to query existing issues for prefix %q: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/storage/dolt/issues.go","lineNumber":808,"sourceCode":"// It is idempotent: if a counter row already exists for this prefix, it does nothing.\nfunc seedCounterFromExistingIssuesTx(ctx context.Context, tx *sql.Tx, prefix string) error {\n\t// Check whether a counter row already exists for this prefix.\n\t// If it does, we must not overwrite it (the counter may already be in use).\n\tvar existing int\n\terr := tx.QueryRowContext(ctx, \"SELECT last_id FROM issue_counter WHERE prefix = ?\", prefix).Scan(&existing)\n\tif err == nil {\n\t\t// Row exists - counter is already initialized, nothing to do.\n\t\treturn nil\n\t}\n\tif err != sql.ErrNoRows {\n\t\treturn fmt.Errorf(\"failed to check issue_counter for prefix %q: %w\", prefix, err)\n\t}\n\n\t// No counter row yet. Scan existing issues to find the highest numeric suffix.\n\tlikePattern := prefix + \"-%\"\n\trows, err := tx.QueryContext(ctx, \"SELECT id FROM issues WHERE id LIKE ?\", likePattern)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to query existing issues for prefix %q: %w\", prefix, err)\n\t}\n\tdefer rows.Close()\n\n\tmaxNum := 0\n\tprefixDash := prefix + \"-\"\n\tfor rows.Next() {\n\t\tvar id string\n\t\tif err := rows.Scan(&id); err != nil {\n\t\t\treturn fmt.Errorf(\"failed to scan issue id: %w\", err)\n\t\t}\n\t\t// Strip the prefix and attempt to parse the remainder as an integer.\n\t\tsuffix := strings.TrimPrefix(id, prefixDash)\n\t\tif suffix == id {\n\t\t\t// id did not start with prefix- (should not happen given LIKE, but be safe)\n\t\t\tcontinue\n\t\t}\n\t\tvar num int\n\t\tif _, parseErr := fmt.Sscanf(suffix, \"%d\", &num); parseErr == nil && fmt.Sprintf(\"%d\", num) == suffix {","sourceCodeStart":790,"sourceCodeEnd":826,"githubUrl":"https://github.com/gastownhall/beads/blob/71377f276968b452ee607177637970a4ff888584/internal/storage/dolt/issues.go#L790-L826","documentation":"When no issue_counter row exists for a prefix, the seeder scans existing issues with a LIKE query to find the highest numeric suffix; failure of that scan query is wrapped with this message. The counter cannot be safely seeded without this scan, so the operation aborts.","triggerScenarios":"First ID generation for a prefix with no counter row, where SELECT id FROM issues WHERE id LIKE 'prefix-%' fails — SQL error, transaction failure, or backend unavailable.","commonSituations":"Databases created before the counter table existed (legacy data being adopted); transient connection loss during first create after migration; very large issue tables timing out.","solutions":["Read the wrapped cause and fix the underlying SQL/connection error","Retry the operation; seeding is read-only up to the failure point","Verify the issues table exists and is queryable (bd list)","Ensure schema migrations ran so both issues and issue_counter are present"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// Ensure the issues table exists and is readable before creating issues\nconst count = await query(\"SELECT COUNT(*) FROM issues\");","typeGuard":null,"tryCatchPattern":"try {\n  await bd.create(title);\n} catch (e) {\n  if (String(e.message).includes(\"failed to query existing issues\")) {\n    // check connectivity/migrations, then retry\n  } else throw e;\n}","preventionTips":["Run migrations before first use of a database adopted from older versions","Maintain stable connectivity to the Dolt server during first creates","Verify the issues table with bd list after any import/migration","Retry seeding; it is safe to re-run"],"tags":["database","counter","migration","query"],"backgroundTag":"counter-query-failed","analyzedSha":"71377f276968b452ee607177637970a4ff888584","analyzedAt":"2026-08-30T18:55:39.744Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}