fix: solucionar bug de comparacion de fechas offset-naive y offset-aware en las politicas de retencion
This commit is contained in:
@@ -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
|
# Always keep the most recent backup regardless of age to avoid empty repo
|
||||||
protected_file_ids.add(backup_files[0].id)
|
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
|
# 1. Daily retention: Keep 1 backup per calendar day for the last `job.keep_daily` days
|
||||||
seen_days = set()
|
seen_days = set()
|
||||||
for bf in backup_files:
|
for bf in backup_files:
|
||||||
day_key = bf.created_at.strftime("%Y-%m-%d")
|
bf_created = ensure_utc(bf.created_at)
|
||||||
age_days = (now - bf.created_at).total_seconds() / 86400.0
|
day_key = bf_created.strftime("%Y-%m-%d")
|
||||||
|
age_days = (now - bf_created).total_seconds() / 86400.0
|
||||||
if age_days <= job.keep_daily:
|
if age_days <= job.keep_daily:
|
||||||
if day_key not in seen_days:
|
if day_key not in seen_days:
|
||||||
seen_days.add(day_key)
|
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
|
# 2. Weekly retention: Keep 1 backup per calendar week for the last `job.keep_weekly` weeks
|
||||||
seen_weeks = set()
|
seen_weeks = set()
|
||||||
for bf in backup_files:
|
for bf in backup_files:
|
||||||
week_key = f"{bf.created_at.year}-W{bf.created_at.isocalendar()[1]:02d}"
|
bf_created = ensure_utc(bf.created_at)
|
||||||
age_weeks = (now - bf.created_at).total_seconds() / (86400.0 * 7)
|
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 age_weeks <= job.keep_weekly:
|
||||||
if week_key not in seen_weeks:
|
if week_key not in seen_weeks:
|
||||||
seen_weeks.add(week_key)
|
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
|
# 3. Monthly retention: Keep 1 backup per calendar month for the last `job.keep_monthly` months
|
||||||
seen_months = set()
|
seen_months = set()
|
||||||
for bf in backup_files:
|
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)
|
# 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 age_months <= job.keep_monthly:
|
||||||
if month_key not in seen_months:
|
if month_key not in seen_months:
|
||||||
seen_months.add(month_key)
|
seen_months.add(month_key)
|
||||||
|
|||||||
Reference in New Issue
Block a user