diff --git a/plugins/catalog-backend/migrations/20260510000000_search_indices_and_dedup.js b/plugins/catalog-backend/migrations/20260510000000_search_indices_and_dedup.js index e60e39a017..623c5bbfce 100644 --- a/plugins/catalog-backend/migrations/20260510000000_search_indices_and_dedup.js +++ b/plugins/catalog-backend/migrations/20260510000000_search_indices_and_dedup.js @@ -80,10 +80,45 @@ exports.up = async function up(knex) { /** * @param {import('knex').Knex} knex */ -exports.down = async function down(_knex) { - // Intentionally a no-op. The old non-covering indices are not worth - // recreating, and the UNIQUE constraint should not be removed since the - // code now relies on ON CONFLICT for correctness. +exports.down = async function down(knex) { + const client = knex.client.config.client; + + if (client.includes('pg')) { + // Remove the new indices and restore the old ones + await knex.raw( + 'DROP INDEX CONCURRENTLY IF EXISTS search_entity_key_value_idx', + ); + await knex.raw( + 'DROP INDEX CONCURRENTLY IF EXISTS search_key_value_entity_idx', + ); + await knex.raw( + 'DROP INDEX CONCURRENTLY IF EXISTS search_facets_covering_idx', + ); + await knex.raw( + 'CREATE INDEX CONCURRENTLY IF NOT EXISTS search_key_value_idx ON search (key, value)', + ); + await knex.raw( + 'CREATE INDEX CONCURRENTLY IF NOT EXISTS search_key_original_value_idx ON search (key, original_value)', + ); + } else if (client.includes('mysql')) { + await mysqlDropIndexIfExists(knex, 'search_entity_key_value_idx'); + await mysqlDropIndexIfExists(knex, 'search_key_value_entity_idx'); + await mysqlDropIndexIfExists(knex, 'search_facets_covering_idx'); + await knex.schema.alterTable('search', table => { + table.index(['key', 'value'], 'search_key_value_idx'); + table.index(['key', 'original_value'], 'search_key_original_value_idx'); + }); + } else { + await knex.raw('DROP INDEX IF EXISTS search_entity_key_value_idx'); + await knex.raw('DROP INDEX IF EXISTS search_key_value_entity_idx'); + await knex.raw('DROP INDEX IF EXISTS search_facets_covering_idx'); + await knex.raw( + 'CREATE INDEX IF NOT EXISTS search_key_value_idx ON search (key, value)', + ); + await knex.raw( + 'CREATE INDEX IF NOT EXISTS search_key_original_value_idx ON search (key, original_value)', + ); + } }; exports.config = { transaction: false }; diff --git a/plugins/catalog-backend/report.sql.md b/plugins/catalog-backend/report.sql.md index e23b250be8..0ea80db3f4 100644 --- a/plugins/catalog-backend/report.sql.md +++ b/plugins/catalog-backend/report.sql.md @@ -127,8 +127,9 @@ ### Indices - `search_entity_id_idx` (`entity_id`) -- `search_key_original_value_idx` (`key`, `original_value`) -- `search_key_value_idx` (`key`, `value`) +- `search_entity_key_value_idx` (`entity_id`, `key`, `value`) unique +- `search_facets_covering_idx` (`key`, `original_value`, `entity_id`) +- `search_key_value_entity_idx` (`key`, `value`, `entity_id`) ## Table `stitch_queue`