diff --git a/apps/edr-freight-api/src/migrations/1718549400000-FixOrphanedRouteMilestones.ts b/apps/edr-freight-api/src/migrations/1718549400000-FixOrphanedRouteMilestones.ts new file mode 100644 index 000000000..cc8768868 --- /dev/null +++ b/apps/edr-freight-api/src/migrations/1718549400000-FixOrphanedRouteMilestones.ts @@ -0,0 +1,19 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class FixOrphanedRouteMilestones1718549400000 implements MigrationInterface { + public async up(queryRunner: QueryRunner): Promise { + // Clean up orphaned route_milestones records + await queryRunner.query(` + UPDATE "freight"."route_milestones" + SET "yard_id" = NULL + WHERE "yard_id" IS NOT NULL + AND "yard_id" NOT IN (SELECT "id" FROM "freight"."yards") + `); + + console.log('✅ Cleaned up orphaned route_milestones records'); + } + + public async down(queryRunner: QueryRunner): Promise { + // No rollback needed for data cleanup + } +}