/**
 * Migration 026: Fuzzy search with trigram similarity (pg_trgm).
 *
 * This is an ordered node-pg-migrate migration. It must not open its own
 * connection or execute work while the migration module is being loaded.
 */

/** @param {import('node-pg-migrate').MigrationBuilder} pgm */
exports.up = (pgm) => {
  pgm.sql('CREATE EXTENSION IF NOT EXISTS pg_trgm');

  pgm.sql(`
    CREATE INDEX IF NOT EXISTS catalog_products_name_trgm_idx
    ON catalog_products
    USING GIN (name gin_trgm_ops)
  `);

  pgm.sql(`
    CREATE INDEX IF NOT EXISTS catalog_products_description_trgm_idx
    ON catalog_products
    USING GIN (description gin_trgm_ops)
  `);

  pgm.sql(`
    CREATE INDEX IF NOT EXISTS brands_brands_name_trgm_idx
    ON brands_brands
    USING GIN (name gin_trgm_ops)
  `);

  pgm.sql(`
    CREATE INDEX IF NOT EXISTS categories_categories_name_trgm_idx
    ON categories_categories
    USING GIN (name gin_trgm_ops)
  `);
};

/** @param {import('node-pg-migrate').MigrationBuilder} pgm */
exports.down = (pgm) => {
  pgm.sql('DROP INDEX IF EXISTS categories_categories_name_trgm_idx');
  pgm.sql('DROP INDEX IF EXISTS brands_brands_name_trgm_idx');
  pgm.sql('DROP INDEX IF EXISTS catalog_products_description_trgm_idx');
  pgm.sql('DROP INDEX IF EXISTS catalog_products_name_trgm_idx');
  pgm.sql('DROP EXTENSION IF EXISTS pg_trgm');
};
