33 lines
1.5 KiB
SQL
33 lines
1.5 KiB
SQL
-- Phase 90: Add delivery_price column and backfill from import JSON data
|
|
-- Column was in CREATE TABLE migration but table predates it — add if missing
|
|
|
|
ALTER TABLE orders ADD COLUMN delivery_price DECIMAL(12,2) NULL AFTER total_paid;
|
|
|
|
-- Allegro: delivery_cost stored as object {amount, currency} in preferences_json
|
|
UPDATE orders
|
|
SET delivery_price = CAST(JSON_UNQUOTE(JSON_EXTRACT(preferences_json, '$.delivery_cost.amount')) AS DECIMAL(12,2))
|
|
WHERE source = 'allegro'
|
|
AND delivery_price IS NULL
|
|
AND preferences_json IS NOT NULL
|
|
AND JSON_EXTRACT(preferences_json, '$.delivery_cost.amount') IS NOT NULL;
|
|
|
|
-- Allegro fallback: delivery_cost stored as plain number (edge case)
|
|
UPDATE orders
|
|
SET delivery_price = CAST(JSON_UNQUOTE(JSON_EXTRACT(preferences_json, '$.delivery_cost')) AS DECIMAL(12,2))
|
|
WHERE source = 'allegro'
|
|
AND delivery_price IS NULL
|
|
AND preferences_json IS NOT NULL
|
|
AND JSON_EXTRACT(preferences_json, '$.delivery_cost') IS NOT NULL
|
|
AND JSON_TYPE(JSON_EXTRACT(preferences_json, '$.delivery_cost')) != 'OBJECT';
|
|
|
|
-- shopPRO: transport_cost / delivery_cost / shipping.cost in payload_json
|
|
UPDATE orders
|
|
SET delivery_price = COALESCE(
|
|
CAST(NULLIF(JSON_UNQUOTE(JSON_EXTRACT(payload_json, '$.transport_cost')), 'null') AS DECIMAL(12,2)),
|
|
CAST(NULLIF(JSON_UNQUOTE(JSON_EXTRACT(payload_json, '$.delivery_cost')), 'null') AS DECIMAL(12,2)),
|
|
CAST(NULLIF(JSON_UNQUOTE(JSON_EXTRACT(payload_json, '$.shipping.cost')), 'null') AS DECIMAL(12,2))
|
|
)
|
|
WHERE source = 'shoppro'
|
|
AND delivery_price IS NULL
|
|
AND payload_json IS NOT NULL;
|