diff --git a/db/migrations/0009_shopify_catalog_alignment.sql b/db/migrations/0009_shopify_catalog_alignment.sql new file mode 100644 index 0000000..f4610f6 --- /dev/null +++ b/db/migrations/0009_shopify_catalog_alignment.sql @@ -0,0 +1,121 @@ +BEGIN; + +-- Shopify catalog alignment for the current ERP catalog. +-- Historical Wix aliases remain untouched; Shopify aliases are added separately. + +INSERT INTO product (sku, name, status, uom, created_at, updated_at) +VALUES ( + '006', + 'Cordyceps Extrakt Tinktur 50 ml | 100% rein', + 'active', + 'unit', + NOW(), + NOW() +) +ON CONFLICT (sku) DO NOTHING; + +-- The existing ERP item carrying the historical 005.01 alias is the +-- Lion''s Mane & Chaga item. Its internal code must match its Shopify SKU. +UPDATE sellable_item si +SET item_code = '005.01', + display_name = 'Lion''s Mane & Chaga Extrakt Tinktur | 2 x 50 ml', + updated_at = NOW() +WHERE si.id = ( + SELECT eia.sellable_item_id + FROM external_item_alias eia + WHERE eia.source_system = 'wix' + AND eia.external_article_number = '005.01' + ORDER BY eia.id + LIMIT 1 +); + +UPDATE sellable_item +SET display_name = CASE item_code + WHEN '001.01' THEN 'Chaga Extrakt Tinktur 50 ml | 100% rein' + WHEN '001.02' THEN 'Chaga Extrakt Tinktur | 2 x 50 ml | 100% rein' + WHEN '002.01' THEN 'Lion''s Mane Extrakt Tinktur 50 ml | 100% rein' + WHEN '002.02' THEN 'Lion''s Mane Extrakt Tinktur | 2 × 50 ml | 100% rein' + WHEN '003.01' THEN 'Reishi Extrakt Tinktur 50 ml | 100% rein' + WHEN '003.02' THEN 'Reishi Extrakt Tinktur | 2 x 50 ml | 100% rein' + WHEN '004.01' THEN 'Shiitake Extrakt Tinktur 50 ml | 100% rein' + WHEN '004.02' THEN 'Shiitake Extrakt Tinktur | 2 x 50 ml | 100% rein' + WHEN '005.01' THEN 'Lion''s Mane & Chaga Extrakt Tinktur | 2 x 50 ml' + WHEN '005.02' THEN 'Lion''s Mane & Reishi Extrakt Tinktur | 2 x 50 ml' + ELSE display_name +END, +updated_at = NOW() +WHERE item_code IN ( + '001.01', '001.02', '002.01', '002.02', '003.01', '003.02', + '004.01', '004.02', '005.01', '005.02' +); + +INSERT INTO sellable_item (item_code, display_name, status, created_at, updated_at) +VALUES + ('006.01', 'Cordyceps Extrakt Tinktur 50 ml | 100% rein', 'active', NOW(), NOW()), + ('006.02', 'Cordyceps Extrakt Tinktur 50 ml | 100% rein | 2er Set', 'active', NOW(), NOW()) +ON CONFLICT (item_code) DO NOTHING; + +INSERT INTO sellable_item_component ( + sellable_item_id, + product_id, + qty_per_item, + created_at, + updated_at +) +SELECT si.id, p.id, v.qty_per_item, NOW(), NOW() +FROM ( + VALUES ('006.01', 1.0000::NUMERIC), ('006.02', 2.0000::NUMERIC) +) AS v(item_code, qty_per_item) +JOIN sellable_item si ON si.item_code = v.item_code +JOIN product p ON p.sku = '006' +WHERE NOT EXISTS ( + SELECT 1 + FROM sellable_item_component sic + WHERE sic.sellable_item_id = si.id + AND sic.product_id = p.id +); + +-- Add the Shopify catalog identities without rewriting historical Wix aliases. +INSERT INTO external_item_alias ( + source_system, + external_article_number, + external_title, + title_normalized, + sellable_item_id, + is_active, + created_at, + updated_at +) +SELECT + 'shopify', + v.sku, + v.title, + v.title_normalized, + si.id, + TRUE, + NOW(), + NOW() +FROM ( + VALUES + ('001.01', 'Chaga Extrakt Tinktur 50 ml | 100% rein', 'chaga extrakt tinktur 50 ml 100 rein'), + ('001.02', 'Chaga Extrakt Tinktur | 2 x 50 ml | 100% rein', 'chaga extrakt tinktur 2 x 50 ml 100 rein'), + ('002.01', 'Lion''s Mane Extrakt Tinktur 50 ml | 100% rein', 'lion s mane extrakt tinktur 50 ml 100 rein'), + ('002.02', 'Lion''s Mane Extrakt Tinktur | 2 × 50 ml | 100% rein', 'lion s mane extrakt tinktur 2 50 ml 100 rein'), + ('003.01', 'Reishi Extrakt Tinktur 50 ml | 100% rein', 'reishi extrakt tinktur 50 ml 100 rein'), + ('003.02', 'Reishi Extrakt Tinktur | 2 x 50 ml | 100% rein', 'reishi extrakt tinktur 2 x 50 ml 100 rein'), + ('004.01', 'Shiitake Extrakt Tinktur 50 ml | 100% rein', 'shiitake extrakt tinktur 50 ml 100 rein'), + ('004.02', 'Shiitake Extrakt Tinktur | 2 x 50 ml | 100% rein', 'shiitake extrakt tinktur 2 x 50 ml 100 rein'), + ('005.01', 'Lion''s Mane & Chaga Extrakt Tinktur | 2 x 50 ml', 'lion s mane chaga extrakt tinktur 2 x 50 ml'), + ('005.02', 'Lion''s Mane & Reishi Extrakt Tinktur | 2 x 50 ml', 'lion s mane reishi extrakt tinktur 2 x 50 ml'), + ('006.01', 'Cordyceps Extrakt Tinktur 50 ml | 100% rein', 'cordyceps extrakt tinktur 50 ml 100 rein'), + ('006.02', 'Cordyceps Extrakt Tinktur 50 ml | 100% rein | 2er Set', 'cordyceps extrakt tinktur 50 ml 100 rein 2er set') +) AS v(sku, title, title_normalized) +JOIN sellable_item si ON si.item_code = v.sku +WHERE NOT EXISTS ( + SELECT 1 + FROM external_item_alias eia + WHERE eia.source_system = 'shopify' + AND eia.external_article_number = v.sku +); + +COMMIT;