import { MigrationInterface, QueryRunner } from 'typeorm';

/**
 * Contact email/phone for an external profile is sourced from IAM (the user's
 * identity) and from the company record, so the duplicated `email`/`phone`
 * columns on external_profiles are redundant and are dropped. Dropping `email`
 * also removes its UNIQUE constraint.
 */
export class DropEmailPhoneFromExternalProfiles1820000000011
  implements MigrationInterface
{
  name = 'DropEmailPhoneFromExternalProfiles1820000000011';

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `ALTER TABLE freight.external_profiles DROP COLUMN IF EXISTS email;`,
    );
    await queryRunner.query(
      `ALTER TABLE freight.external_profiles DROP COLUMN IF EXISTS phone;`,
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    // Re-added as nullable (the original email was UNIQUE NOT NULL) since the
    // dropped values cannot be recovered to satisfy those constraints.
    await queryRunner.query(
      `ALTER TABLE freight.external_profiles ADD COLUMN IF NOT EXISTS email varchar(150);`,
    );
    await queryRunner.query(
      `ALTER TABLE freight.external_profiles ADD COLUMN IF NOT EXISTS phone varchar(20);`,
    );
  }
}
