DO
$$
    DECLARE
        _path varchar(256) := 'patch-2025_11_26-ProjectTaskUserPerformer';
    BEGIN
        IF EXISTS (SELECT 1 FROM public."Versions" WHERE "Path" = _path) THEN
            RETURN;
        END IF;

        IF EXISTS (SELECT 1 FROM public."Versions" WHERE "Build" >= 15723) THEN
            RETURN;
        END IF;

        BEGIN

            insert into public."ProjectTaskUserPerformer" ("UserId","TaskId","ProjectId")
            select pt."PerformerId", pt."Id", pt."ProjectId" from public."ProjectTasks" pt
            where not exists (
            SELECT 1
                FROM public."ProjectTaskUserPerformer" ptp
                WHERE ptp."UserId" = pt."PerformerId" 
      	            AND ptp."TaskId" = pt."Id"
            )
            and pt."PerformerId" is not null and pt."State" <=4 and pt."PerformerId"  > 0;

        EXCEPTION
            WHEN OTHERS THEN
                RAISE NOTICE '[ERR] %: %', _path, SQLERRM;
        END;
    END;
$$;
