mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-09-07 20:05:41 +00:00
26 lines
910 B
TypeScript
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
|
|
}
|
|
}
|