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

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