feat: finalize category seeding process and remove obsolete product categorization logic
Test Suite / test (24.15.0) (push) Has been cancelled

This commit is contained in:
Nils-Johan Gynther
2026-05-09 15:20:16 +02:00
parent 97e7b09bcd
commit 000a28bea4
+6 -81
View File
@@ -560,90 +560,15 @@ INSERT INTO `Category` (`name`, `parentId`)
-- ============================================================
-- STEG 3: ÅTERKOPPLA Product.categoryId EFTER KATEGORI-RESET
-- STEG 3: KATEGORISEED SLUTFÖRD
-- ============================================================
-- Gamla kolumner (Product.category, Product.subcategory) finns
-- inte längre — dessa togs bort i migrationen
-- 20260509000000_remove_product_category_and_add_indexes
--
-- Matchningsordning (från mest specifik till minst specifik):
-- 1) Product.category = nivå 2, Product.subcategory = nivå 3
-- 2) Product.category = nivå 1, Product.subcategory = nivå 3
-- 3) Product.category = nivå 1, Product.subcategory = nivå 2
-- 4) Product.category = nivå 1 (fallback)
--
-- Obs: Vi sätter bara categoryId där den fortfarande är NULL.
-- All produktkategorisering görs nu via categoryId direkt.
-- ============================================================
SET @has_subcategory := (
SELECT COUNT(*)
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'Product'
AND COLUMN_NAME = 'subcategory'
);
-- 1) category (nivå 2) + subcategory (nivå 3)
SET @sql := IF(
@has_subcategory > 0,
'UPDATE `Product` p
JOIN `Category` c1 ON c1.parentId IS NULL
JOIN `Category` c2 ON c2.parentId = c1.id
JOIN `Category` c3 ON c3.parentId = c2.id
SET p.`categoryId` = c3.id
WHERE p.`categoryId` IS NULL
AND p.`category` IS NOT NULL
AND p.`subcategory` IS NOT NULL
AND LOWER(TRIM(p.`category`)) = LOWER(TRIM(c2.`name`))
AND LOWER(TRIM(p.`subcategory`)) = LOWER(TRIM(c3.`name`))',
'SELECT ''Skipping subcategory remap step 1: Product.subcategory missing'' AS info'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- 2) category (nivå 1) + subcategory (nivå 3)
SET @sql := IF(
@has_subcategory > 0,
'UPDATE `Product` p
JOIN `Category` c1 ON c1.parentId IS NULL
JOIN `Category` c2 ON c2.parentId = c1.id
JOIN `Category` c3 ON c3.parentId = c2.id
SET p.`categoryId` = c3.id
WHERE p.`categoryId` IS NULL
AND p.`category` IS NOT NULL
AND p.`subcategory` IS NOT NULL
AND LOWER(TRIM(p.`category`)) = LOWER(TRIM(c1.`name`))
AND LOWER(TRIM(p.`subcategory`)) = LOWER(TRIM(c3.`name`))',
'SELECT ''Skipping subcategory remap step 2: Product.subcategory missing'' AS info'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- 3) category (nivå 1) + subcategory (nivå 2)
SET @sql := IF(
@has_subcategory > 0,
'UPDATE `Product` p
JOIN `Category` c1 ON c1.parentId IS NULL
JOIN `Category` c2 ON c2.parentId = c1.id
SET p.`categoryId` = c2.id
WHERE p.`categoryId` IS NULL
AND p.`category` IS NOT NULL
AND p.`subcategory` IS NOT NULL
AND LOWER(TRIM(p.`category`)) = LOWER(TRIM(c1.`name`))
AND LOWER(TRIM(p.`subcategory`)) = LOWER(TRIM(c2.`name`))',
'SELECT ''Skipping subcategory remap step 3: Product.subcategory missing'' AS info'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- 4) fallback: category (nivå 1)
UPDATE `Product` p
JOIN `Category` c1 ON c1.parentId IS NULL
SET p.`categoryId` = c1.id
WHERE p.`categoryId` IS NULL
AND p.`category` IS NOT NULL
AND LOWER(TRIM(p.`category`)) = LOWER(TRIM(c1.`name`));
-- Summering efter återkoppling
SELECT COUNT(*) AS products_with_category_id
FROM `Product`
WHERE `categoryId` IS NOT NULL;