Skip to content

apple: lookup indexes and a text-only search trigger (schema migration) - #175

Merged
morajabi merged 1 commit into
mainfrom
claude/lookup-indexes-migration
Nov 28, 2026
Merged

morajabi merged 1 commit into
mainfrom
claude/lookup-indexes-migration

Conversation

@morajabi

Copy link
Copy Markdown
Contributor

Schema change: needs your OK before merging. This appends one GRDB migration. Earlier migrations are untouched. It runs in the migrator at open, once: about 250 ms on a 120k-message file on an M5 Mac, so expect 0.5–1 s on phones at first launch. If that's too much at launch, it can move to a background maintenance step.

What the migration does

  • Adds four lookup indexes:
    • dialog(chatId): the chat list joins dialogs by chat, so SQLite built an automatic index on every chat list read.
    • chat(parentChatId, parentMessageId) WHERE parentChatId IS NOT NULL: reply-thread lookups by parent message, used by message bundles and the chat list. They scanned chat.
    • chat(peerUserId) WHERE peerUserId IS NOT NULL: DM chats by peer.
    • member(spaceId): space member lists.
  • Drops two duplicated indexes: attachment_messageId_idx and file_profileForUserId_idx. Each has a partial … IS NOT NULL twin on the same column. The twin serves every equality, IN and join lookup, including the foreign-key scans on message and user deletes. Every insert maintained both B-trees.
  • Narrows the FTS sync trigger: it now fires only on updates that set text or globalId, with GRDB's trigger body unchanged. Targeted updates (status, revision, resource flags, history pages) no longer delete and re-insert the message's text in the search index. Full-record saves set every column and still re-index, as before.

Measured

M5 Mac, 120k-message SQLCipher file:

Before After
Status/revision update of one message 0.077 ms 0.020 ms
Text edit 0.078 ms 0.074 ms
Chat list plan 1 automatic index none
Reply threads by parent message SCAN chat SEARCH chat_parent_idx
Chat list, 300 chats 2.1 ms 2.1 ms
Migration, once — ~250 ms (first open 328–351 ms incl. key derivation)

Tests

New LookupIndexesMigrationTests:

  • The four indexes exist, the two dropped ones are gone, and the trigger is AFTER UPDATE OF "text", "globalId". The migration runs after the read-cursor migration.
  • A database migrated to the previous migration, with messages, upgrades with search still in sync:
    • a status/revision update doesn't run the FTS trigger (checked in the statement trace);
    • a text edit re-indexes, and a delete removes the row;
    • integrity-check with rank 1 (index against the table) passes.
  • Query plans:
    • Reply threads use chat_parent_idx and dialogs-by-chat use dialog_chatId_idx; the chat list SQL has no automatic index. These checks fail without the migration.
    • Attachment and profile-photo lookups (=, IN, LEFT JOIN) use the partial twins.
  • DatabaseMigrationOrderTests pass.

Review

An independent review checked:

  • the partial twins, including foreign-key child scans and cascades;
  • that globalId is the rowid and the trigger body matches GRDB 7.12;
  • UPSERT and REPLACE paths;
  • upgrade at every earlier version, and downgrade.

On downgrade, an older release build ignores the unknown migration and keeps working. It doesn't recreate the dropped indexes or the old trigger. An older Debug build erases the database, as with any new migration.

Its test fixes are in: pinned upgrade base, rank-1 integrity check, and plan checks for the dropped indexes. Its notes on what still re-indexes and on a future FTS rebuild are in the migration's comment.

No protocol change.

🤖 Generated with Claude Code

@morajabi
morajabi force-pushed the claude/lookup-indexes-migration branch from a5a200d to de91d18 Compare November 27, 2026 20:31
@morajabi
morajabi marked this pull request as ready for review November 27, 2026 20:31
@morajabi morajabi changed the title apple: lookup indexes and a text-only search trigger (schema migration, needs sign-off) apple: lookup indexes and a text-only search trigger (schema migration) Nov 27, 2026
@morajabi
morajabi force-pushed the claude/lookup-indexes-migration branch from de91d18 to 3a99c2c Compare November 28, 2026 14:53
…duplicated indexes, re-index message search only on text changes (schema migration)
@morajabi
morajabi force-pushed the claude/lookup-indexes-migration branch from 3a99c2c to 2487c6c Compare November 28, 2026 15:41
@morajabi
morajabi merged commit b51bc1b into main Nov 28, 2026
29 of 33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant