Skip to content

Commit 045074c

Browse files
committed
Merge remote-tracking branch 'origin/master' into refactor-logger
2 parents 664511b + 4099e4f commit 045074c

File tree

3 files changed

+100
-3
lines changed

3 files changed

+100
-3
lines changed

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ var migrations = []Migration{
248248
NewMigration("add changed_protected_files column for pull_request table", addChangedProtectedFilesPullRequestColumn),
249249
// v156 -> v157
250250
NewMigration("fix publisher ID for tag releases", fixPublisherIDforTagReleases),
251+
// v157 -> v158
252+
NewMigration("ensure repo topics are up-to-date", fixRepoTopics),
251253
}
252254

253255
// GetCurrentDBVersion returns the current db version

models/migrations/v157.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2020 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package migrations
6+
7+
import (
8+
"xorm.io/xorm"
9+
)
10+
11+
func fixRepoTopics(x *xorm.Engine) error {
12+
13+
type Topic struct {
14+
ID int64 `xorm:"pk autoincr"`
15+
Name string `xorm:"UNIQUE VARCHAR(25)"`
16+
RepoCount int
17+
}
18+
19+
type RepoTopic struct {
20+
RepoID int64 `xorm:"pk"`
21+
TopicID int64 `xorm:"pk"`
22+
}
23+
24+
type Repository struct {
25+
ID int64 `xorm:"pk autoincr"`
26+
Topics []string `xorm:"TEXT JSON"`
27+
}
28+
29+
const batchSize = 100
30+
sess := x.NewSession()
31+
defer sess.Close()
32+
repos := make([]*Repository, 0, batchSize)
33+
topics := make([]string, 0, batchSize)
34+
for start := 0; ; start += batchSize {
35+
repos = repos[:0]
36+
37+
if err := sess.Begin(); err != nil {
38+
return err
39+
}
40+
41+
if err := sess.Limit(batchSize, start).Find(&repos); err != nil {
42+
return err
43+
}
44+
45+
if len(repos) == 0 {
46+
break
47+
}
48+
49+
for _, repo := range repos {
50+
topics = topics[:0]
51+
if err := sess.Select("name").Table("topic").
52+
Join("INNER", "repo_topic", "repo_topic.topic_id = topic.id").
53+
Where("repo_topic.repo_id = ?", repo.ID).Desc("topic.repo_count").Find(&topics); err != nil {
54+
return err
55+
}
56+
repo.Topics = topics
57+
if _, err := sess.ID(repo.ID).Cols("topics").Update(repo); err != nil {
58+
return err
59+
}
60+
}
61+
62+
if err := sess.Commit(); err != nil {
63+
return err
64+
}
65+
}
66+
67+
return nil
68+
}

models/topic.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,13 @@ func FindTopics(opts *FindTopicOptions) (topics []*Topic, err error) {
197197

198198
// GetRepoTopicByName retrives topic from name for a repo if it exist
199199
func GetRepoTopicByName(repoID int64, topicName string) (*Topic, error) {
200+
return getRepoTopicByName(x, repoID, topicName)
201+
}
202+
func getRepoTopicByName(e Engine, repoID int64, topicName string) (*Topic, error) {
200203
var cond = builder.NewCond()
201204
var topic Topic
202205
cond = cond.And(builder.Eq{"repo_topic.repo_id": repoID}).And(builder.Eq{"topic.name": topicName})
203-
sess := x.Table("topic").Where(cond)
206+
sess := e.Table("topic").Where(cond)
204207
sess.Join("INNER", "repo_topic", "repo_topic.topic_id = topic.id")
205208
has, err := sess.Get(&topic)
206209
if has {
@@ -211,7 +214,13 @@ func GetRepoTopicByName(repoID int64, topicName string) (*Topic, error) {
211214

212215
// AddTopic adds a topic name to a repository (if it does not already have it)
213216
func AddTopic(repoID int64, topicName string) (*Topic, error) {
214-
topic, err := GetRepoTopicByName(repoID, topicName)
217+
sess := x.NewSession()
218+
defer sess.Close()
219+
if err := sess.Begin(); err != nil {
220+
return nil, err
221+
}
222+
223+
topic, err := getRepoTopicByName(sess, repoID, topicName)
215224
if err != nil {
216225
return nil, err
217226
}
@@ -220,7 +229,25 @@ func AddTopic(repoID int64, topicName string) (*Topic, error) {
220229
return topic, nil
221230
}
222231

223-
return addTopicByNameToRepo(x, repoID, topicName)
232+
topic, err = addTopicByNameToRepo(sess, repoID, topicName)
233+
if err != nil {
234+
return nil, err
235+
}
236+
237+
topicNames := make([]string, 0, 25)
238+
if err := sess.Select("name").Table("topic").
239+
Join("INNER", "repo_topic", "repo_topic.topic_id = topic.id").
240+
Where("repo_topic.repo_id = ?", repoID).Desc("topic.repo_count").Find(&topicNames); err != nil {
241+
return nil, err
242+
}
243+
244+
if _, err := sess.ID(repoID).Cols("topics").Update(&Repository{
245+
Topics: topicNames,
246+
}); err != nil {
247+
return nil, err
248+
}
249+
250+
return topic, sess.Commit()
224251
}
225252

226253
// DeleteTopic removes a topic name from a repository (if it has it)

0 commit comments

Comments
 (0)