From 6554ff104ecf61927b68f3755b84a1cbf5ce27d9 Mon Sep 17 00:00:00 2001 From: Alexey Terentyev Date: Fri, 22 Jun 2018 02:30:06 +0300 Subject: [PATCH] Fixed violation of the unique constraint for v68 migration Signed-off-by: Alexey Terentyev --- models/migrations/v68.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/models/migrations/v68.go b/models/migrations/v68.go index d6a0d04c5..4ac55b722 100644 --- a/models/migrations/v68.go +++ b/models/migrations/v68.go @@ -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,14 +58,20 @@ func reformatAndRemoveIncorrectTopics(x *xorm.Engine) (err error) { } if models.ValidateTopic(topic.Name) { - log.Info("Updating topic: id = %v, name = %v", topic.ID, topic.Name) - if _, err := sess.Table("topic").ID(topic.ID). - Update(&Topic{Name: topic.Name}); err != nil { + count, err := sess.Where("name = ?", topic.Name).Count(&Topic{}) + if err != nil { return err } - } else { - delTopicIDs = append(delTopicIDs, topic.ID) + 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 + } + continue + } } + delTopicIDs = append(delTopicIDs, topic.ID) } }