Skip to content

Commit e741c02

Browse files
committed
fix(sql): repair invalid UPDATE syntax in order_items migration script
1 parent f58f8d0 commit e741c02

File tree

1 file changed

+4
-10
lines changed

1 file changed

+4
-10
lines changed

supabase/migrations/fix_order_items_snapshot.sql

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,12 @@ ALTER TABLE public.order_items
1616
ADD COLUMN IF NOT EXISTS product_name text,
1717
ADD COLUMN IF NOT EXISTS variant_name text;
1818

19-
-- 2. Back-fill existing rows from the joined product/variant tables so historical
20-
-- orders aren't left with NULLs where the product still exists.
21-
UPDATE public.order_items oi
19+
UPDATE public.order_items
2220
SET
23-
product_name = COALESCE(oi.product_name, p.title),
24-
variant_name = COALESCE(oi.variant_name, pv.name)
25-
FROM
26-
public.products p
27-
LEFT JOIN public.product_variants pv ON pv.id = oi.variant_id
21+
product_name = COALESCE(product_name, (SELECT title FROM public.products p WHERE p.id = product_id)),
22+
variant_name = COALESCE(variant_name, (SELECT name FROM public.product_variants pv WHERE pv.id = variant_id))
2823
WHERE
29-
p.id = oi.product_id
30-
AND (oi.product_name IS NULL OR oi.variant_name IS NULL);
24+
product_name IS NULL OR (variant_name IS NULL AND variant_id IS NOT NULL);
3125

3226
-- 3. Add index on order_id if missing (speeds up order detail page queries)
3327
CREATE INDEX IF NOT EXISTS idx_order_items_order_id

0 commit comments

Comments
 (0)