Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce usage of allcols on update #2596

Merged
merged 2 commits into from
Sep 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions models/issue_milestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func ChangeMilestoneStatus(m *Milestone, isClosed bool) (err error) {

repo.NumMilestones = int(countRepoMilestones(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 sess.Commit()
Expand Down Expand Up @@ -341,7 +341,7 @@ func DeleteMilestoneByRepoID(repoID, id int64) error {

repo.NumMilestones = int(countRepoMilestones(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
}

Expand Down
2 changes: 1 addition & 1 deletion models/issue_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func UpdateIssueUsersByMentions(e Engine, issueID int64, uids []int64) error {

iu.IsMentioned = true
if has {
_, err = e.Id(iu.ID).AllCols().Update(iu)
_, err = e.Id(iu.ID).Cols("is_mentioned").Update(iu)
} else {
_, err = e.Insert(iu)
}
Expand Down
2 changes: 1 addition & 1 deletion models/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ func ChangeOrgUserStatus(orgID, uid int64, public bool) error {
}

ou.IsPublic = public
_, err = x.Id(ou.ID).AllCols().Update(ou)
_, err = x.Id(ou.ID).Cols("is_public").Update(ou)
return err
}

Expand Down
10 changes: 5 additions & 5 deletions models/org_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (t *Team) addRepository(e Engine, repo *Repository) (err error) {
}

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)
}

Expand Down Expand Up @@ -142,7 +142,7 @@ func (t *Team) removeRepository(e Engine, repo *Repository, recalculate bool) (e
}

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
}

Expand Down Expand Up @@ -521,7 +521,7 @@ func AddTeamMember(team *Team, userID int64) error {
if team.IsOwnerTeam() {
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
}

Expand Down Expand Up @@ -552,7 +552,7 @@ func removeTeamMember(e Engine, team *Team, userID int64) error {
return err
} else if _, err = e.
Id(team.ID).
AllCols().
Cols("num_members").
Update(team); err != nil {
return err
}
Expand All @@ -579,7 +579,7 @@ func removeTeamMember(e Engine, team *Team, userID int64) error {
}
if _, err = e.
Id(ou.ID).
AllCols().
Cols("num_teams").
Update(ou); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion models/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ func (pr *PullRequest) setMerged() (err error) {
if err = pr.Issue.changeStatus(sess, pr.Merger, pr.Issue.Repo, true); err != nil {
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)
}

Expand Down
2 changes: 1 addition & 1 deletion models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -1458,7 +1458,7 @@ func TransferOwnership(doer *User, newOwnerName string, repo *Repository) error
}

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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion models/repo_collaboration.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (repo *Repository) ChangeCollaborationAccessMode(uid int64, mode AccessMode

if _, err = sess.
Id(collaboration.ID).
AllCols().
Cols("mode").
Update(collaboration); err != nil {
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 {
Expand Down
10 changes: 1 addition & 9 deletions models/ssh_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,15 +477,8 @@ func ListPublicKeys(uid int64) ([]*PublicKey, error) {
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.
func UpdatePublicKeyUpdated(id int64) error {
now := time.Now()
// 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
if cnt, err := x.ID(id).Count(&PublicKey{}); err != nil {
Expand All @@ -495,8 +488,7 @@ func UpdatePublicKeyUpdated(id int64) error {
}

_, err := x.ID(id).Cols("updated_unix").Update(&PublicKey{
Updated: now,
UpdatedUnix: now.Unix(),
UpdatedUnix: time.Now().Unix(),
})
if err != nil {
return err
Expand Down
6 changes: 5 additions & 1 deletion models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,10 @@ func UpdateUser(u *User) error {

// UpdateUserCols update user according special columns
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
u.Email = strings.ToLower(u.Email)
if !u.IsOrganization() {
Expand All @@ -890,7 +894,7 @@ func UpdateUserCols(u *User, cols ...string) error {
u.Website = base.TruncateString(u.Website, 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
}

Expand Down
6 changes: 3 additions & 3 deletions models/user_mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ func (email *EmailAddress) Activate() error {
email.IsActivated = true
if _, err := sess.
Id(email.ID).
AllCols().
Cols("is_activated").
Update(email); err != nil {
return err
} else if err = updateUser(sess, user); err != nil {
} else if err = updateUserCols(sess, user, "rands"); err != nil {
return err
}

Expand Down Expand Up @@ -222,7 +222,7 @@ func MakeEmailPrimary(email *EmailAddress) error {
}

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
}

Expand Down