From f93a31dcf9cb4cb63bdafb0371b5683255e8c5c9 Mon Sep 17 00:00:00 2001 From: Carlos Tello Date: Fri, 14 Aug 2026 21:55:09 -0300 Subject: [PATCH] fix: solucionar bug de comparacion de fechas offset-naive y offset-aware en las politicas de retencion --- backend/app/services/retention_service.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/backend/app/services/retention_service.py b/backend/app/services/retention_service.py index a9ddd14..f87ab9f 100644 --- a/backend/app/services/retention_service.py +++ b/backend/app/services/retention_service.py @@ -35,11 +35,17 @@ async def apply_retention_policy(db: AsyncSession, job_id: int) -> int: # Always keep the most recent backup regardless of age to avoid empty repo protected_file_ids.add(backup_files[0].id) + def ensure_utc(dt: datetime) -> datetime: + if dt.tzinfo is None: + return dt.replace(tzinfo=timezone.utc) + return dt.astimezone(timezone.utc) + # 1. Daily retention: Keep 1 backup per calendar day for the last `job.keep_daily` days seen_days = set() for bf in backup_files: - day_key = bf.created_at.strftime("%Y-%m-%d") - age_days = (now - bf.created_at).total_seconds() / 86400.0 + bf_created = ensure_utc(bf.created_at) + day_key = bf_created.strftime("%Y-%m-%d") + age_days = (now - bf_created).total_seconds() / 86400.0 if age_days <= job.keep_daily: if day_key not in seen_days: seen_days.add(day_key) @@ -48,8 +54,9 @@ async def apply_retention_policy(db: AsyncSession, job_id: int) -> int: # 2. Weekly retention: Keep 1 backup per calendar week for the last `job.keep_weekly` weeks seen_weeks = set() for bf in backup_files: - week_key = f"{bf.created_at.year}-W{bf.created_at.isocalendar()[1]:02d}" - age_weeks = (now - bf.created_at).total_seconds() / (86400.0 * 7) + bf_created = ensure_utc(bf.created_at) + week_key = f"{bf_created.year}-W{bf_created.isocalendar()[1]:02d}" + age_weeks = (now - bf_created).total_seconds() / (86400.0 * 7) if age_weeks <= job.keep_weekly: if week_key not in seen_weeks: seen_weeks.add(week_key) @@ -58,9 +65,10 @@ async def apply_retention_policy(db: AsyncSession, job_id: int) -> int: # 3. Monthly retention: Keep 1 backup per calendar month for the last `job.keep_monthly` months seen_months = set() for bf in backup_files: - month_key = bf.created_at.strftime("%Y-%m") + bf_created = ensure_utc(bf.created_at) + month_key = bf_created.strftime("%Y-%m") # Approximate age in months (30 days per month) - age_months = (now - bf.created_at).total_seconds() / (86400.0 * 30.4375) + age_months = (now - bf_created).total_seconds() / (86400.0 * 30.4375) if age_months <= job.keep_monthly: if month_key not in seen_months: seen_months.add(month_key)