1 import { Migration } from "@medusajs/framework/mikro-orm/migrations"
2
3 /**
4 * Enables portable search extensions and creates the catalog table.
5 *
6 * Always enabled (native engine):
7 * - `pg_trgm` — typo tolerance
8 * - `unaccent` — accent-insensitive FTS
9 * - `medusa_search_english` — unaccent-backed text search config
10 *
11 * Lakebase extensions (`lakebase_vector`, `lakebase_text`) are enabled in a
12 * later migration via `CREATE EXTENSION ... CASCADE`.
13 */
14 export class Migration20260807120000 extends Migration {
15 override async up(): Promise<void> {
16 // Extension creation needs privileges the app role may not have on managed
17 // Postgres. Soft-fail so migrations still apply; the provider assumes these
18 // extensions (and the text search config) exist at runtime.
19 this.addSql(`
20 DO $$
21 BEGIN
22 BEGIN
23 CREATE EXTENSION IF NOT EXISTS pg_trgm;
24 EXCEPTION WHEN OTHERS THEN
25 RAISE NOTICE 'search-postgres: could not enable pg_trgm (%).', SQLERRM;
26 END;
27 BEGIN
28 CREATE EXTENSION IF NOT EXISTS unaccent;
29 EXCEPTION WHEN OTHERS THEN
30 RAISE NOTICE 'search-postgres: could not enable unaccent (%).', SQLERRM;
31 END;
32 BEGIN
33 IF EXISTS (SELECT 1 FROM pg_extension WHERE extname = 'unaccent')
34 AND NOT EXISTS (
35 SELECT 1 FROM pg_ts_config WHERE cfgname = 'medusa_search_english'
36 ) THEN
37 CREATE TEXT SEARCH CONFIGURATION medusa_search_english (COPY = english);
38 ALTER TEXT SEARCH CONFIGURATION medusa_search_english
39 ALTER MAPPING FOR hword, hword_part, word
40 WITH unaccent, english_stem;
41 END IF;
42 EXCEPTION WHEN OTHERS THEN
43 RAISE NOTICE 'search-postgres: could not create medusa_search_english (%).', SQLERRM;
44 END;
45 END
46 $$;
47 `)
48
49 this.addSql(`
50 create table if not exists "search_postgres_index" (
51 "name" text not null,
52 "table_name" text not null,
53 "schema_hash" text not null,
54 "plan" jsonb not null,
55 "document_count" integer not null default 0,
56 "created_at" timestamptz not null default now(),
57 "updated_at" timestamptz not null default now(),
58 constraint "search_postgres_index_pkey" primary key ("name")
59 );
60 `)
61 }
62
63 override async down(): Promise<void> {
64 this.addSql(`drop table if exists "search_postgres_index" cascade;`)
65 }
66 }