fix: store NULL instead of 'undefined' string when last_message_time is missing - #17
fix: store NULL instead of 'undefined' string when last_message_time is missing#17ErnestHysa wants to merge 1 commit into
Conversation
📝 WalkthroughWalkthroughThe PR fixes a single-line bug in ChangesFix last_message_time null handling
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~5 minutes Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
ESLint skipped: no ESLint configuration detected in root package.json. To enable, add Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/database.ts (1)
98-108:⚠️ Potential issue | 🟠 Major | ⚡ Quick winMissing one-time cleanup keeps existing corrupted timestamps in place
This change fixes new writes, but existing rows with
'undefined'/'null'/''are not repaired. With Line 98 usingCOALESCE, laternullwrites also won’t overwrite those bad values, so sort order can remain broken.💡 Suggested fix
export function initializeDatabase(): DatabaseSync { const db = getDb(); db.exec("PRAGMA journal_mode = WAL"); @@ db.exec( `CREATE INDEX IF NOT EXISTS idx_chats_last_message_time ON chats (last_message_time);`, ); + + // One-time normalization for legacy bad values + db.exec(` + UPDATE chats + SET last_message_time = NULL + WHERE last_message_time IN ('undefined', 'null', ''); + `); return db; }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/database.ts` around lines 98 - 108, The DB currently keeps bad string timestamps like 'undefined'/'null'/'', and because your INSERT ... ON CONFLICT uses COALESCE(last_message_time, ...), subsequent null writes don't overwrite those bad strings; run a one-time cleanup migration before the upsert to normalize corrupted rows (e.g., an UPDATE on the table referenced by stmt.run to set last_message_time = NULL WHERE last_message_time IN ('undefined','null','') OR TRIM(last_message_time) = '' or otherwise invalid), and also simplify the value mapping in stmt.run for chat.last_message_time (use Date -> toISOString(), null -> null, else null) so new writes are correct. Ensure the migration executes prior to using stmt.run/INSERT with COALESCE so the bad values are replaced by NULLs.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@src/database.ts`:
- Around line 98-108: The DB currently keeps bad string timestamps like
'undefined'/'null'/'', and because your INSERT ... ON CONFLICT uses
COALESCE(last_message_time, ...), subsequent null writes don't overwrite those
bad strings; run a one-time cleanup migration before the upsert to normalize
corrupted rows (e.g., an UPDATE on the table referenced by stmt.run to set
last_message_time = NULL WHERE last_message_time IN ('undefined','null','') OR
TRIM(last_message_time) = '' or otherwise invalid), and also simplify the value
mapping in stmt.run for chat.last_message_time (use Date -> toISOString(), null
-> null, else null) so new writes are correct. Ensure the migration executes
prior to using stmt.run/INSERT with COALESCE so the bad values are replaced by
NULLs.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: a19dc036-cc44-45ae-bc7a-be3b0400ff40
📒 Files selected for processing (1)
src/database.ts
|
Thanks CodeRabbit — addressed the review notes. The fix is intentionally scoped to the single line in storeChat that was stringifying the fallback; left the one-time cleanup SQL out of this PR since it's a separate concern from the code fix itself. |
Fixes #14
Summary by CodeRabbit