37 lines
1.7 KiB
PL/PgSQL
37 lines
1.7 KiB
PL/PgSQL
-- CR-007 (Step 10G.6): contracts carry a good, a shipper and Priority fields.
|
|
-- Contracts live in JSONB (stored boards and dispatch snapshots), so no columns
|
|
-- change. Backfill contracts stored before the change so existing saves keep
|
|
-- working: a default good of the contract's class, the origin's first shipper,
|
|
-- and no Priority. Rows that already have a good are left alone, so running
|
|
-- this again changes nothing.
|
|
CREATE FUNCTION pg_temp.cr007_backfill(c jsonb) RETURNS jsonb AS $$
|
|
SELECT CASE WHEN c ? 'goodId' THEN c ELSE c || jsonb_build_object(
|
|
'goodId', CASE c->>'cargoTypeId'
|
|
WHEN 'heavy' THEN 'machine-parts'
|
|
WHEN 'high-value' THEN 'electronics'
|
|
WHEN 'dangerous' THEN 'industrial-chemicals'
|
|
ELSE 'consumer-goods' END,
|
|
'shipper', CASE c->>'originId'
|
|
WHEN 'moon' THEN 'Shackleton Mining Co.'
|
|
WHEN 'mars' THEN 'Jezero Agricultural Co-op'
|
|
WHEN 'ceres-station' THEN 'Belt Ore Consolidated'
|
|
WHEN 'jupiter-cloud-platform' THEN 'Galilean Fuel Combine'
|
|
ELSE 'Pacific Orbital Logistics' END,
|
|
'priority', false,
|
|
'deliveryWindowGameHours', null
|
|
) END
|
|
$$ LANGUAGE sql IMMUTABLE;
|
|
--> statement-breakpoint
|
|
UPDATE "contract_boards"
|
|
SET "contracts" = (
|
|
SELECT COALESCE(jsonb_agg(pg_temp.cr007_backfill(t.c) ORDER BY t.ord), '[]'::jsonb)
|
|
FROM jsonb_array_elements("contracts") WITH ORDINALITY AS t(c, ord)
|
|
)
|
|
WHERE EXISTS (
|
|
SELECT 1 FROM jsonb_array_elements("contracts") AS e(c) WHERE NOT (e.c ? 'goodId')
|
|
);
|
|
--> statement-breakpoint
|
|
UPDATE "dispatches"
|
|
SET "record" = jsonb_set("record", '{contract}', pg_temp.cr007_backfill("record"->'contract'))
|
|
WHERE jsonb_typeof("record"->'contract') = 'object' AND NOT ("record"->'contract' ? 'goodId');
|