Fix malformed ID detection to actually work (bd-54)

SQLite's CAST to INTEGER never returns NULL - it returns 0 for
invalid strings. This meant the malformed ID detection query was
completely broken and never found any malformed IDs.

The Problem:
- Query used: CAST(suffix AS INTEGER) IS NULL
- SQLite behavior: CAST('abc' AS INTEGER) = 0 (not NULL!)
- Result: Malformed IDs were never detected

The Fix:
- Check if CAST returns 0 AND suffix doesn't start with '0'
- This catches non-numeric suffixes like 'abc', 'foo123'
- Avoids false positives on legitimate IDs like 'test-0', 'test-007'

Changes:
- internal/storage/sqlite/sqlite.go:126-131
  * Updated malformed ID query logic
  * Check: CAST = 0 AND first char != '0'
  * Added third parameter for prefix (used 3 times now)

Testing:
- Created test DB with test-abc, test-1, test-foo123
- Warning correctly shows: [test-abc test-foo123] ✓
- Added test-0, test-007 (zero-prefixed IDs)
- No false positives ✓
- All existing tests pass ✓

Impact:
- Malformed IDs are now properly detected and warned about
- Helps maintain data quality
- Prevents confusion when auto-incrementing IDs

Closes bd-54

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Steve Yegge
2025-10-14 00:32:42 -07:00
parent 92759710de
commit 3aeeeb752c

View File

@@ -121,13 +121,15 @@ func getNextID(db *sql.DB) int {
}
// Check for malformed IDs (non-numeric suffixes) and warn
// These are silently ignored by CAST but indicate data quality issues
// SQLite's CAST returns 0 for invalid integers, never NULL
// So we detect malformed IDs by checking if CAST returns 0 AND suffix doesn't start with '0'
malformedQuery := `
SELECT id FROM issues
WHERE id LIKE ? || '-%'
AND CAST(SUBSTR(id, LENGTH(?) + 2) AS INTEGER) IS NULL
AND CAST(SUBSTR(id, LENGTH(?) + 2) AS INTEGER) = 0
AND SUBSTR(id, LENGTH(?) + 2, 1) != '0'
`
rows, err := db.Query(malformedQuery, prefix, prefix)
rows, err := db.Query(malformedQuery, prefix, prefix, prefix)
if err == nil {
defer rows.Close()
var malformedIDs []string