Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/storage/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2173,8 +2173,8 @@ export class MysqlBackend implements StorageBackend {
args.push(namespace);
}
if (keyPrefix !== undefined && keyPrefix !== "") {
where.push("kv_key LIKE CONCAT(?, '%')");
args.push(keyPrefix.replace(/[\\%_]/g, (ch) => "\\" + ch));
where.push("kv_key LIKE CONCAT(?, '%') AND LOCATE(CAST(? AS BINARY), CAST(kv_key AS BINARY)) = 1");
args.push(keyPrefix.replace(/[\\%_]/g, (ch) => "\\" + ch), keyPrefix);
}
const sql =
"SELECT namespace, kv_key, value_json, updated_at FROM kv_store" +
Expand Down
4 changes: 2 additions & 2 deletions src/storage/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2567,8 +2567,8 @@ export class UnifiedStore {
}
if (keyPrefix !== undefined && keyPrefix !== "") {
validateExtKey(keyPrefix, "key prefix");
where.push("key LIKE ? ESCAPE '\\'");
args.push(likeEscape(keyPrefix) + "%");
where.push("key LIKE ? ESCAPE '\\' AND instr(key, ?) = 1");
args.push(likeEscape(keyPrefix) + "%", keyPrefix);
}
const sql =
"SELECT namespace, key, value_json, updated_at FROM kv_store" +
Expand Down
25 changes: 25 additions & 0 deletions tests/storage-backend-contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,31 @@ test("registry: sqlite resolves through the registry; names are advertised", asy
}
});

for (const lane of lanes) {
test(`${lane.name}: KV prefixes match literal keys without case or accent folding`, async () => {
const dir = scratch();
const backend = await lane.open(dir);
const namespace = `kv-prefix-${lane.name}-${process.pid}-${Date.now()}`;
try {
for (const key of ["Prefix:one", "prefix:two", "café:one", "cafe:two", "a%_\\[literal]:one", "aXYZliteral:two"]) {
await backend.setKv(namespace, key, { key });
}
const keys = async (prefix: string) => (await backend.listKv(namespace, prefix)).map((r) => r.key);
assert.deepEqual(await keys("Prefix:"), ["Prefix:one"]);
assert.deepEqual(await keys("prefix:"), ["prefix:two"]);
assert.deepEqual(await keys("café:"), ["café:one"]);
assert.deepEqual(await keys("cafe:"), ["cafe:two"]);
assert.deepEqual(await keys("a%_\\[literal]:"), ["a%_\\[literal]:one"]);
assert.deepEqual(await keys("missing:"), []);
assert.equal((await keys("")).length, 6);
} finally {
await backend.deleteKv(namespace);
await backend.close();
fs.rmSync(dir, { recursive: true, force: true });
}
});
}

test("contract: ingest → reads round-trip through the interface", async () => {
const dir = scratch();
try {
Expand Down
Loading