import { MigrationInterface, QueryRunner, TableColumn } from 'typeorm';

/**
 * Fix for fresh deployments: AddWarehouseInspection1750000000003 runs before
 * the warehouse_inventory table exists, so it cannot add inspection_status.
 */
export class AddWarehouseInventoryInspectionStatusFix1791000000005 implements MigrationInterface {
  private readonly table = 'freight.warehouse_inventory';

  public async up(queryRunner: QueryRunner): Promise<void> {
    if ((await queryRunner.hasTable(this.table)) && !(await queryRunner.hasColumn(this.table, 'inspection_status'))) {
      await queryRunner.addColumn(
        this.table,
        new TableColumn({ name: 'inspection_status', type: 'varchar', length: '20', isNullable: true }),
      );
    }
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    if ((await queryRunner.hasTable(this.table)) && (await queryRunner.hasColumn(this.table, 'inspection_status'))) {
      await queryRunner.dropColumn(this.table, 'inspection_status');
    }
  }
}
