@@ -1567,6 +1567,10 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
1567
1567
releaseAttachments = append (releaseAttachments , attachments [i ].LocalPath ())
1568
1568
}
1569
1569
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
+
1570
1574
if err = deleteBeans (sess ,
1571
1575
& Access {RepoID : repo .ID },
1572
1576
& Action {RepoID : repo .ID },
@@ -2332,3 +2336,38 @@ func updateRepositoryCols(e Engine, repo *Repository, cols ...string) error {
2332
2336
func UpdateRepositoryCols (repo * Repository , cols ... string ) error {
2333
2337
return updateRepositoryCols (x , repo , cols ... )
2334
2338
}
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