-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Add doctor dbconsistency fix to delete repos with no owner #27290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
cf8476a
Add doctor dbconsistency fix to delete repos with no owner
6543 2b6ee3c
Update modules/doctor/repository.go
6543 debbd16
...
6543 8c20759
check the error
6543 a228d72
Merge branch 'main' into doctor_delete_orphaned_repos
6543 cf00b1b
use dedicated optional arg instead
6543 55adbd1
Update modules/doctor/repository.go
6543 abc89b4
Update services/repository/delete.go
6543 8af3a99
Merge branch 'main' into doctor_delete_orphaned_repos
6543 6c19d75
Merge branch 'main' into doctor_delete_orphaned_repos
6543 14629dd
Update modules/doctor/dbconsistency.go
6543 7d90ee6
Merge branch 'main' into doctor_delete_orphaned_repos
6543 834de98
move to its own task
6543 a7735eb
Merge branch 'main' into doctor_delete_orphaned_repos
6543 5eb0357
Merge branch 'main' into doctor_delete_orphaned_repos
6543 f3bf88d
Merge branch 'main' into doctor_delete_orphaned_repos
6543 a43dfc9
Merge branch 'main' into doctor_delete_orphaned_repos
6543 31a6c90
Merge branch 'main' into doctor_delete_orphaned_repos
6543 04715f0
Merge branch 'main' into doctor_delete_orphaned_repos
6543 2e64308
Merge branch 'main' into doctor_delete_orphaned_repos
6543 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// 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" | ||
repo_service "code.gitea.io/gitea/services/repository" | ||
|
||
"xorm.io/builder" | ||
) | ||
|
||
// 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) { | ||
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 deleted them all | ||
6543 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if len(ids) == 0 { | ||
return deleted, nil | ||
} | ||
|
||
for _, id := range ids { | ||
if err := repo_service.DeleteRepositoryDirectly(ctx, adminUser, 0, id, true); err != nil { | ||
6543 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return deleted, err | ||
} | ||
deleted++ | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.