import { MigrationInterface, QueryRunner } from 'typeorm';

export class LeaveDocument1780980000000 implements MigrationInterface {
  name = 'LeaveDocument1780980000000';

  public async up(queryRunner: QueryRunner): Promise<void> {
    const table = await queryRunner.getTable('leaves');
    const hasDocument = table?.columns.some(
      (column) => column.name === 'document',
    );

    if (!hasDocument) {
      await queryRunner.query(
        `ALTER TABLE \`leaves\` ADD \`document\` json NULL AFTER \`reason\``,
      );
    }
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    const table = await queryRunner.getTable('leaves');
    const hasDocument = table?.columns.some(
      (column) => column.name === 'document',
    );

    if (hasDocument) {
      await queryRunner.query(`ALTER TABLE \`leaves\` DROP COLUMN \`document\``);
    }
  }
}
