diff --git a/migrations/000.586.sql b/migrations/000.586.sql index b488389..cafe719 100644 --- a/migrations/000.586.sql +++ b/migrations/000.586.sql @@ -15359,7 +15359,7 @@ BEGIN FROM temp_room_updates tru LEFT JOIN "product-printproducts" pp ON pp.productid = tru.product_id AND pp.groupid IN (1, 3) WHERE pp.printid IS NULL; - + IF missing_count > 0 THEN RAISE NOTICE 'Warning: % products have no matching prints with groupid 1 or 3', missing_count; END IF; @@ -15374,7 +15374,7 @@ BEGIN FROM temp_room_updates tru LEFT JOIN rooms r ON r.name = tru.new_room_name || '_wallpaper_standing' WHERE r.id IS NULL; - + IF invalid_count > 0 THEN RAISE NOTICE 'Warning: % unique room names not found in database', invalid_count; END IF; @@ -15391,18 +15391,18 @@ BEGIN FROM temp_room_updates tru INNER JOIN "product-printproducts" pp ON pp.productid = tru.product_id AND pp.groupid IN (1, 3) INNER JOIN rooms r ON r.name = tru.new_room_name || '_wallpaper_standing'; - + SELECT COUNT(pp.printid) INTO print_count FROM temp_room_updates tru INNER JOIN "product-printproducts" pp ON pp.productid = tru.product_id AND pp.groupid IN (1, 3) INNER JOIN rooms r ON r.name = tru.new_room_name || '_wallpaper_standing'; - + SELECT COUNT(*) INTO interior_count FROM temp_room_updates tru INNER JOIN "product-printproducts" pp ON pp.productid = tru.product_id AND pp.groupid IN (1, 3) INNER JOIN interiors i ON i.print_id = pp.printid AND i.position = 0 INNER JOIN rooms r ON r.name = tru.new_room_name || '_wallpaper_standing'; - + RAISE NOTICE 'Summary: % products, % prints matched. Currently % interiors exist at position 0 (will be updated, rest will be inserted)', product_count, print_count, interior_count; END $$; @@ -15419,7 +15419,12 @@ UPDATE interiors i SET room_id = prm.new_room_id FROM print_room_mapping prm WHERE i.print_id = prm.printid - AND i.position = 0; + AND i.position = 0 + AND NOT EXISTS ( + SELECT 1 FROM interiors i2 + WHERE i2.print_id = i.print_id + AND i2.room_id = prm.new_room_id + ); -- Step 5: Insert new position 0 interiors for prints that don't have them yet WITH print_room_mapping AS ( @@ -15450,7 +15455,7 @@ BEGIN INNER JOIN interiors i ON i.print_id = pp.printid AND i.position = 0 INNER JOIN rooms r ON r.name = tru.new_room_name || '_wallpaper_standing' WHERE i.room_id = r.id; - + RAISE NOTICE 'Verification: Successfully updated/inserted % position 0 interiors with correct room_id', updated_count; END $$;