Skip to content

Conversation

erikhfj
Copy link
Member

@erikhfj erikhfj commented Nov 25, 2026

Closes #130

Stacked on #136 (fix/129-server-hardening-and-cleanup).

What was done

  • Drizzle migration (server/drizzle/0008_unaccent-search.sql, _journal.json, 0008_snapshot.json):
    • Enables CREATE EXTENSION IF NOT EXISTS unaccent;.
    • Idempotently creates the public.simple_unaccent text search configuration (copied from pg_catalog.simple) and maps hword, hword_part, word, numword, and hword_numpart through unaccent, pg_catalog.simple.
    • Updates set_album_search_vector() to build weighted title (A) and artist (B) vectors with 'public.simple_unaccent'.
    • Backfills existing album.search_vector rows via UPDATE album SET title = title;.
  • Search query normalization (server/src/search/search.ts):
    • Updated tsQuery() to tokenize with to_tsvector('public.simple_unaccent', $1), preserving :* prefix matching and GIN index usage on album_search_vector_idx.
  • Tests (server/src/search/search.test.ts, server/src/search/search.postgres.test.ts):
    • Updated SQL generation unit test for 'public.simple_unaccent'.
    • Expanded PostgreSQL integration tests (26 tests total) covering accent-insensitive matches and prefixes on both inline fixtures and real album/artist/album_artist rows populated by set_album_search_vector() (bjork/bjor -> Björk, cafe/café -> Café del Mar, royksopp -> Röyksopp, beyonce -> Beyoncé, jose merce/jose mer -> José Mercé, plus title-vs-artist rank weighting).
  • Documentation (README.md):
    • Updated the catalog limitations note at README.md:272-274 so accent-sensitive search is no longer listed as a limitation while keeping the accurate note for non-Latin scripts.

What was tested

pnpm format:check && pnpm lint && pnpm typecheck && pnpm test && pnpm build
# All passed with 0 lint warnings and clean build.

Verified against a local throwaway PostgreSQL cluster:

  • DATABASE_URL="$TEST_DATABASE_URL" pnpm --filter @t19/server db:migrate (fresh + idempotent re-run) → Migrations applied.
  • DATABASE_URL="$TEST_DATABASE_URL" pnpm --filter @t19/server db:import:musicbrainz → succeeded
  • EXPLAIN verified Bitmap Index Scan on album_search_vector_idx
  • pnpm --filter @t19/server exec vitest run → 16/16 test files, 218/218 tests passed (search.postgres.test.ts: 26 passed)
  • node server/scripts/assert-postgres-tests.mjs → all 6 database-backed suites executed

Copy link
Member

@evenkkl evenkkl left a comment •

Choose a reason for hiding this comment

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

Looks good. I ran 0000–0008 on a throwaway PostgreSQL 16 cluster as a non-superuser role that owns the database (the same setup as t19 on the VM), with about 200k albums.

What I checked

  • Extension permissions: unaccent has been a trusted extension since PG 13, so the database owner can create it without being a superuser. It worked as a non-superuser owner, so the deploy migration should work on the VM (PG 18, t19 owns crate).
  • Idempotency: running 0008 a second time is clean, and the stored vectors stay the same.
  • Backfill: UPDATE album SET title = title does fire the UPDATE OF title trigger, so existing rows went from 'björk' to 'bjork'. It locks every album row until the commit. Reads still work, but writes to albums wait. With 200k rows it took about 7 s. With the real 2,000 albums it takes milliseconds.
  • Volatility: unaccent() is STABLE, but it only runs through to_tsvector(regconfig, text), which is IMMUTABLE. The GIN index is on a stored column that the trigger fills, not on an expression, so this is fine. If the config ever changes, the same backfill has to run again.
  • GIN index: EXPLAIN shows a Bitmap Index Scan on album_search_vector_idx.
  • Snapshot: 0008_snapshot.json is identical to 0007 except for id/prevId.

Nit (non-blocking)

numhword (a hyphenated token with letters and digits, for example Café-80s) is missing from ALTER MAPPING, so that token keeps its accent. The query cafe-80s finds nothing, while café-80s and cafe 80s both match. None of the current catalog titles or artist names hit this, but adding numhword to the list fixes it.

Optional

A test for renaming an accented artist (artist_refresh_search_vector) would cover the last trigger path.

Base automatically changed from fix/129-server-hardening-and-cleanup to main November 26, 2026 19:22
@erikhfj erikhfj force-pushed the feat/130-unaccent-search branch from a0513a1 to d235ecf Compare November 26, 2026 19:27
@erikhfj
Copy link
Member Author

erikhfj commented Nov 26, 2026

Thanks for the review and non-superuser PG testing, @evenkkl! Rebased onto main and addressed both items in d235ecf:

  1. numhword mapping (server/drizzle/0008_unaccent-search.sql): Added numhword to ALTER MAPPING FOR hword, hword_part, word, numword, numhword, hword_numpart so hyphenated letter+digit accented tokens like Café-80s match cafe-80s.
  2. PostgreSQL integration tests (server/src/search/search.postgres.test.ts): Added tests for Café-80s (cafe-80s / café-80s / cafe 80s) and renaming an artist to an accented name (UPDATE artist SET name = ... firing artist_refresh_search_vector), verified against a throwaway PG cluster (fresh + idempotent migration re-run, all 229 server tests passing).

Copy link
Member

@evenkkl evenkkl left a comment

Choose a reason for hiding this comment

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

Thanks, both items are fixed. Re-tested on the rebased branch against a fresh PostgreSQL 16 cluster with a non-superuser database owner:

  • Migrated on main (0000–0007) with accented rows already in place, then applied 0008. The backfill updated existing rows ('café-80s' → 'cafe-80s', 'björk' → 'bjork'), and cafe-80s, café-80s and cafe 80s now all match.
  • Running db:migrate again, and replaying 0008 by hand, is clean.
  • All 229 server tests pass, including 30 in search.postgres.test.ts, and assert-postgres-tests.mjs confirms that all 6 database-backed suites ran.
  • The new numhword and artist-rename tests cover what I asked for. The snapshot chain still lines up after the rebase.

LGTM.

@erikhfj erikhfj merged commit eb5833e into main Nov 26, 2026
3 checks passed
Sign in to join this conversation on GitHub.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support accent-insensitive album and artist search via PostgreSQL unaccent
2 participants