45 lines
1.9 KiB
SQL
45 lines
1.9 KiB
SQL
-- Fix missing product data in order items
|
|
-- Updates order items to use actual product names and images from the product table
|
|
-- instead of placeholders or NULLs.
|
|
|
|
DO $$
|
|
BEGIN
|
|
-- 1. Update image_url from SKU if available
|
|
UPDATE public.ml_order_items oi
|
|
SET image_url = sku.image_url
|
|
FROM public.ml_product_skus sku
|
|
WHERE oi.sku_id = sku.id
|
|
AND (oi.image_url IS NULL OR oi.image_url = '');
|
|
|
|
-- 2. Update image_url from Product Main Image if still NULL
|
|
UPDATE public.ml_order_items oi
|
|
SET image_url = p.main_image_url
|
|
FROM public.ml_products p
|
|
WHERE oi.product_id = p.id
|
|
AND (oi.image_url IS NULL OR oi.image_url = '');
|
|
|
|
-- 3. Update product_name from Product if it looks like a placeholder
|
|
-- (checking for "Product" or similar might be too aggressive, but let's update all to ensure consistency with current product DB)
|
|
-- Actually, let's only update if it looks like the mock data we inserted "Pending Order Product", etc.
|
|
UPDATE public.ml_order_items oi
|
|
SET product_name = p.name
|
|
FROM public.ml_products p
|
|
WHERE oi.product_id = p.id
|
|
AND (
|
|
oi.product_name LIKE '%Product%'
|
|
OR oi.product_name LIKE 'Pending Order%'
|
|
OR oi.product_name = 'Test Product'
|
|
);
|
|
|
|
-- 4. Ensure we have items for all orders (Cleanup empty orders if any, or insert default item)
|
|
-- For now, let's just log if there are orders without items
|
|
-- SELECT count(*) FROM ml_orders o WHERE NOT EXISTS (SELECT 1 FROM ml_order_items oi WHERE oi.order_id = o.id);
|
|
|
|
-- Insert a default item for orders that have NO items (if any exist)
|
|
-- We need a valid product ID for this.
|
|
-- This is a bit complex in a DO block without specific IDs, so we'll skip creating new items for now
|
|
-- unless specifically requested. The main issue reported was likely "missing associated data" (images/names).
|
|
|
|
RAISE NOTICE 'Updated order items with real product data.';
|
|
END $$;
|