@@ -1567,6 +1567,10 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
15671567 releaseAttachments = append (releaseAttachments , attachments [i ].LocalPath ())
15681568 }
15691569
1570+ if _ , err = sess .Exec ("UPDATE `user` SET num_stars=num_stars-1 WHERE id IN (SELECT `uid` FROM `star` WHERE repo_id = ?)" , repo .ID ); err != nil {
1571+ return err
1572+ }
1573+
15701574 if err = deleteBeans (sess ,
15711575 & Access {RepoID : repo .ID },
15721576 & Action {RepoID : repo .ID },
@@ -2332,3 +2336,38 @@ func updateRepositoryCols(e Engine, repo *Repository, cols ...string) error {
23322336func UpdateRepositoryCols (repo * Repository , cols ... string ) error {
23332337 return updateRepositoryCols (x , repo , cols ... )
23342338}
2339+
2340+ // DoctorUserStarNum recalculate Stars number for all user
2341+ func DoctorUserStarNum () (err error ) {
2342+ const batchSize = 100
2343+ sess := x .NewSession ()
2344+ defer sess .Close ()
2345+
2346+ for start := 0 ; ; start += batchSize {
2347+ users := make ([]User , 0 , batchSize )
2348+ if err = sess .Limit (batchSize , start ).Where ("type = ?" , 0 ).Cols ("id" ).Find (& users ); err != nil {
2349+ return
2350+ }
2351+ if len (users ) == 0 {
2352+ break
2353+ }
2354+
2355+ if err = sess .Begin (); err != nil {
2356+ return
2357+ }
2358+
2359+ for _ , user := range users {
2360+ if _ , err = sess .Exec ("UPDATE `user` SET num_stars=(SELECT COUNT(*) FROM `star` WHERE uid=?) WHERE id=?" , user .ID , user .ID ); err != nil {
2361+ return
2362+ }
2363+ }
2364+
2365+ if err = sess .Commit (); err != nil {
2366+ return
2367+ }
2368+ }
2369+
2370+ log .Debug ("recalculate Stars number for all user finished" )
2371+
2372+ return
2373+ }
0 commit comments