From 2b5db67bcdeca367c3b2006b5973f7d4aeaac617 Mon Sep 17 00:00:00 2001 From: natib21 Date: Tue, 16 Jun 2026 06:33:43 +0000 Subject: [PATCH 1/7] fix --- ...718549400000-FixOrphanedRouteMilestones.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 apps/edr-freight-api/src/migrations/1718549400000-FixOrphanedRouteMilestones.ts 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 + } +} From 0198b545c91b1f8dd139c3013a46faf787264304 Mon Sep 17 00:00:00 2001 From: natib21 Date: Tue, 16 Jun 2026 06:35:51 +0000 Subject: [PATCH 2/7] fix --- .../src/migrations/1718549400000-FixOrphanedRouteMilestones.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/edr-freight-api/src/migrations/1718549400000-FixOrphanedRouteMilestones.ts b/apps/edr-freight-api/src/migrations/1718549400000-FixOrphanedRouteMilestones.ts index cc8768868..d2aa22e8c 100644 --- a/apps/edr-freight-api/src/migrations/1718549400000-FixOrphanedRouteMilestones.ts +++ b/apps/edr-freight-api/src/migrations/1718549400000-FixOrphanedRouteMilestones.ts @@ -13,7 +13,7 @@ export class FixOrphanedRouteMilestones1718549400000 implements MigrationInterfa console.log('✅ Cleaned up orphaned route_milestones records'); } - public async down(queryRunner: QueryRunner): Promise { + public async down(_queryRunner: QueryRunner): Promise { // No rollback needed for data cleanup } } From 8362bd94c52ae4d2a6154b0144cdc2e2e6c2e361 Mon Sep 17 00:00:00 2001 From: natib21 Date: Tue, 16 Jun 2026 06:39:59 +0000 Subject: [PATCH 3/7] fix --- .../migrations/1718549400000-FixOrphanedRouteMilestones.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/apps/edr-freight-api/src/migrations/1718549400000-FixOrphanedRouteMilestones.ts b/apps/edr-freight-api/src/migrations/1718549400000-FixOrphanedRouteMilestones.ts index d2aa22e8c..c28836194 100644 --- a/apps/edr-freight-api/src/migrations/1718549400000-FixOrphanedRouteMilestones.ts +++ b/apps/edr-freight-api/src/migrations/1718549400000-FixOrphanedRouteMilestones.ts @@ -2,15 +2,14 @@ import { MigrationInterface, QueryRunner } from 'typeorm'; export class FixOrphanedRouteMilestones1718549400000 implements MigrationInterface { public async up(queryRunner: QueryRunner): Promise { - // Clean up orphaned route_milestones records + // Delete orphaned route_milestones records (yard_id is NOT NULL, cannot be set to null) await queryRunner.query(` - UPDATE "freight"."route_milestones" - SET "yard_id" = NULL + DELETE FROM "freight"."route_milestones" WHERE "yard_id" IS NOT NULL AND "yard_id" NOT IN (SELECT "id" FROM "freight"."yards") `); - console.log('✅ Cleaned up orphaned route_milestones records'); + console.log('✅ Deleted orphaned route_milestones records'); } public async down(_queryRunner: QueryRunner): Promise { From e7e3d61975bb1bf486823804feb29892bae7f26b Mon Sep 17 00:00:00 2001 From: natib21 Date: Tue, 16 Jun 2026 06:45:14 +0000 Subject: [PATCH 4/7] 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 + } +} From ab99c96d62ace8a96e40721d16d9bcea51f561b0 Mon Sep 17 00:00:00 2001 From: natib21 Date: Tue, 16 Jun 2026 07:01:25 +0000 Subject: [PATCH 5/7] fix --- .../1718549600000-FixOrphanedBookings.ts | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 apps/edr-freight-api/src/migrations/1718549600000-FixOrphanedBookings.ts diff --git a/apps/edr-freight-api/src/migrations/1718549600000-FixOrphanedBookings.ts b/apps/edr-freight-api/src/migrations/1718549600000-FixOrphanedBookings.ts new file mode 100644 index 000000000..9599db72c --- /dev/null +++ b/apps/edr-freight-api/src/migrations/1718549600000-FixOrphanedBookings.ts @@ -0,0 +1,25 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class FixOrphanedBookings1718549600000 implements MigrationInterface { + public async up(queryRunner: QueryRunner): Promise { + // Delete orphaned bookings records with invalid origin_yard_id + await queryRunner.query(` + DELETE FROM "freight"."bookings" + 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"."bookings" + WHERE "destination_yard_id" IS NOT NULL + AND "destination_yard_id" NOT IN (SELECT "id" FROM "freight"."yards") + `); + + console.log('✅ Deleted orphaned bookings records'); + } + + public async down(_queryRunner: QueryRunner): Promise { + // No rollback needed for data cleanup + } +} From 9157e144ad2b6ca186fb71ac4c257f07f79b9afd Mon Sep 17 00:00:00 2001 From: natib21 Date: Tue, 16 Jun 2026 07:03:10 +0000 Subject: [PATCH 6/7] fix --- .../1718549600000-FixOrphanedBookings.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/apps/edr-freight-api/src/migrations/1718549600000-FixOrphanedBookings.ts b/apps/edr-freight-api/src/migrations/1718549600000-FixOrphanedBookings.ts index 9599db72c..a51adbaa0 100644 --- a/apps/edr-freight-api/src/migrations/1718549600000-FixOrphanedBookings.ts +++ b/apps/edr-freight-api/src/migrations/1718549600000-FixOrphanedBookings.ts @@ -2,6 +2,19 @@ import { MigrationInterface, QueryRunner } from 'typeorm'; export class FixOrphanedBookings1718549600000 implements MigrationInterface { public async up(queryRunner: QueryRunner): Promise { + // First, delete orphaned unloading_registrations that reference bookings with invalid yards + await queryRunner.query(` + DELETE FROM "freight"."unloading_registrations" + WHERE "booking_id" IN ( + SELECT b."id" FROM "freight"."bookings" b + WHERE ( + ("origin_yard_id" IS NOT NULL AND "origin_yard_id" NOT IN (SELECT "id" FROM "freight"."yards")) + OR + ("destination_yard_id" IS NOT NULL AND "destination_yard_id" NOT IN (SELECT "id" FROM "freight"."yards")) + ) + ) + `); + // Delete orphaned bookings records with invalid origin_yard_id await queryRunner.query(` DELETE FROM "freight"."bookings" @@ -16,7 +29,7 @@ export class FixOrphanedBookings1718549600000 implements MigrationInterface { AND "destination_yard_id" NOT IN (SELECT "id" FROM "freight"."yards") `); - console.log('✅ Deleted orphaned bookings records'); + console.log('✅ Deleted orphaned unloading_registrations and bookings records'); } public async down(_queryRunner: QueryRunner): Promise { From deae339887e88b362eaa8c3da728df863109741a Mon Sep 17 00:00:00 2001 From: natib21 Date: Tue, 16 Jun 2026 07:17:21 +0000 Subject: [PATCH 7/7] fix --- ...718549400000-FixOrphanedRouteMilestones.ts | 18 --------- .../1718549500000-FixOrphanedRoutes.ts | 25 ------------ .../1718549600000-FixOrphanedBookings.ts | 38 ------------------- 3 files changed, 81 deletions(-) delete mode 100644 apps/edr-freight-api/src/migrations/1718549400000-FixOrphanedRouteMilestones.ts delete mode 100644 apps/edr-freight-api/src/migrations/1718549500000-FixOrphanedRoutes.ts delete mode 100644 apps/edr-freight-api/src/migrations/1718549600000-FixOrphanedBookings.ts diff --git a/apps/edr-freight-api/src/migrations/1718549400000-FixOrphanedRouteMilestones.ts b/apps/edr-freight-api/src/migrations/1718549400000-FixOrphanedRouteMilestones.ts deleted file mode 100644 index c28836194..000000000 --- a/apps/edr-freight-api/src/migrations/1718549400000-FixOrphanedRouteMilestones.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { MigrationInterface, QueryRunner } from 'typeorm'; - -export class FixOrphanedRouteMilestones1718549400000 implements MigrationInterface { - public async up(queryRunner: QueryRunner): Promise { - // Delete orphaned route_milestones records (yard_id is NOT NULL, cannot be set to null) - await queryRunner.query(` - DELETE FROM "freight"."route_milestones" - WHERE "yard_id" IS NOT NULL - AND "yard_id" NOT IN (SELECT "id" FROM "freight"."yards") - `); - - console.log('✅ Deleted orphaned route_milestones records'); - } - - public async down(_queryRunner: QueryRunner): Promise { - // No rollback needed for data cleanup - } -} diff --git a/apps/edr-freight-api/src/migrations/1718549500000-FixOrphanedRoutes.ts b/apps/edr-freight-api/src/migrations/1718549500000-FixOrphanedRoutes.ts deleted file mode 100644 index bfd9994f3..000000000 --- a/apps/edr-freight-api/src/migrations/1718549500000-FixOrphanedRoutes.ts +++ /dev/null @@ -1,25 +0,0 @@ -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 - } -} diff --git a/apps/edr-freight-api/src/migrations/1718549600000-FixOrphanedBookings.ts b/apps/edr-freight-api/src/migrations/1718549600000-FixOrphanedBookings.ts deleted file mode 100644 index a51adbaa0..000000000 --- a/apps/edr-freight-api/src/migrations/1718549600000-FixOrphanedBookings.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { MigrationInterface, QueryRunner } from 'typeorm'; - -export class FixOrphanedBookings1718549600000 implements MigrationInterface { - public async up(queryRunner: QueryRunner): Promise { - // First, delete orphaned unloading_registrations that reference bookings with invalid yards - await queryRunner.query(` - DELETE FROM "freight"."unloading_registrations" - WHERE "booking_id" IN ( - SELECT b."id" FROM "freight"."bookings" b - WHERE ( - ("origin_yard_id" IS NOT NULL AND "origin_yard_id" NOT IN (SELECT "id" FROM "freight"."yards")) - OR - ("destination_yard_id" IS NOT NULL AND "destination_yard_id" NOT IN (SELECT "id" FROM "freight"."yards")) - ) - ) - `); - - // Delete orphaned bookings records with invalid origin_yard_id - await queryRunner.query(` - DELETE FROM "freight"."bookings" - 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"."bookings" - WHERE "destination_yard_id" IS NOT NULL - AND "destination_yard_id" NOT IN (SELECT "id" FROM "freight"."yards") - `); - - console.log('✅ Deleted orphaned unloading_registrations and bookings records'); - } - - public async down(_queryRunner: QueryRunner): Promise { - // No rollback needed for data cleanup - } -}