relay/app/feeds/migrations/0006_feed_last_successful_fetch.py
Andros Fenollosa 390561fffe Add automatic cleanup of stale feeds
This commit implements automatic cleanup of feeds that haven't been successfully fetched in 3 days.

Changes:
- Add last_successful_fetch field to Feed model to track when feeds are successfully fetched (HTTP 200)
- Update parse_org_social() and validate_org_social_feed() to record successful fetches
- Add new periodic task cleanup_stale_feeds() that runs every 3 days and deletes inactive feeds
- Add comprehensive test suite (9 tests, all passing)
- Migration safely handles existing feeds by setting initial last_successful_fetch to protect them

Safety features:
- Feeds with last_successful_fetch = NULL are never deleted (protects legacy feeds)
- Only feeds older than 3 days are deleted
- Extensive logging of cleanup operations
- Initial migration sets timestamp for all existing feeds to prevent accidental deletion

🤖 Generated with [Claude Code](https://claude.com/claude-code)
2025-10-11 10:53:29 +02:00

34 lines
1,014 B
Python

# Generated by Django 5.2.6 on 2025-10-11 08:49
from django.db import migrations, models
from django.utils import timezone
def set_initial_last_successful_fetch(apps, schema_editor):
"""
Set last_successful_fetch to current time for all existing feeds.
This prevents existing feeds from being deleted on first cleanup run.
"""
Feed = apps.get_model("feeds", "Feed")
Feed.objects.all().update(last_successful_fetch=timezone.now())
class Migration(migrations.Migration):
dependencies = [
("feeds", "0005_post_group"),
]
operations = [
migrations.AddField(
model_name="feed",
name="last_successful_fetch",
field=models.DateTimeField(
blank=True,
help_text="Last time this feed was successfully fetched with HTTP 200",
null=True,
),
),
migrations.RunPython(
set_initial_last_successful_fetch, migrations.RunPython.noop
),
]