From 9157e144ad2b6ca186fb71ac4c257f07f79b9afd Mon Sep 17 00:00:00 2001 From: natib21 Date: Tue, 16 Jun 2026 07:03:10 +0000 Subject: [PATCH] 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 {