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

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

    const blockColumn = table.findColumnByName('block');
    if (!blockColumn || blockColumn.type !== 'int') {
      return;
    }

    await queryRunner.changeColumn(
      'transactions',
      'block',
      new TableColumn({
        name: 'block',
        type: 'bigint',
        isNullable: true,
      }),
    );
  }

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

    const blockColumn = table.findColumnByName('block');
    if (!blockColumn || blockColumn.type !== 'bigint') {
      return;
    }

    await queryRunner.changeColumn(
      'transactions',
      'block',
      new TableColumn({
        name: 'block',
        type: 'int',
        isNullable: true,
      }),
    );
  }
}
