import { MigrationInterface, QueryRunner } from 'typeorm';

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

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `ALTER TABLE "transaction_demographies" DROP COLUMN "local"`,
    );
    await queryRunner.query(
      `ALTER TABLE "transaction_demographies" DROP COLUMN "foreign"`,
    );
    await queryRunner.query(
      `CREATE TYPE "public"."transaction_demographies_nationality_enum" AS ENUM('local', 'foreign', 'mix')`,
    );
    await queryRunner.query(
      `ALTER TABLE "transaction_demographies" ADD "nationality" "public"."transaction_demographies_nationality_enum" NOT NULL DEFAULT 'local'`,
    );
    await queryRunner.query(
      `ALTER TABLE "transaction_demographies" DROP CONSTRAINT "FK_a2b705884bca06c148e3b35ab04"`,
    );
    await queryRunner.query(
      `ALTER TABLE "transaction_demographies" ADD CONSTRAINT "FK_a2b705884bca06c148e3b35ab04" FOREIGN KEY ("transaction_id") REFERENCES "transactions"("id") ON DELETE CASCADE ON UPDATE CASCADE`,
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `ALTER TABLE "transaction_demographies" DROP CONSTRAINT "FK_a2b705884bca06c148e3b35ab04"`,
    );
    await queryRunner.query(
      `ALTER TABLE "transaction_demographies" ADD CONSTRAINT "FK_a2b705884bca06c148e3b35ab04" FOREIGN KEY ("transaction_id") REFERENCES "transactions"("id") ON DELETE CASCADE ON UPDATE CASCADE`,
    );
    await queryRunner.query(
      `ALTER TABLE "transaction_demographies" DROP COLUMN "nationality"`,
    );
    await queryRunner.query(
      `DROP TYPE "public"."transaction_demographies_nationality_enum"`,
    );
    await queryRunner.query(
      `ALTER TABLE "transaction_demographies" ADD "foreign" integer NOT NULL DEFAULT '0'`,
    );
    await queryRunner.query(
      `ALTER TABLE "transaction_demographies" ADD "local" integer NOT NULL DEFAULT '0'`,
    );
  }
}
