import { MigrationInterface, QueryRunner } from "typeorm";

/**
 * Enforce one driver record per verified Fayda identity. A unique index on
 * fayda_sub blocks a second driver from being created against the same Fayda
 * OIDC subject; NULLs stay distinct so legacy/unverified rows are unaffected.
 */
export class AddDriverFaydaSubUnique1890000000006 implements MigrationInterface {
  name = "AddDriverFaydaSubUnique1890000000006";

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
      CREATE UNIQUE INDEX IF NOT EXISTS "UQ_DRIVERS_FAYDA_SUB"
      ON freight.drivers (fayda_sub)
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
      DROP INDEX IF EXISTS freight."UQ_DRIVERS_FAYDA_SUB"
    `);
  }
}
