import { MigrationInterface, QueryRunner } from 'typeorm';

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

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

    if (!hasColumn) {
      await queryRunner.query(`
        ALTER TABLE \`working_schedule\`
          ADD COLUMN \`monthly_correction_limit\` INT NOT NULL DEFAULT 2 AFTER \`timezone\`
      `);
    }
  }

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

    if (hasColumn) {
      await queryRunner.query(`
        ALTER TABLE \`working_schedule\` DROP COLUMN \`monthly_correction_limit\`
      `);
    }
  }
}
