import { MigrationInterface, QueryRunner, Table, TableForeignKey } from 'typeorm'; /** * Create the freight.booking_container_allocations table — container-to-vehicle * allocation mapping for flexible routing of containers across available vehicles. */ export class CreateBookingContainerAllocations1825000000000 implements MigrationInterface { name = 'CreateBookingContainerAllocations1825000000000'; public async up(queryRunner: QueryRunner): Promise { const exists = await queryRunner.hasTable('freight.booking_container_allocations'); if (exists) return; await queryRunner.createTable( new Table({ name: 'freight.booking_container_allocations', columns: [ { name: 'id', type: 'uuid', isPrimary: true, default: 'gen_random_uuid()', }, { name: 'booking_id', type: 'uuid', isNullable: false }, { name: 'container_id', type: 'uuid', isNullable: false }, { name: 'vehicle_id', type: 'uuid', isNullable: true }, { name: 'container_type', type: 'text', isNullable: false, }, { name: 'quantity', type: 'integer', default: 1, isNullable: false, }, { name: 'created_at', type: 'timestamptz', default: 'now()' }, { name: 'updated_at', type: 'timestamptz', default: 'now()' }, { name: 'deleted_at', type: 'timestamptz', isNullable: true }, ], }), true, ); await queryRunner.createForeignKey( 'freight.booking_container_allocations', new TableForeignKey({ columnNames: ['booking_id'], referencedTableName: 'freight.bookings', referencedColumnNames: ['id'], onDelete: 'CASCADE', }), ); await queryRunner.createForeignKey( 'freight.booking_container_allocations', new TableForeignKey({ columnNames: ['vehicle_id'], referencedTableName: 'freight.vehicles', referencedColumnNames: ['id'], onDelete: 'SET NULL', }), ); await queryRunner.query( `CREATE INDEX "IDX_booking_container_allocations_booking_id" ON "freight"."booking_container_allocations" ("booking_id")`, ); await queryRunner.query( `CREATE INDEX "IDX_booking_container_allocations_vehicle_id" ON "freight"."booking_container_allocations" ("vehicle_id")`, ); } public async down(queryRunner: QueryRunner): Promise { const exists = await queryRunner.hasTable('freight.booking_container_allocations'); if (exists) { await queryRunner.dropTable('freight.booking_container_allocations'); } } }