Skip to content

Commit 12960b9

Browse files
6543zeripath
andauthored
[BugFix] remove nil inserts in models (#11096)
* Fix InsertReleases Nil Insert on Attachments * FIX "No element on slice when insert" & smal refactor * again * impruve * rm useles newline * Apply suggestions from code review Co-Authored-By: zeripath <[email protected]> * process insert as a whole Co-authored-by: zeripath <[email protected]>
1 parent 0010fde commit 12960b9

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

models/migrate.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,20 @@ func insertIssue(sess *xorm.Session, issue *Issue) error {
6464
})
6565
labelIDs = append(labelIDs, label.ID)
6666
}
67-
if _, err := sess.Insert(issueLabels); err != nil {
68-
return err
67+
if len(issueLabels) > 0 {
68+
if _, err := sess.Insert(issueLabels); err != nil {
69+
return err
70+
}
6971
}
7072

7173
for _, reaction := range issue.Reactions {
7274
reaction.IssueID = issue.ID
7375
}
74-
if _, err := sess.Insert(issue.Reactions); err != nil {
75-
return err
76+
77+
if len(issue.Reactions) > 0 {
78+
if _, err := sess.Insert(issue.Reactions); err != nil {
79+
return err
80+
}
7681
}
7782

7883
cols := make([]string, 0)
@@ -151,8 +156,10 @@ func InsertIssueComments(comments []*Comment) error {
151156
reaction.IssueID = comment.IssueID
152157
reaction.CommentID = comment.ID
153158
}
154-
if _, err := sess.Insert(comment.Reactions); err != nil {
155-
return err
159+
if len(comment.Reactions) > 0 {
160+
if _, err := sess.Insert(comment.Reactions); err != nil {
161+
return err
162+
}
156163
}
157164
}
158165

@@ -196,12 +203,14 @@ func InsertReleases(rels ...*Release) error {
196203
return err
197204
}
198205

199-
for i := 0; i < len(rel.Attachments); i++ {
200-
rel.Attachments[i].ReleaseID = rel.ID
201-
}
206+
if len(rel.Attachments) > 0 {
207+
for i := range rel.Attachments {
208+
rel.Attachments[i].ReleaseID = rel.ID
209+
}
202210

203-
if _, err := sess.NoAutoTime().Insert(rel.Attachments); err != nil {
204-
return err
211+
if _, err := sess.NoAutoTime().Insert(rel.Attachments); err != nil {
212+
return err
213+
}
205214
}
206215
}
207216

modules/migrations/gitea.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -393,13 +393,16 @@ func (g *GiteaLocalUploader) CreateIssues(issues ...*base.Issue) error {
393393
iss = append(iss, &is)
394394
}
395395

396-
err := models.InsertIssues(iss...)
397-
if err != nil {
398-
return err
399-
}
400-
for _, is := range iss {
401-
g.issues.Store(is.Index, is.ID)
396+
if len(iss) > 0 {
397+
if err := models.InsertIssues(iss...); err != nil {
398+
return err
399+
}
400+
401+
for _, is := range iss {
402+
g.issues.Store(is.Index, is.ID)
403+
}
402404
}
405+
403406
return nil
404407
}
405408

@@ -478,6 +481,9 @@ func (g *GiteaLocalUploader) CreateComments(comments ...*base.Comment) error {
478481
cms = append(cms, &cm)
479482
}
480483

484+
if len(cms) == 0 {
485+
return nil
486+
}
481487
return models.InsertIssueComments(cms)
482488
}
483489

0 commit comments

Comments
 (0)