@@ -61,26 +61,20 @@ func asFixer(fn func(ctx context.Context) error) func(ctx context.Context) (int6
6161 }
6262}
6363
64- func genericOrphanCheck (name , subject , refobject , joincond string ) consistencyCheck {
64+ func genericOrphanCheck (name , subject , refObject , joinCond string ) consistencyCheck {
6565 return consistencyCheck {
6666 Name : name ,
6767 Counter : func (ctx context.Context ) (int64 , error ) {
68- return db .CountOrphanedObjects (ctx , subject , refobject , joincond )
68+ return db .CountOrphanedObjects (ctx , subject , refObject , joinCond )
6969 },
7070 Fixer : func (ctx context.Context ) (int64 , error ) {
71- err := db .DeleteOrphanedObjects (ctx , subject , refobject , joincond )
71+ err := db .DeleteOrphanedObjects (ctx , subject , refObject , joinCond )
7272 return - 1 , err
7373 },
7474 }
7575}
7676
77- func checkDBConsistency (ctx context.Context , logger log.Logger , autofix bool ) error {
78- // make sure DB version is uptodate
79- if err := db .InitEngineWithMigration (ctx , migrations .EnsureUpToDate ); err != nil {
80- logger .Critical ("Model version on the database does not match the current Gitea version. Model consistency will not be checked until the database is upgraded" )
81- return err
82- }
83-
77+ func prepareDBConsistencyChecks () []consistencyCheck {
8478 consistencyChecks := []consistencyCheck {
8579 {
8680 // find labels without existing repo or org
@@ -210,7 +204,7 @@ func checkDBConsistency(ctx context.Context, logger log.Logger, autofix bool) er
210204 "oauth2_grant" , "user" , "oauth2_grant.user_id=`user`.id" ),
211205 // find OAuth2Application without existing user
212206 genericOrphanCheck ("Orphaned OAuth2Application without existing User" ,
213- "oauth2_application" , "user" , "oauth2_application.uid=`user`.id" ),
207+ "oauth2_application" , "user" , "oauth2_application.uid=0 OR oauth2_application.uid= `user`.id" ),
214208 // find OAuth2AuthorizationCode without existing OAuth2Grant
215209 genericOrphanCheck ("Orphaned OAuth2AuthorizationCode without existing OAuth2Grant" ,
216210 "oauth2_authorization_code" , "oauth2_grant" , "oauth2_authorization_code.grant_id=oauth2_grant.id" ),
@@ -224,7 +218,16 @@ func checkDBConsistency(ctx context.Context, logger log.Logger, autofix bool) er
224218 genericOrphanCheck ("Orphaned Redirects without existing redirect user" ,
225219 "user_redirect" , "user" , "user_redirect.redirect_user_id=`user`.id" ),
226220 )
221+ return consistencyChecks
222+ }
227223
224+ func checkDBConsistency (ctx context.Context , logger log.Logger , autofix bool ) error {
225+ // make sure DB version is uptodate
226+ if err := db .InitEngineWithMigration (ctx , migrations .EnsureUpToDate ); err != nil {
227+ logger .Critical ("Model version on the database does not match the current Gitea version. Model consistency will not be checked until the database is upgraded" )
228+ return err
229+ }
230+ consistencyChecks := prepareDBConsistencyChecks ()
228231 for _ , c := range consistencyChecks {
229232 if err := c .Run (ctx , logger , autofix ); err != nil {
230233 return err
0 commit comments