Fix email agent database schema bug#35
Open
mshuffett wants to merge 3 commits intoanthropics:mainfrom
Open
Conversation
The database schema creation used camelCase column names (toAddresses, ccAddresses, bccAddresses) but all SQL queries use snake_case (to_addresses, cc_addresses, bcc_addresses). When Bun's SQLite creates tables, it converts camelCase to snake_case automatically for some columns but not others, causing a mismatch. This resulted in 'table emails has no column named to_addresses' errors when searching emails. Changes: - Updated schema definition to use snake_case consistently for to_addresses, cc_addresses, bcc_addresses columns - Updated FTS5 table and triggers to use matching snake_case column names Fixes search functionality that was failing with column not found errors.
- Added imapUid column to emails table schema - Updated INSERT/UPDATE queries to include imap_uid - Fixes compatibility with email-db.ts which expects this column - Both database classes now use compatible schemas
- Added imap_uid, to_addresses, cc_addresses, bcc_addresses columns - email-db.ts initializes first, so it needs these columns - Both database classes now create compatible schemas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The email agent has multiple database schema mismatches causing search and sync failures:
table emails has no column named to_addressestable emails has no column named imap_uidRoot Cause
The codebase has two database initialization classes that were creating incompatible schemas:
database/email-db.ts- Initializes first (via EmailSyncService on server.ts:71)imap_uid,to_addresses,cc_addresses,bcc_addressescolumnsdatabase/database-manager.ts- SQL queries reference missing columnsto_addresses,cc_addresses,bcc_addressesin INSERT/UPDATE (lines 265, 288-290)imap_uidcolumnSince both use
CREATE TABLE IF NOT EXISTS emails, whichever runs first determines the schema. The second class's schema is ignored, causing mismatches between the actual database structure and the SQL queries.Code References
SQL queries using snake_case (database-manager.ts):
https://github.com/anthropics/claude-agent-sdk-demos/blob/0131cde/email-agent/database/database-manager.ts#L265
https://github.com/anthropics/claude-agent-sdk-demos/blob/0131cde/email-agent/database/database-manager.ts#L288-L290
SQL queries using imap_uid (email-db.ts):
https://github.com/anthropics/claude-agent-sdk-demos/blob/0131cde/email-agent/database/email-db.ts#L224
Solution
Updated BOTH database initialization files to use identical, compatible schemas:
email-db.ts:
imap_uid INTEGERcolumnto_addresses TEXTcolumncc_addresses TEXTcolumnbcc_addresses TEXTcolumndatabase-manager.ts:
toAddresses→to_addressesccAddresses→cc_addressesbccAddresses→bcc_addressesimapUid INTEGERcolumnimap_uidBoth files now create identical schemas regardless of initialization order.
Testing
Impact
This affects all users of the email-agent demo. Without this fix:
\ud83e\udd16 Generated with Claude Code
Co-Authored-By: Claude noreply@anthropic.com