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

export class AddStatusToDevices1786100653473 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    const table = await queryRunner.getTable('devices');
    if (table?.findColumnByName('status')) {
      return;
    }

    await queryRunner.addColumn(
      'devices',
      new TableColumn({
        name: 'status',
        type: 'enum',
        enum: ['active', 'background', 'closed'],
        default: "'closed'",
        isNullable: false,
      }),
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    const table = await queryRunner.getTable('devices');
    if (table?.findColumnByName('status')) {
      await queryRunner.dropColumn('devices', 'status');
    }
  }
}
