Skip to content
Draft
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
5 changes: 5 additions & 0 deletions .changeset/quiet-folders-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"emdash": patch
---

Fixes media folder searches failing on Cloudflare D1 when long, Unicode, or punctuation-heavy search terms exceed its pattern limit. Searches match the complete term without truncation.
12 changes: 6 additions & 6 deletions packages/core/src/database/repositories/media-folders.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { sql, type Kysely } from "kysely";
import { ulid } from "ulidx";

import { isPostgres } from "../dialect-helpers.js";
import type { Database } from "../types.js";
import { decodeCursor, EmDashValidationError, encodeCursor, type FindManyResult } from "./types.js";

Expand All @@ -15,10 +16,6 @@ export interface FindManyMediaFoldersOptions {
q?: string;
}

function escapeLike(value: string): string {
return value.replaceAll("\\", "\\\\").replaceAll("%", "\\%").replaceAll("_", "\\_");
}

function normalizeFolderSearch(value: string): string {
return value.trim().normalize("NFKC").toLowerCase();
}
Expand Down Expand Up @@ -48,8 +45,11 @@ export class MediaFolderRepository {

const term = normalizeFolderSearch(options.q ?? "");
if (term) {
const pattern = `%${escapeLike(term)}%`;
query = query.where("name_key", "like", sql<string>`${pattern} escape '\\'`);
// D1 limits LIKE patterns to 50 bytes, including escapes and wildcards.
const position = isPostgres(this.db)
? sql<number>`strpos(name_key, ${term})`
: sql<number>`instr(name_key, ${term})`;
query = query.where(position, ">", 0);
}

if (options.cursor !== undefined) {
Expand Down
34 changes: 34 additions & 0 deletions packages/core/tests/workerd/media-folder-search-d1.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { env } from "cloudflare:test";
import { Kysely } from "kysely";
import { expect, it } from "vitest";

import { RawBindingD1Dialect } from "../../../cloudflare/src/db/d1-dialect.js";
import { runMigrations } from "../../src/database/migrations/runner.js";
import { MediaFolderRepository } from "../../src/database/repositories/media-folders.js";
import type { Database } from "../../src/database/types.js";
import { resetD1Schema } from "./d1-schema.js";

declare module "cloudflare:test" {
interface ProvidedEnv {
DB: D1Database;
}
}

it("searches the complete 49-character folder substring on D1", async () => {
const db = new Kysely<Database>({ dialect: new RawBindingD1Dialect({ database: env.DB }) });
try {
await resetD1Schema(db);
await runMigrations(db);
const folders = new MediaFolderRepository(db);
const term = `${"a".repeat(48)}b`;
const match = await folders.create(`prefix ${term} suffix`);
await folders.create(`prefix ${term.slice(0, -1)}c suffix`);

expect(await folders.findMany({ q: term })).toEqual({
items: [match],
nextCursor: undefined,
});
} finally {
await db.destroy();
}
});
Loading