Fixed violation of the unique constraint for v68 migration

Signed-off-by: Alexey Terentyev <axifnx@gmail.com>
This commit is contained in:
Alexey Terentyev 2018-06-22 02:30:06 +03:00
parent 0b3ea42847
commit 6554ff104e
No known key found for this signature in database
GPG Key ID: 60D6C550AEEBB467

View File

@ -45,6 +45,8 @@ func reformatAndRemoveIncorrectTopics(x *xorm.Engine) (err error) {
if models.ValidateTopic(topic.Name) {
continue
}
log.Info("Incorrect topic: id = %v, name = %q", topic.ID, topic.Name)
topic.Name = strings.Replace(strings.TrimSpace(strings.ToLower(topic.Name)), " ", "-", -1)
if err := sess.Table("repo_topic").Cols("repo_id").
@ -56,15 +58,21 @@ func reformatAndRemoveIncorrectTopics(x *xorm.Engine) (err error) {
}
if models.ValidateTopic(topic.Name) {
log.Info("Updating topic: id = %v, name = %v", topic.ID, topic.Name)
count, err := sess.Where("name = ?", topic.Name).Count(&Topic{})
if err != nil {
return err
}
if count == 0 {
log.Info("Updating topic: id = %v, name = %q", topic.ID, topic.Name)
if _, err := sess.Table("topic").ID(topic.ID).
Update(&Topic{Name: topic.Name}); err != nil {
return err
}
} else {
delTopicIDs = append(delTopicIDs, topic.ID)
continue
}
}
delTopicIDs = append(delTopicIDs, topic.ID)
}
}
log.Info("Deleting incorrect topics...")