-- Недели календарного учебного графика выровнены по ISO-неделе:
-- понедельник — первый день, воскресенье — последний.
UPDATE academic_calendar_days AS calendar_day
SET week_number = (
    (
        calendar_day.date
        - academic_year.start_date
        + EXTRACT(ISODOW FROM academic_year.start_date)::INTEGER
        - 1
    ) / 7
) + 1
FROM academic_calendars AS calendar
JOIN academic_years AS academic_year
    ON academic_year.id = calendar.academic_year_id
WHERE calendar_day.calendar_id = calendar.id
  AND calendar_day.week_number IS DISTINCT FROM (
      (
          calendar_day.date
          - academic_year.start_date
          + EXTRACT(ISODOW FROM academic_year.start_date)::INTEGER
          - 1
      ) / 7
  ) + 1;
