Skip to content

Commit

Permalink
Run updates in batches
Browse files Browse the repository at this point in the history
  • Loading branch information
ethantkoenig committed May 19, 2017
1 parent 86e03ca commit 969c552
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 38 deletions.
47 changes: 20 additions & 27 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -921,48 +921,41 @@ func deleteUser(e *xorm.Session, u *User) error {
}

// ***** START: Watch *****
watches := make([]*Watch, 0, 10)
if err = e.Find(&watches, &Watch{UserID: u.ID}); err != nil {
watchedRepoIDs := make([]int64, 0, 10)
if err = e.Table("watch").Cols("watch.repo_id").
Where("watch.user_id = ?", u.ID).Find(&watchedRepoIDs); err != nil {
return fmt.Errorf("get all watches: %v", err)
}
for i := range watches {
if _, err = e.Exec("UPDATE `repository` SET num_watches=num_watches-1 WHERE id=?", watches[i].RepoID); err != nil {
return fmt.Errorf("decrease repository watch number[%d]: %v", watches[i].RepoID, err)
}
if _, err = e.Decr("num_watches").In("id", watchedRepoIDs).Update(new(Repository)); err != nil {
return fmt.Errorf("decrease repository num_watches: %v", err)
}
// ***** END: Watch *****

// ***** START: Star *****
stars := make([]*Star, 0, 10)
if err = e.Find(&stars, &Star{UID: u.ID}); err != nil {
starredRepoIDs := make([]int64, 0, 10)
if err = e.Table("star").Cols("star.repo_id").
Where("star.uid = ?", u.ID).Find(&starredRepoIDs); err != nil {
return fmt.Errorf("get all stars: %v", err)
}
for i := range stars {
if _, err = e.Exec("UPDATE `repository` SET num_stars=num_stars-1 WHERE id=?", stars[i].RepoID); err != nil {
return fmt.Errorf("decrease repository star number[%d]: %v", stars[i].RepoID, err)
}
} else if _, err = e.Decr("num_watches").In("id", starredRepoIDs).Update(new(Repository)); err != nil {
return fmt.Errorf("decrease repository num_stars: %v", err)
}
// ***** END: Star *****

// ***** START: Follow *****
followees := make([]*Follow, 0, 10)
if err = e.Find(&followees, &Follow{UserID: u.ID}); err != nil {
followeeIDs := make([]int64, 0, 10)
if err = e.Table("follow").Cols("follow.follow_id").
Where("follow.user_id = ?", u.ID).Find(&followeeIDs); err != nil {
return fmt.Errorf("get all followees: %v", err)
}
for i := range followees {
if _, err = e.Exec("UPDATE `user` SET num_followers=num_followers-1 WHERE id=?", followees[i].FollowID); err != nil {
return fmt.Errorf("decrease user follower number[%d]: %v", followees[i].FollowID, err)
}
} else if _, err = e.Decr("num_followers").In("id", followeeIDs).Update(new(User)); err != nil {
return fmt.Errorf("decrease user num_followers: %v", err)
}

followers := make([]*Follow, 0, 10)
if err = e.Find(&followers, &Follow{FollowID: u.ID}); err != nil {
followerIDs := make([]int64, 0, 10)
if err = e.Table("follow").Cols("follow.user_id").
Where("follow.follow_id = ?", u.ID).Find(&followerIDs); err != nil {
return fmt.Errorf("get all followers: %v", err)
}
for i := range followers {
if _, err = e.Exec("UPDATE `user` SET num_following=num_following-1 WHERE id=?", followers[i].UserID); err != nil {
return fmt.Errorf("decrease user following number[%d]: %v", followers[i].UserID, err)
}
} else if _, err = e.Decr("num_following").In("id", followerIDs).Update(new(User)); err != nil {
return fmt.Errorf("decrease user num_following: %v", err)
}
// ***** END: Follow *****

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -450,10 +450,10 @@
"revisionTime": "2016-11-01T11:13:14Z"
},
{
"checksumSHA1": "HHB+Jna1wv0cXLxtCyOnQqFwvn4=",
"checksumSHA1": "9SXbj96wb1PgppBZzxMIN0axbFQ=",
"path": "github.com/go-xorm/builder",
"revision": "c6e604e9c7b7461715091e14ad0c242ec44c26e4",
"revisionTime": "2017-02-24T04:30:50Z"
"revision": "043186300e9b2c22abdfc83567a979e3af04d9ae",
"revisionTime": "2017-05-18T21:58:56Z"
},
{
"checksumSHA1": "vt2CGANHLNXPAZ01ve3UlsgQ0uU=",
Expand Down

0 comments on commit 969c552

Please sign in to comment.