import { MigrationInterface, QueryRunner } from "typeorm";

export class AddNationalityToCompanies1791000000002
  implements MigrationInterface
{
  name = "AddNationalityToCompanies1791000000002";

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
      ALTER TABLE freight.companies
        ADD COLUMN IF NOT EXISTS nationality varchar(32);
    `);

    // Existing companies default to Ethiopian (country defaults to Ethiopia).
    await queryRunner.query(`
      UPDATE freight.companies
        SET nationality = 'ethiopian'
        WHERE nationality IS NULL;
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
      ALTER TABLE freight.companies
        DROP COLUMN IF EXISTS nationality;
    `);
  }
}
