-- Phase 6 dependencies are restored after product intelligence creates the
-- canonical products table. Both statements are idempotent for databases that
-- already completed the historical Phase 6 migration.
CREATE INDEX IF NOT EXISTS "products_name_fts"
  ON "products" USING gin(to_tsvector('english', "canonicalName"));

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1
    FROM pg_constraint
    WHERE conname = 'product_score_history_productId_fkey'
      AND conrelid = 'public.product_score_history'::regclass
  ) THEN
    ALTER TABLE "product_score_history"
      ADD CONSTRAINT "product_score_history_productId_fkey"
      FOREIGN KEY ("productId") REFERENCES "products"("id") ON DELETE CASCADE ON UPDATE CASCADE;
  END IF;
END $$;
