Added update query for repo_topic references. Fixed transactions
Signed-off-by: Alexey Terentyev <axifnx@gmail.com>
This commit is contained in:
parent
6554ff104e
commit
7ee9774cea
|
|
@ -29,13 +29,13 @@ func reformatAndRemoveIncorrectTopics(x *xorm.Engine) (err error) {
|
|||
delTopicIDs := make([]int64, 0, batchSize)
|
||||
ids := make([]int64, 0, 30)
|
||||
|
||||
log.Info("Validating existed topics...")
|
||||
if err := sess.Begin(); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Info("Validating existed topics...")
|
||||
for start := 0; ; start += batchSize {
|
||||
topics = topics[:0]
|
||||
if err := sess.Asc("id").Limit(batchSize, start).Find(&topics); err != nil {
|
||||
if err := x.Asc("id").Limit(batchSize, start).Find(&topics); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(topics) == 0 {
|
||||
|
|
@ -49,7 +49,7 @@ func reformatAndRemoveIncorrectTopics(x *xorm.Engine) (err error) {
|
|||
|
||||
topic.Name = strings.Replace(strings.TrimSpace(strings.ToLower(topic.Name)), " ", "-", -1)
|
||||
|
||||
if err := sess.Table("repo_topic").Cols("repo_id").
|
||||
if err := x.Table("repo_topic").Cols("repo_id").
|
||||
Where("topic_id = ?", topic.ID).Find(&ids); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -58,11 +58,21 @@ func reformatAndRemoveIncorrectTopics(x *xorm.Engine) (err error) {
|
|||
}
|
||||
|
||||
if models.ValidateTopic(topic.Name) {
|
||||
count, err := sess.Where("name = ?", topic.Name).Count(&Topic{})
|
||||
unifiedTopic := Topic{Name:topic.Name}
|
||||
exists, err := sess.Get(&unifiedTopic)
|
||||
log.Info("Exists topic with the name %q? %v id = %v", topic.Name, exists, unifiedTopic.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if count == 0 {
|
||||
if exists {
|
||||
log.Info("Updating repo_topic rows with topic_id = %v to topic_id = %v", topic.ID, unifiedTopic.ID)
|
||||
if _, err := sess.Where("topic_id = ? AND repo_id NOT IN " +
|
||||
"(SELECT rt1.repo_id FROM repo_topic rt1 INNER JOIN repo_topic rt2 " +
|
||||
"ON rt1.repo_id = rt2.repo_id WHERE rt1.topic_id = ? AND rt2.topic_id = ?)",
|
||||
topic.ID, topic.ID, unifiedTopic.ID).Update(&models.RepoTopic{TopicID:unifiedTopic.ID}); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
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 {
|
||||
|
|
@ -74,8 +84,16 @@ func reformatAndRemoveIncorrectTopics(x *xorm.Engine) (err error) {
|
|||
delTopicIDs = append(delTopicIDs, topic.ID)
|
||||
}
|
||||
}
|
||||
if err := sess.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sess.Init()
|
||||
|
||||
log.Info("Deleting incorrect topics...")
|
||||
if err := sess.Begin(); err != nil {
|
||||
return err
|
||||
}
|
||||
for start := 0; ; start += batchSize {
|
||||
if (start + batchSize) < len(delTopicIDs) {
|
||||
ids = delTopicIDs[start:(start + batchSize)]
|
||||
|
|
@ -97,6 +115,9 @@ func reformatAndRemoveIncorrectTopics(x *xorm.Engine) (err error) {
|
|||
break
|
||||
}
|
||||
}
|
||||
if err := sess.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
repoTopics := make([]*models.RepoTopic, 0, batchSize)
|
||||
delRepoTopics := make([]*models.RepoTopic, 0, batchSize)
|
||||
|
|
@ -105,7 +126,7 @@ func reformatAndRemoveIncorrectTopics(x *xorm.Engine) (err error) {
|
|||
log.Info("Checking the number of topics in the repositories...")
|
||||
for start := 0; ; start += batchSize {
|
||||
repoTopics = repoTopics[:0]
|
||||
if err := sess.Cols("repo_id").Asc("repo_id").Limit(batchSize, start).
|
||||
if err := x.Cols("repo_id").Asc("repo_id").Limit(batchSize, start).
|
||||
GroupBy("repo_id").Having("COUNT(*) > 25").Find(&repoTopics); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -118,7 +139,7 @@ func reformatAndRemoveIncorrectTopics(x *xorm.Engine) (err error) {
|
|||
touchedRepo[repoTopic.RepoID] = struct{}{}
|
||||
|
||||
tmpRepoTopics = tmpRepoTopics[:0]
|
||||
if err := sess.Where("repo_id = ?", repoTopic.RepoID).Find(&tmpRepoTopics); err != nil {
|
||||
if err := x.Where("repo_id = ?", repoTopic.RepoID).Find(&tmpRepoTopics); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
@ -130,7 +151,12 @@ func reformatAndRemoveIncorrectTopics(x *xorm.Engine) (err error) {
|
|||
}
|
||||
}
|
||||
|
||||
sess.Init()
|
||||
|
||||
log.Info("Deleting superfluous topics for repositories (more than 25 topics)...")
|
||||
if err := sess.Begin(); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, repoTopic := range delRepoTopics {
|
||||
log.Info("Deleting 'repo_topic' rows for 'repository' with id = %v. Topic id = %v",
|
||||
repoTopic.RepoID, repoTopic.TopicID)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user