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

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

    const fromColumn = table.findColumnByName('from_address_id');
    const toColumn = table.findColumnByName('to_address_id');

    if (!fromColumn || fromColumn.length !== '36') {
      return;
    }

    await queryRunner.changeColumn(
      'transactions',
      'from_address_id',
      new TableColumn({
        name: 'from_address_id',
        type: 'varchar',
        length: '255',
        isNullable: true,
      }),
    );

    if (toColumn && toColumn.length === '36') {
      await queryRunner.changeColumn(
        'transactions',
        'to_address_id',
        new TableColumn({
          name: 'to_address_id',
          type: 'varchar',
          length: '255',
          isNullable: true,
        }),
      );
    }
  }

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

    const fromColumn = table.findColumnByName('from_address_id');
    const toColumn = table.findColumnByName('to_address_id');

    if (!fromColumn || fromColumn.length !== '255') {
      return;
    }

    await queryRunner.changeColumn(
      'transactions',
      'from_address_id',
      new TableColumn({
        name: 'from_address_id',
        type: 'varchar',
        length: '36',
        isNullable: true,
      }),
    );

    if (toColumn && toColumn.length === '255') {
      await queryRunner.changeColumn(
        'transactions',
        'to_address_id',
        new TableColumn({
          name: 'to_address_id',
          type: 'varchar',
          length: '36',
          isNullable: true,
        }),
      );
    }
  }
}
