mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-23 22:01:22 +00:00
c5ac659d1b
Backport #28487 by @earl-warren
- When a repository is orphaned and has objects stored in any of the
storages such as repository avatar or attachments the delete function
would error, because the storage module wasn't initalized.
- Add code to initialize the storage module.
Refs: https://codeberg.org/forgejo/forgejo/pulls/1954
Co-authored-by: Earl Warren <109468362+earl-warren@users.noreply.github.com>
Co-authored-by: Gusted <postmaster@gusted.xyz>
(cherry picked from commit 8ee1ed877b
)
76 lines
2 KiB
Go
76 lines
2 KiB
Go
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package doctor
|
|
|
|
import (
|
|
"context"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
"code.gitea.io/gitea/modules/log"
|
|
"code.gitea.io/gitea/modules/storage"
|
|
repo_service "code.gitea.io/gitea/services/repository"
|
|
|
|
"xorm.io/builder"
|
|
)
|
|
|
|
func handleDeleteOrphanedRepos(ctx context.Context, logger log.Logger, autofix bool) error {
|
|
test := &consistencyCheck{
|
|
Name: "Repos with no existing owner",
|
|
Counter: countOrphanedRepos,
|
|
Fixer: deleteOrphanedRepos,
|
|
FixedMessage: "Deleted all content related to orphaned repos",
|
|
}
|
|
return test.Run(ctx, logger, autofix)
|
|
}
|
|
|
|
// countOrphanedRepos count repository where user of owner_id do not exist
|
|
func countOrphanedRepos(ctx context.Context) (int64, error) {
|
|
return db.CountOrphanedObjects(ctx, "repository", "user", "repository.owner_id=`user`.id")
|
|
}
|
|
|
|
// deleteOrphanedRepos delete repository where user of owner_id do not exist
|
|
func deleteOrphanedRepos(ctx context.Context) (int64, error) {
|
|
if err := storage.Init(); err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
batchSize := db.MaxBatchInsertSize("repository")
|
|
e := db.GetEngine(ctx)
|
|
var deleted int64
|
|
adminUser := &user_model.User{IsAdmin: true}
|
|
|
|
for {
|
|
var ids []int64
|
|
if err := e.Table("`repository`").
|
|
Join("LEFT", "`user`", "repository.owner_id=`user`.id").
|
|
Where(builder.IsNull{"`user`.id"}).
|
|
Select("`repository`.id").Limit(batchSize).Find(&ids); err != nil {
|
|
return deleted, err
|
|
}
|
|
|
|
// if we don't get ids we have deleted them all
|
|
if len(ids) == 0 {
|
|
return deleted, nil
|
|
}
|
|
|
|
for _, id := range ids {
|
|
if err := repo_service.DeleteRepositoryDirectly(ctx, adminUser, 0, id, true); err != nil {
|
|
return deleted, err
|
|
}
|
|
deleted++
|
|
}
|
|
}
|
|
}
|
|
|
|
func init() {
|
|
Register(&Check{
|
|
Title: "Deleted all content related to orphaned repos",
|
|
Name: "delete-orphaned-repos",
|
|
IsDefault: false,
|
|
Run: handleDeleteOrphanedRepos,
|
|
Priority: 4,
|
|
})
|
|
}
|