Files
edr-platform/apps/edr-freight-api/src/migrations/1718549600000-FixOrphanedBookings.ts
natib21 ab99c96d62 fix
2026-06-16 07:01:25 +00:00

26 lines
910 B
TypeScript

import { MigrationInterface, QueryRunner } from 'typeorm';
export class FixOrphanedBookings1718549600000 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
// 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<void> {
// No rollback needed for data cleanup
}
}