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

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

    const hasColumn = table.findColumnByName('push_token');
    if (hasColumn) {
      return;
    }

    await queryRunner.addColumn(
      'devices',
      new TableColumn({
        name: 'push_token',
        type: 'varchar',
        length: '255',
        isNullable: true,
      }),
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    const table = await queryRunner.getTable('devices');
    if (!table) {
      return;
    }

    const hasColumn = table.findColumnByName('push_token');
    if (!hasColumn) {
      return;
    }

    await queryRunner.dropColumn('devices', 'push_token');
  }
}
