Skip to content

Commit 7ee15fd

Browse files
committed
replace update repository function in some places
1 parent 7149c9c commit 7ee15fd

File tree

11 files changed

+104
-36
lines changed

11 files changed

+104
-36
lines changed

models/issues/pull.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -649,12 +649,6 @@ func GetAllUnmergedAgitPullRequestByPoster(ctx context.Context, uid int64) ([]*P
649649
return pulls, err
650650
}
651651

652-
// Update updates all fields of pull request.
653-
func (pr *PullRequest) Update(ctx context.Context) error {
654-
_, err := db.GetEngine(ctx).ID(pr.ID).AllCols().Update(pr)
655-
return err
656-
}
657-
658652
// UpdateCols updates specific fields of pull request.
659653
func (pr *PullRequest) UpdateCols(ctx context.Context, cols ...string) error {
660654
_, err := db.GetEngine(ctx).ID(pr.ID).Cols(cols...).Update(pr)

models/issues/pull_test.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -248,19 +248,6 @@ func TestGetPullRequestByIssueID(t *testing.T) {
248248
assert.True(t, issues_model.IsErrPullRequestNotExist(err))
249249
}
250250

251-
func TestPullRequest_Update(t *testing.T) {
252-
assert.NoError(t, unittest.PrepareTestDatabase())
253-
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1})
254-
pr.BaseBranch = "baseBranch"
255-
pr.HeadBranch = "headBranch"
256-
pr.Update(db.DefaultContext)
257-
258-
pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: pr.ID})
259-
assert.Equal(t, "baseBranch", pr.BaseBranch)
260-
assert.Equal(t, "headBranch", pr.HeadBranch)
261-
unittest.CheckConsistencyFor(t, pr)
262-
}
263-
264251
func TestPullRequest_UpdateCols(t *testing.T) {
265252
assert.NoError(t, unittest.PrepareTestDatabase())
266253
pr := &issues_model.PullRequest{

models/repo/release.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func AddReleaseAttachments(ctx context.Context, releaseID int64, attachmentUUIDs
180180
}
181181
attachments[i].ReleaseID = releaseID
182182
// No assign value could be 0, so ignore AllCols().
183-
if _, err = db.GetEngine(ctx).ID(attachments[i].ID).Update(attachments[i]); err != nil {
183+
if _, err = db.GetEngine(ctx).ID(attachments[i].ID).Cols("release_id").Update(attachments[i]); err != nil {
184184
return fmt.Errorf("update attachment [%d]: %w", attachments[i].ID, err)
185185
}
186186
}

models/repo/repo.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,3 +992,11 @@ func UpdateRepositoryOwnerName(ctx context.Context, oldUserName, newUserName str
992992
}
993993
return nil
994994
}
995+
996+
func UpdateRepositoryForksNum(ctx context.Context, repoID int64) error {
997+
// Update the forks count of the repository
998+
if _, err := db.GetEngine(ctx).Exec("UPDATE `repository` SET num_forks = (SELECT COUNT(*) FROM `repository` WHERE fork_id = ?) WHERE id = ?", repoID, repoID); err != nil {
999+
return fmt.Errorf("update repository forks num: %w", err)
1000+
}
1001+
return nil
1002+
}

models/repo/update.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,18 @@ func UpdateRepositoryUpdatedTime(ctx context.Context, repoID int64, updateTime t
4242

4343
// UpdateRepositoryColsWithAutoTime updates repository's columns
4444
func UpdateRepositoryColsWithAutoTime(ctx context.Context, repo *Repository, cols ...string) error {
45+
if len(cols) == 0 {
46+
return nil
47+
}
4548
_, err := db.GetEngine(ctx).ID(repo.ID).Cols(cols...).Update(repo)
4649
return err
4750
}
4851

4952
// UpdateRepositoryColsNoAutoTime updates repository's columns and but applies time change automatically
5053
func UpdateRepositoryColsNoAutoTime(ctx context.Context, repo *Repository, cols ...string) error {
54+
if len(cols) == 0 {
55+
return nil
56+
}
5157
_, err := db.GetEngine(ctx).ID(repo.ID).Cols(cols...).NoAutoTime().Update(repo)
5258
return err
5359
}

services/repository/adopt.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,9 @@ func adoptRepository(ctx context.Context, repo *repo_model.Repository, defaultBr
196196
return fmt.Errorf("setDefaultBranch: %w", err)
197197
}
198198
}
199-
if err = updateRepository(ctx, repo, false); err != nil {
200-
return fmt.Errorf("updateRepository: %w", err)
199+
200+
if err = repo_model.UpdateRepositoryColsNoAutoTime(ctx, repo, "is_empty", "default_branch"); err != nil {
201+
return fmt.Errorf("UpdateRepositoryCols: %w", err)
201202
}
202203

203204
return nil

services/repository/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func initRepository(ctx context.Context, u *user_model.User, repo *repo_model.Re
191191
}
192192
}
193193

194-
if err = UpdateRepository(ctx, repo, false); err != nil {
194+
if err = repo_model.UpdateRepositoryColsNoAutoTime(ctx, repo, "is_empty", "default_branch", "default_wiki_branch"); err != nil {
195195
return fmt.Errorf("updateRepository: %w", err)
196196
}
197197

services/repository/fork.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func ForkRepository(ctx context.Context, doer, owner *user_model.User, opts Fork
209209

210210
// ConvertForkToNormalRepository convert the provided repo from a forked repo to normal repo
211211
func ConvertForkToNormalRepository(ctx context.Context, repo *repo_model.Repository) error {
212-
err := db.WithTx(ctx, func(ctx context.Context) error {
212+
return db.WithTx(ctx, func(ctx context.Context) error {
213213
repo, err := repo_model.GetRepositoryByID(ctx, repo.ID)
214214
if err != nil {
215215
return err
@@ -224,18 +224,15 @@ func ConvertForkToNormalRepository(ctx context.Context, repo *repo_model.Reposit
224224
return err
225225
}
226226

227+
parentRepoID := repo.ForkID
227228
repo.IsFork = false
228229
repo.ForkID = 0
229-
230-
if err := updateRepository(ctx, repo, false); err != nil {
231-
log.Error("Unable to update repository %-v whilst converting from fork. Error: %v", repo, err)
230+
if err := repo_model.UpdateRepositoryColsNoAutoTime(ctx, repo, "is_fork", "fork_id"); err != nil {
232231
return err
233232
}
234233

235-
return nil
234+
return repo_model.UpdateRepositoryForksNum(ctx, parentRepoID)
236235
})
237-
238-
return err
239236
}
240237

241238
type findForksOptions struct {

services/repository/generate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ func generateGitContent(ctx context.Context, repo, templateRepo, generateRepo *r
279279
if err = gitrepo.SetDefaultBranch(ctx, repo, repo.DefaultBranch); err != nil {
280280
return fmt.Errorf("setDefaultBranch: %w", err)
281281
}
282-
if err = UpdateRepository(ctx, repo, false); err != nil {
282+
if err = repo_model.UpdateRepositoryColsNoAutoTime(ctx, repo, "default_branch"); err != nil {
283283
return fmt.Errorf("updateRepository: %w", err)
284284
}
285285

services/repository/migrate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User,
220220
}
221221

222222
repo.IsMirror = true
223-
if err = UpdateRepository(ctx, repo, false); err != nil {
223+
if err = repo_model.UpdateRepositoryColsNoAutoTime(ctx, repo, "num_watches", "is_empty", "default_branch", "default_wiki_branch", "is_mirror"); err != nil {
224224
return nil, err
225225
}
226226

0 commit comments

Comments
 (0)