You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

v157.go 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. package migrations
  5. import (
  6. "xorm.io/xorm"
  7. )
  8. func fixRepoTopics(x *xorm.Engine) error {
  9. type Topic struct {
  10. ID int64 `xorm:"pk autoincr"`
  11. Name string `xorm:"UNIQUE VARCHAR(25)"`
  12. RepoCount int
  13. }
  14. type RepoTopic struct {
  15. RepoID int64 `xorm:"pk"`
  16. TopicID int64 `xorm:"pk"`
  17. }
  18. type Repository struct {
  19. ID int64 `xorm:"pk autoincr"`
  20. Topics []string `xorm:"TEXT JSON"`
  21. }
  22. const batchSize = 100
  23. sess := x.NewSession()
  24. defer sess.Close()
  25. repos := make([]*Repository, 0, batchSize)
  26. topics := make([]string, 0, batchSize)
  27. for start := 0; ; start += batchSize {
  28. repos = repos[:0]
  29. if err := sess.Begin(); err != nil {
  30. return err
  31. }
  32. if err := sess.Limit(batchSize, start).Find(&repos); err != nil {
  33. return err
  34. }
  35. if len(repos) == 0 {
  36. break
  37. }
  38. for _, repo := range repos {
  39. topics = topics[:0]
  40. if err := sess.Select("name").Table("topic").
  41. Join("INNER", "repo_topic", "repo_topic.topic_id = topic.id").
  42. Where("repo_topic.repo_id = ?", repo.ID).Desc("topic.repo_count").Find(&topics); err != nil {
  43. return err
  44. }
  45. repo.Topics = topics
  46. if _, err := sess.ID(repo.ID).Cols("topics").Update(repo); err != nil {
  47. return err
  48. }
  49. }
  50. if err := sess.Commit(); err != nil {
  51. return err
  52. }
  53. }
  54. return nil
  55. }