Reduce usage of allcols on update (#2596)

* reduce usage of allcols on update

* fix bug and tests
release/v1.15
Lunny Xiao 2017-09-25 12:59:27 +08:00 committed by GitHub
parent 6b6f16cfae
commit dd55534b82
10 changed files with 21 additions and 25 deletions

View File

@ -227,7 +227,7 @@ func ChangeMilestoneStatus(m *Milestone, isClosed bool) (err error) {
repo.NumMilestones = int(countRepoMilestones(sess, repo.ID)) repo.NumMilestones = int(countRepoMilestones(sess, repo.ID))
repo.NumClosedMilestones = int(countRepoClosedMilestones(sess, repo.ID)) repo.NumClosedMilestones = int(countRepoClosedMilestones(sess, repo.ID))
if _, err = sess.Id(repo.ID).AllCols().Update(repo); err != nil { if _, err = sess.Id(repo.ID).Cols("num_milestones, num_closed_milestones").Update(repo); err != nil {
return err return err
} }
return sess.Commit() return sess.Commit()
@ -341,7 +341,7 @@ func DeleteMilestoneByRepoID(repoID, id int64) error {
repo.NumMilestones = int(countRepoMilestones(sess, repo.ID)) repo.NumMilestones = int(countRepoMilestones(sess, repo.ID))
repo.NumClosedMilestones = int(countRepoClosedMilestones(sess, repo.ID)) repo.NumClosedMilestones = int(countRepoClosedMilestones(sess, repo.ID))
if _, err = sess.Id(repo.ID).AllCols().Update(repo); err != nil { if _, err = sess.Id(repo.ID).Cols("num_milestones, num_closed_milestones").Update(repo); err != nil {
return err return err
} }

View File

@ -101,7 +101,7 @@ func UpdateIssueUsersByMentions(e Engine, issueID int64, uids []int64) error {
iu.IsMentioned = true iu.IsMentioned = true
if has { if has {
_, err = e.Id(iu.ID).AllCols().Update(iu) _, err = e.Id(iu.ID).Cols("is_mentioned").Update(iu)
} else { } else {
_, err = e.Insert(iu) _, err = e.Insert(iu)
} }

View File

@ -412,7 +412,7 @@ func ChangeOrgUserStatus(orgID, uid int64, public bool) error {
} }
ou.IsPublic = public ou.IsPublic = public
_, err = x.Id(ou.ID).AllCols().Update(ou) _, err = x.Id(ou.ID).Cols("is_public").Update(ou)
return err return err
} }

View File

@ -96,7 +96,7 @@ func (t *Team) addRepository(e Engine, repo *Repository) (err error) {
} }
t.NumRepos++ t.NumRepos++
if _, err = e.Id(t.ID).AllCols().Update(t); err != nil { if _, err = e.Id(t.ID).Cols("num_repos").Update(t); err != nil {
return fmt.Errorf("update team: %v", err) return fmt.Errorf("update team: %v", err)
} }
@ -142,7 +142,7 @@ func (t *Team) removeRepository(e Engine, repo *Repository, recalculate bool) (e
} }
t.NumRepos-- t.NumRepos--
if _, err = e.Id(t.ID).AllCols().Update(t); err != nil { if _, err = e.Id(t.ID).Cols("num_repos").Update(t); err != nil {
return err return err
} }
@ -521,7 +521,7 @@ func AddTeamMember(team *Team, userID int64) error {
if team.IsOwnerTeam() { if team.IsOwnerTeam() {
ou.IsOwner = true ou.IsOwner = true
} }
if _, err := sess.Id(ou.ID).AllCols().Update(ou); err != nil { if _, err := sess.Id(ou.ID).Cols("num_teams, is_owner").Update(ou); err != nil {
return err return err
} }
@ -552,7 +552,7 @@ func removeTeamMember(e Engine, team *Team, userID int64) error {
return err return err
} else if _, err = e. } else if _, err = e.
Id(team.ID). Id(team.ID).
AllCols(). Cols("num_members").
Update(team); err != nil { Update(team); err != nil {
return err return err
} }
@ -579,7 +579,7 @@ func removeTeamMember(e Engine, team *Team, userID int64) error {
} }
if _, err = e. if _, err = e.
Id(ou.ID). Id(ou.ID).
AllCols(). Cols("num_teams").
Update(ou); err != nil { Update(ou); err != nil {
return err return err
} }

View File

@ -425,7 +425,7 @@ func (pr *PullRequest) setMerged() (err error) {
if err = pr.Issue.changeStatus(sess, pr.Merger, pr.Issue.Repo, true); err != nil { if err = pr.Issue.changeStatus(sess, pr.Merger, pr.Issue.Repo, true); err != nil {
return fmt.Errorf("Issue.changeStatus: %v", err) return fmt.Errorf("Issue.changeStatus: %v", err)
} }
if _, err = sess.Id(pr.ID).AllCols().Update(pr); err != nil { if _, err = sess.Id(pr.ID).Cols("has_merged").Update(pr); err != nil {
return fmt.Errorf("update pull request: %v", err) return fmt.Errorf("update pull request: %v", err)
} }

View File

@ -1458,7 +1458,7 @@ func TransferOwnership(doer *User, newOwnerName string, repo *Repository) error
} }
t.NumRepos-- t.NumRepos--
if _, err := sess.Id(t.ID).AllCols().Update(t); err != nil { if _, err := sess.Id(t.ID).Cols("num_repos").Update(t); err != nil {
return fmt.Errorf("decrease team repository count '%d': %v", t.ID, err) return fmt.Errorf("decrease team repository count '%d': %v", t.ID, err)
} }
} }

View File

@ -143,7 +143,7 @@ func (repo *Repository) ChangeCollaborationAccessMode(uid int64, mode AccessMode
if _, err = sess. if _, err = sess.
Id(collaboration.ID). Id(collaboration.ID).
AllCols(). Cols("mode").
Update(collaboration); err != nil { Update(collaboration); err != nil {
return fmt.Errorf("update collaboration: %v", err) return fmt.Errorf("update collaboration: %v", err)
} else if _, err = sess.Exec("UPDATE access SET mode = ? WHERE user_id = ? AND repo_id = ?", mode, uid, repo.ID); err != nil { } else if _, err = sess.Exec("UPDATE access SET mode = ? WHERE user_id = ? AND repo_id = ?", mode, uid, repo.ID); err != nil {

View File

@ -477,15 +477,8 @@ func ListPublicKeys(uid int64) ([]*PublicKey, error) {
Find(&keys) Find(&keys)
} }
// UpdatePublicKey updates given public key.
func UpdatePublicKey(key *PublicKey) error {
_, err := x.Id(key.ID).AllCols().Update(key)
return err
}
// UpdatePublicKeyUpdated updates public key use time. // UpdatePublicKeyUpdated updates public key use time.
func UpdatePublicKeyUpdated(id int64) error { func UpdatePublicKeyUpdated(id int64) error {
now := time.Now()
// Check if key exists before update as affected rows count is unreliable // Check if key exists before update as affected rows count is unreliable
// and will return 0 affected rows if two updates are made at the same time // and will return 0 affected rows if two updates are made at the same time
if cnt, err := x.ID(id).Count(&PublicKey{}); err != nil { if cnt, err := x.ID(id).Count(&PublicKey{}); err != nil {
@ -495,8 +488,7 @@ func UpdatePublicKeyUpdated(id int64) error {
} }
_, err := x.ID(id).Cols("updated_unix").Update(&PublicKey{ _, err := x.ID(id).Cols("updated_unix").Update(&PublicKey{
Updated: now, UpdatedUnix: time.Now().Unix(),
UpdatedUnix: now.Unix(),
}) })
if err != nil { if err != nil {
return err return err

View File

@ -874,6 +874,10 @@ func UpdateUser(u *User) error {
// UpdateUserCols update user according special columns // UpdateUserCols update user according special columns
func UpdateUserCols(u *User, cols ...string) error { func UpdateUserCols(u *User, cols ...string) error {
return updateUserCols(x, u, cols...)
}
func updateUserCols(e Engine, u *User, cols ...string) error {
// Organization does not need email // Organization does not need email
u.Email = strings.ToLower(u.Email) u.Email = strings.ToLower(u.Email)
if !u.IsOrganization() { if !u.IsOrganization() {
@ -890,7 +894,7 @@ func UpdateUserCols(u *User, cols ...string) error {
u.Website = base.TruncateString(u.Website, 255) u.Website = base.TruncateString(u.Website, 255)
u.Description = base.TruncateString(u.Description, 255) u.Description = base.TruncateString(u.Description, 255)
_, err := x.Id(u.ID).Cols(cols...).Update(u) _, err := e.Id(u.ID).Cols(cols...).Update(u)
return err return err
} }

View File

@ -135,10 +135,10 @@ func (email *EmailAddress) Activate() error {
email.IsActivated = true email.IsActivated = true
if _, err := sess. if _, err := sess.
Id(email.ID). Id(email.ID).
AllCols(). Cols("is_activated").
Update(email); err != nil { Update(email); err != nil {
return err return err
} else if err = updateUser(sess, user); err != nil { } else if err = updateUserCols(sess, user, "rands"); err != nil {
return err return err
} }
@ -222,7 +222,7 @@ func MakeEmailPrimary(email *EmailAddress) error {
} }
user.Email = email.Email user.Email = email.Email
if _, err = sess.Id(user.ID).AllCols().Update(user); err != nil { if _, err = sess.Id(user.ID).Cols("email").Update(user); err != nil {
return err return err
} }