export flow and fix intercity issue

This commit is contained in:
Marshal
2026-07-30 13:11:35 +00:00
parent 9208050cb5
commit e80aac877f
19 changed files with 486 additions and 27 deletions

View File

@@ -0,0 +1,57 @@
import { MigrationInterface, QueryRunner } from "typeorm";
/**
* Several DCT (DORALEH) → GMP (KALITY) routes are missing the Dire Dawa stop
* in their milestone list. The corridor budget builds its per-leg edges from
* route_milestones, so on those routes a DCT→Dire Dawa or Dire Dawa→GMP
* booking cannot resolve its own leg and conservatively occupies the WHOLE
* route — per-leg wagon reuse (a wagon freed at Dire Dawa reloading for GMP)
* silently degrades to train-wide accounting.
*
* Insert the Dire Dawa milestone at sequence 2 on every active DORALEH→KALITY
* route with a stop list that lacks it, shifting later stops down. Matched by
* yard CODE so the repair is portable across environments. Idempotent: routes
* already carrying Dire Dawa are untouched.
*/
export class BackfillDireDawaMilestone3080000000000 implements MigrationInterface {
name = "BackfillDireDawaMilestone3080000000000";
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
DO $$
DECLARE
dire uuid;
r record;
BEGIN
SELECT id INTO dire FROM freight.yards
WHERE code = 'DIRE_DAWA' AND deleted_at IS NULL;
IF dire IS NULL THEN
RETURN;
END IF;
FOR r IN
SELECT rt.id
FROM freight.routes rt
JOIN freight.yards o ON o.id = rt.origin_yard_id AND o.code = 'DORALEH'
JOIN freight.yards d ON d.id = rt.destination_yard_id AND d.code = 'KALITY'
WHERE rt.deleted_at IS NULL
AND EXISTS (SELECT 1 FROM freight.route_milestones m
WHERE m.route_id = rt.id AND m.deleted_at IS NULL)
AND NOT EXISTS (SELECT 1 FROM freight.route_milestones m
WHERE m.route_id = rt.id AND m.yard_id = dire
AND m.deleted_at IS NULL)
LOOP
UPDATE freight.route_milestones
SET sequence_no = sequence_no + 1
WHERE route_id = r.id AND deleted_at IS NULL AND sequence_no >= 2;
INSERT INTO freight.route_milestones (route_id, yard_id, sequence_no)
VALUES (r.id, dire, 2);
END LOOP;
END $$;
`);
}
public async down(): Promise<void> {
// Data repair — not reversible.
}
}