Skip to content
This repository has been archived by the owner on Feb 23, 2025. It is now read-only.

pref: add fulltext index for bookmarkEntries.text #382

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { Knex } from "knex";

export async function up(knex: Knex) {
await knex.raw(
"ALTER TABLE `bookmarkEntries` ADD FULLTEXT KEY `text` (`text`) /*!50100 WITH PARSER `ngram` */ ;"
);
}

export async function down(knex: Knex) {
await knex.raw("ALTER TABLE `bookmarkEntries` DROP KEY `text`;");
}
17 changes: 7 additions & 10 deletions app/db/skeema/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,24 @@
we started to experiment with skeema.
we still use knex to apply migrations, but it's already useful enough during development.

## generate knex migrations
## generate up/down migrations

```
# 0. update skeema/some-table.sql

# 1. generate empty knex migration
pnpm knex migrate:make some-migration

# 2. diff for `up` knex migration
# 1. diff for "up" migration
pnpm skeema diff --allow-unsafe

# 3. apply knex migration
pnpm knex migrate:up
# 2. apply "up"
pnpm skeema push --allow-unsafe

# 4. temporary revert skeema/some-table.sql
# 3. temporary revert skeema/some-table.sql
git stash

# 5. diff for `down` knex migration
# 4. diff for "down" migration
pnpm skeema diff --allow-unsafe

# 6. restore skeema/some-table.sql
# 5. restore skeema/some-table.sql
git stash pop
```

Expand Down
3 changes: 2 additions & 1 deletion app/db/skeema/bookmarkEntries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ CREATE TABLE `bookmarkEntries` (
PRIMARY KEY (`id`),
KEY `bookmarkEntries_userId_createdAt_key` (`userId`,`createdAt`),
KEY `bookmarkEntries_videoId_key` (`videoId`),
KEY `bookmarkEntries_captionEntryId_key` (`captionEntryId`)
KEY `bookmarkEntries_captionEntryId_key` (`captionEntryId`),
FULLTEXT KEY `text` (`text`) /*!50100 WITH PARSER `ngram` */
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

skeema put extra trailing space after magic comment.
It's a bit too annoying. Not sure if there's any workaround.

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
8 changes: 6 additions & 2 deletions app/trpc/routes/bookmarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,12 @@ export const trpcRoutesBookmarks = {
.where(
E.and(
E.eq(T.bookmarkEntries.userId, ctx.user.id),
// TODO: index
mapOption(input.q, (v) => E.like(T.bookmarkEntries.text, `%${v}%`))
// cf. https://dev.mysql.com/doc/refman/8.0/en/fulltext-boolean.html
mapOption(
input.q,
(v) =>
sql`MATCH (${T.bookmarkEntries.text}) AGAINST (${v} IN BOOLEAN MODE)`
)
)
)
.orderBy(E.desc(T.bookmarkEntries.createdAt))
Expand Down