import { MigrationInterface, QueryRunner } from 'typeorm';

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

    // Keep the column that the entity maps to (is_native) and drop the
    // duplicate camelCase column if it exists.
    if (table.findColumnByName('is_native') && table.findColumnByName('isNative')) {
      await queryRunner.dropColumn('tokens', 'isNative');
    }
  }

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

    if (table.findColumnByName('is_native') && !table.findColumnByName('isNative')) {
      await queryRunner.query('ALTER TABLE tokens ADD COLUMN isNative tinyint(1) NOT NULL DEFAULT 0');
    }
  }
}
