DO $$
DECLARE 
    _path character varying(256) := 'patch-2025-10-31-FixEvents';
BEGIN
    IF EXISTS (SELECT "Id" FROM public."Versions" WHERE "Path" = _path) THEN
        RETURN;
    END IF;

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

    BEGIN
      UPDATE  public."EventRecurring"  SET "MonthNumber"=EXTRACT(MONTH FROM "StartDate")
where  "Id" IN(SELECT er."Id" from public."EventRecurring" er
INNER JOIN public."EventDetails" ed  ON ed."RecurringId"=er."Id" 
where  er."MonthNumber"=0 And ed."Type"=4);

        	INSERT INTO public."Versions" ("Path", "Build") VALUES (_path, 0);
    END;
END;
$$;