Skip to content

fix(storage): match cache keys exactly on every database, like GitHub does - #286

Merged
LouisHaftmann merged 2 commits into
devfrom
fix/exact-cache-key-matching
Oct 23, 2026
Merged

LouisHaftmann merged 2 commits into
devfrom
fix/exact-cache-key-matching

Conversation

@LouisHaftmann

Copy link
Copy Markdown
Collaborator

📝 Summary

On MySQL, a Cache Entry for foo could be restored by, or overwritten by, a save of Foo, fóo or foo . The default collation compares keys and scopes case-, accent- and trailing-space-insensitively. SQLite's like also ignored ASCII case for restore-key prefix matches. Postgres compared exactly, so the same workflow behaved differently depending on the database.

A test run against GitHub's own cache service confirmed it matches exactly. Restoring casefoo after saving CaseFoo misses for both the exact key and the restore-key prefix. Restoring Cafe after saving Café misses too. Saving casefoo while CaseFoo exists creates a second, separate entry. This PR makes every driver behave that way:

 cache_entries / uploads (MySQL only, migration $8)
-  key   varchar(512)                           -- utf8mb4_0900_ai_ci
-  scope varchar(255)                           -- utf8mb4_0900_ai_ci
+  key   varchar(512) collate utf8mb4_0900_bin  -- NO PAD, exact
+  scope varchar(255) collate utf8mb4_0900_bin

 SQLite connection
+  pragma case_sensitive_like = on

scope is included because it had the same collation and branch refs are case-sensitive. This PR also renames CONTEXT.md to GLOSSARY.md to match the domain-modeling naming.

✅ Verification

tests/exact-key-matching.test.ts mirrors the GitHub run:

save CaseFoo-<id>, Café-<id>, Pad-<id>
match casefoo-<id>            -> miss
match nomatch, prefix casefoo -> miss
match Cafe-<id>               -> miss
match "Pad-<id> "             -> miss
save casefoo-<id>             -> separate entry, CaseFoo-<id> keeps its own storage location
  • Before: fails on VITEST_DB_DRIVER=sqlite and mysql (expected { match: … } to be undefined)
    After: passes on sqlite, postgres and mysql
  • pnpm test:matrix: all 12 DB × storage combinations pass
  • pnpm type-check, pnpm lint, pnpm build: pass

🔍 Reviews

Review Found Fixed
bug-review 1 1
ponytail-review 0 0
code-review 5 3

Unfixed findings:

  • Standards: sharing one helper between the migration's up and down was skipped, since every other migration mirrors them inline.
  • Standards: a comment on why completeUpload looks up uploads by reservationHash is out of scope.
  • Spec: case_sensitive_like is deprecated in SQLite but still supported by better-sqlite3; the new test catches it on the sqlite run if that ever changes.

🔗 Issues

Closes #284

💥 Merge danger

Door: two-way

The migration's down restores the default collation.

Blast Radius: MySQL

MySQL deployments rebuild cache_entries and uploads on startup (ALTER TABLE … MODIFY), which can take a while on large tables. After the upgrade, a workflow that only hit because of case, accent or trailing-space differences now misses, matching GitHub. utf8mb4_0900_bin requires MySQL 8.0.17+; MariaDB lacks it.

🤖 Generated with Claude Code

LouisHaftmann and others added 2 commits October 23, 2026 13:13
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…river

GitHub's cache service compares keys exactly. MySQL's default collation
compared `key` and `scope` case- and accent-insensitively, and SQLite's
`like` ignored ASCII case, so `foo` could restore or overwrite `Foo`.

Closes #284

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@LouisHaftmann
LouisHaftmann merged commit 0fd30a8 into dev Oct 23, 2026
30 checks passed
@LouisHaftmann
LouisHaftmann deleted the fix/exact-cache-key-matching branch October 23, 2026 12:00
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.

fix(storage): cache keys that differ only in case collide on MySQL

1 participant