-- update duplicate dossier names
update dossier d1
set dossier_name = (select case
                               when cnt = 1 then dossier_name
                               else dossier_name || ' ' || rn
                               end as dossier_name
                    from (select *, row_number() over w rn, count(*) over w cnt
                          from dossier
                              window w as (partition by dossier_name)) t
                    where t.id = d1.id);

-- create unique index on dossier_name and hard_deleted_time
CREATE UNIQUE INDEX dossier_name_index ON dossier (dossier_name, (hard_deleted_time IS NULL)) WHERE hard_deleted_time IS NULL;
