Add automatic cleanup job for orphaned topic counters (total_projects_count)
Problem
Topics can show incorrect project counts when projects are deleted but ProjectTopic records remain orphaned, causing topic.total_projects_count to be out of sync.
Current Behavior
-
Topic shows "X projects" in counter:
-
Clicking on topic shows empty site:
(potentially also fewer projects than counter indicates)
Validated discrepancy:
Expected Behavior
- Topic counters should accurately reflect the number of visible projects
- Automatic cleanup should handle orphaned ProjectTopic records
Proposed Solution
Add a scheduled cleanup job (similar to existing cleanup workers) that:
- Identifies orphaned ProjectTopic records
- Runs
Projects::Topic.reset_countersfor affected topics (topics wheretopic.total_projects_count != topic.projects.count) - Cleans up orphaned records
Related Code
-
Projects::ProjectTopicmodel withcounter_cache: :total_projects_count - Similar pattern exists in
Packages::Cleanup::DeleteOrphanedDependenciesWorker
Impact
- Improves data consistency
- Better user experience
- Reduces admin maintenance overhead
Edited by 🤖 GitLab Bot 🤖


