import { MigrationInterface, QueryRunner } from 'typeorm';

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

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

    if (!hasColumn) {
      await queryRunner.query(`
        ALTER TABLE \`onboarding_tasks\` ADD COLUMN \`plan_task_id\` varchar(191) NULL AFTER \`record_id\`;
      `);
    }
  }

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

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