Skip to content

[BugFix] remove nil inserts in models #11096

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

Merged
merged 12 commits into from
Apr 17, 2020
31 changes: 20 additions & 11 deletions models/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,20 @@ func insertIssue(sess *xorm.Session, issue *Issue) error {
})
labelIDs = append(labelIDs, label.ID)
}
if _, err := sess.Insert(issueLabels); err != nil {
return err
if len(issueLabels) > 0 {
if _, err := sess.Insert(issueLabels); err != nil {
return err
}
}

for _, reaction := range issue.Reactions {
reaction.IssueID = issue.ID
}
if _, err := sess.Insert(issue.Reactions); err != nil {
return err

if len(issue.Reactions) > 0 {
if _, err := sess.Insert(issue.Reactions); err != nil {
return err
}
}

cols := make([]string, 0)
Expand Down Expand Up @@ -151,8 +156,10 @@ func InsertIssueComments(comments []*Comment) error {
reaction.IssueID = comment.IssueID
reaction.CommentID = comment.ID
}
if _, err := sess.Insert(comment.Reactions); err != nil {
return err
if len(comment.Reactions) > 0 {
if _, err := sess.Insert(comment.Reactions); err != nil {
return err
}
}
}

Expand Down Expand Up @@ -196,12 +203,14 @@ func InsertReleases(rels ...*Release) error {
return err
}

for i := 0; i < len(rel.Attachments); i++ {
rel.Attachments[i].ReleaseID = rel.ID
}
if len(rel.Attachments) > 0 {
for i := range rel.Attachments {
rel.Attachments[i].ReleaseID = rel.ID
}

if _, err := sess.NoAutoTime().Insert(rel.Attachments); err != nil {
return err
if _, err := sess.NoAutoTime().Insert(rel.Attachments); err != nil {
return err
}
}
}

Expand Down
18 changes: 12 additions & 6 deletions modules/migrations/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,13 +393,16 @@ func (g *GiteaLocalUploader) CreateIssues(issues ...*base.Issue) error {
iss = append(iss, &is)
}

err := models.InsertIssues(iss...)
if err != nil {
return err
}
for _, is := range iss {
g.issues.Store(is.Index, is.ID)
if len(iss) > 0 {
if err := models.InsertIssues(iss...); err != nil {
return err
}

for _, is := range iss {
g.issues.Store(is.Index, is.ID)
}
}

return nil
}

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

if len(cms) == 0 {
return nil
}
return models.InsertIssueComments(cms)
}

Expand Down