import { Migration } from "@mikro-orm/migrations";
class Migration20260610120000 extends Migration {
  up() {
    this.addSql(`
      with ranked as (
        select id,
               row_number() over (
                 partition by tenant_id, email_hash
                 order by coalesce(updated_at, created_at) desc, created_at desc, id desc
               ) as rn
        from users
        where deleted_at is null and email_hash is not null
      )
      update users
      set deleted_at = now()
      from ranked
      where users.id = ranked.id and ranked.rn > 1;
    `);
    this.addSql(`alter table "users" drop constraint if exists "users_email_unique";`);
    this.addSql(`create unique index if not exists "users_tenant_email_hash_uniq" on "users" ("tenant_id", "email_hash") where "deleted_at" is null and "email_hash" is not null;`);
  }
  down() {
    this.addSql(`drop index if exists "users_tenant_email_hash_uniq";`);
    this.addSql(`alter table "users" add constraint "users_email_unique" unique ("email");`);
  }
}
export {
  Migration20260610120000
};
//# sourceMappingURL=Migration20260610120000.js.map
