From e7e3d61975bb1bf486823804feb29892bae7f26b Mon Sep 17 00:00:00 2001 From: natib21 Date: Tue, 16 Jun 2026 06:45:14 +0000 Subject: [PATCH] fix --- .../1718549500000-FixOrphanedRoutes.ts | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 apps/edr-freight-api/src/migrations/1718549500000-FixOrphanedRoutes.ts diff --git a/apps/edr-freight-api/src/migrations/1718549500000-FixOrphanedRoutes.ts b/apps/edr-freight-api/src/migrations/1718549500000-FixOrphanedRoutes.ts new file mode 100644 index 000000000..bfd9994f3 --- /dev/null +++ b/apps/edr-freight-api/src/migrations/1718549500000-FixOrphanedRoutes.ts @@ -0,0 +1,25 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class FixOrphanedRoutes1718549500000 implements MigrationInterface { + public async up(queryRunner: QueryRunner): Promise { + // Delete orphaned routes records with invalid origin_yard_id + await queryRunner.query(` + DELETE FROM "freight"."routes" + WHERE "origin_yard_id" IS NOT NULL + AND "origin_yard_id" NOT IN (SELECT "id" FROM "freight"."yards") + `); + + // Also clean up destination_yard_id if it exists and has the same issue + await queryRunner.query(` + DELETE FROM "freight"."routes" + WHERE "destination_yard_id" IS NOT NULL + AND "destination_yard_id" NOT IN (SELECT "id" FROM "freight"."yards") + `); + + console.log('✅ Deleted orphaned routes records'); + } + + public async down(_queryRunner: QueryRunner): Promise { + // No rollback needed for data cleanup + } +}