From 000a28bea4e67470988644f2f6950382c92e46d9 Mon Sep 17 00:00:00 2001 From: Nils-Johan Gynther Date: Sat, 9 May 2026 15:20:16 +0200 Subject: [PATCH] feat: finalize category seeding process and remove obsolete product categorization logic --- db/seeds/seed_all.sql | 87 +++---------------------------------------- 1 file changed, 6 insertions(+), 81 deletions(-) diff --git a/db/seeds/seed_all.sql b/db/seeds/seed_all.sql index 756db334..a0f11b18 100644 --- a/db/seeds/seed_all.sql +++ b/db/seeds/seed_all.sql @@ -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;