Skip to content

Commit 1739d53

Browse files
earl-warrenGusted
authored andcommitted
Add orphaned topic consistency check (go-gitea#28507)
- If a topic has zero repository count, it means that none of the repositories are using that topic, that would make them 'useless' to keep. One caveat is that if that topic is going to be used in the future, it will be added again to the database, but simply with a new ID. Refs: https://codeberg.org/forgejo/forgejo/pulls/1964 Co-authored-by: Gusted <[email protected]>
1 parent 764b3b7 commit 1739d53

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

models/repo/topic.go

+10
Original file line numberDiff line numberDiff line change
@@ -377,3 +377,13 @@ func syncTopicsInRepository(sess db.Engine, repoID int64) error {
377377
}
378378
return nil
379379
}
380+
381+
// CountOrphanedAttachments returns the number of topics that don't belong to any repository.
382+
func CountOrphanedTopics(ctx context.Context) (int64, error) {
383+
return db.GetEngine(ctx).Where("repo_count = 0").Count(new(Topic))
384+
}
385+
386+
// DeleteOrphanedAttachments delete all topics that don't belong to any repository.
387+
func DeleteOrphanedTopics(ctx context.Context) (int64, error) {
388+
return db.GetEngine(ctx).Where("repo_count = 0").Delete(new(Topic))
389+
}

modules/doctor/dbconsistency.go

+6
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ func checkDBConsistency(ctx context.Context, logger log.Logger, autofix bool) er
158158
Fixer: actions_model.FixRunnersWithoutBelongingOwner,
159159
FixedMessage: "Removed",
160160
},
161+
{
162+
Name: "Topics with empty repository count",
163+
Counter: repo_model.CountOrphanedTopics,
164+
Fixer: repo_model.DeleteOrphanedTopics,
165+
FixedMessage: "Removed",
166+
},
161167
}
162168

163169
// TODO: function to recalc all counters

0 commit comments

Comments
 (0)