Browse Source

Reduce usage of allcols on update (#2596)

* reduce usage of allcols on update

* fix bug and tests
Lunny Xiao 1 year ago
parent
commit
dd55534b82

+ 2 - 2
models/issue_milestone.go

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

+ 1 - 1
models/issue_user.go

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

+ 1 - 1
models/org.go

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

+ 5 - 5
models/org_team.go

@@ -96,7 +96,7 @@ func (t *Team) addRepository(e Engine, repo *Repository) (err error) {
96 96
 	}
97 97
 
98 98
 	t.NumRepos++
99
-	if _, err = e.Id(t.ID).AllCols().Update(t); err != nil {
99
+	if _, err = e.Id(t.ID).Cols("num_repos").Update(t); err != nil {
100 100
 		return fmt.Errorf("update team: %v", err)
101 101
 	}
102 102
 
@@ -142,7 +142,7 @@ func (t *Team) removeRepository(e Engine, repo *Repository, recalculate bool) (e
142 142
 	}
143 143
 
144 144
 	t.NumRepos--
145
-	if _, err = e.Id(t.ID).AllCols().Update(t); err != nil {
145
+	if _, err = e.Id(t.ID).Cols("num_repos").Update(t); err != nil {
146 146
 		return err
147 147
 	}
148 148
 
@@ -521,7 +521,7 @@ func AddTeamMember(team *Team, userID int64) error {
521 521
 	if team.IsOwnerTeam() {
522 522
 		ou.IsOwner = true
523 523
 	}
524
-	if _, err := sess.Id(ou.ID).AllCols().Update(ou); err != nil {
524
+	if _, err := sess.Id(ou.ID).Cols("num_teams, is_owner").Update(ou); err != nil {
525 525
 		return err
526 526
 	}
527 527
 
@@ -552,7 +552,7 @@ func removeTeamMember(e Engine, team *Team, userID int64) error {
552 552
 		return err
553 553
 	} else if _, err = e.
554 554
 		Id(team.ID).
555
-		AllCols().
555
+		Cols("num_members").
556 556
 		Update(team); err != nil {
557 557
 		return err
558 558
 	}
@@ -579,7 +579,7 @@ func removeTeamMember(e Engine, team *Team, userID int64) error {
579 579
 	}
580 580
 	if _, err = e.
581 581
 		Id(ou.ID).
582
-		AllCols().
582
+		Cols("num_teams").
583 583
 		Update(ou); err != nil {
584 584
 		return err
585 585
 	}

+ 1 - 1
models/pull.go

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

+ 1 - 1
models/repo.go

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

+ 1 - 1
models/repo_collaboration.go

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

+ 1 - 9
models/ssh_key.go

@@ -477,15 +477,8 @@ func ListPublicKeys(uid int64) ([]*PublicKey, error) {
477 477
 		Find(&keys)
478 478
 }
479 479
 
480
-// UpdatePublicKey updates given public key.
481
-func UpdatePublicKey(key *PublicKey) error {
482
-	_, err := x.Id(key.ID).AllCols().Update(key)
483
-	return err
484
-}
485
-
486 480
 // UpdatePublicKeyUpdated updates public key use time.
487 481
 func UpdatePublicKeyUpdated(id int64) error {
488
-	now := time.Now()
489 482
 	// Check if key exists before update as affected rows count is unreliable
490 483
 	//    and will return 0 affected rows if two updates are made at the same time
491 484
 	if cnt, err := x.ID(id).Count(&PublicKey{}); err != nil {
@@ -495,8 +488,7 @@ func UpdatePublicKeyUpdated(id int64) error {
495 488
 	}
496 489
 
497 490
 	_, err := x.ID(id).Cols("updated_unix").Update(&PublicKey{
498
-		Updated:     now,
499
-		UpdatedUnix: now.Unix(),
491
+		UpdatedUnix: time.Now().Unix(),
500 492
 	})
501 493
 	if err != nil {
502 494
 		return err

+ 5 - 1
models/user.go

@@ -874,6 +874,10 @@ func UpdateUser(u *User) error {
874 874
 
875 875
 // UpdateUserCols update user according special columns
876 876
 func UpdateUserCols(u *User, cols ...string) error {
877
+	return updateUserCols(x, u, cols...)
878
+}
879
+
880
+func updateUserCols(e Engine, u *User, cols ...string) error {
877 881
 	// Organization does not need email
878 882
 	u.Email = strings.ToLower(u.Email)
879 883
 	if !u.IsOrganization() {
@@ -890,7 +894,7 @@ func UpdateUserCols(u *User, cols ...string) error {
890 894
 	u.Website = base.TruncateString(u.Website, 255)
891 895
 	u.Description = base.TruncateString(u.Description, 255)
892 896
 
893
-	_, err := x.Id(u.ID).Cols(cols...).Update(u)
897
+	_, err := e.Id(u.ID).Cols(cols...).Update(u)
894 898
 	return err
895 899
 }
896 900
 

+ 3 - 3
models/user_mail.go

@@ -135,10 +135,10 @@ func (email *EmailAddress) Activate() error {
135 135
 	email.IsActivated = true
136 136
 	if _, err := sess.
137 137
 		Id(email.ID).
138
-		AllCols().
138
+		Cols("is_activated").
139 139
 		Update(email); err != nil {
140 140
 		return err
141
-	} else if err = updateUser(sess, user); err != nil {
141
+	} else if err = updateUserCols(sess, user, "rands"); err != nil {
142 142
 		return err
143 143
 	}
144 144
 
@@ -222,7 +222,7 @@ func MakeEmailPrimary(email *EmailAddress) error {
222 222
 	}
223 223
 
224 224
 	user.Email = email.Email
225
-	if _, err = sess.Id(user.ID).AllCols().Update(user); err != nil {
225
+	if _, err = sess.Id(user.ID).Cols("email").Update(user); err != nil {
226 226
 		return err
227 227
 	}
228 228