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

v16.go 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package migrations
  2. import (
  3. "fmt"
  4. "time"
  5. "code.gitea.io/gitea/modules/markdown"
  6. "github.com/go-xorm/xorm"
  7. )
  8. // RepoUnit describes all units of a repository
  9. type RepoUnit struct {
  10. ID int64
  11. RepoID int64 `xorm:"INDEX(s)"`
  12. Type int `xorm:"INDEX(s)"`
  13. Index int
  14. Config map[string]string `xorm:"JSON"`
  15. CreatedUnix int64 `xorm:"INDEX CREATED"`
  16. Created time.Time `xorm:"-"`
  17. }
  18. // Enumerate all the unit types
  19. const (
  20. UnitTypeCode = iota + 1 // 1 code
  21. UnitTypeIssues // 2 issues
  22. UnitTypePRs // 3 PRs
  23. UnitTypeCommits // 4 Commits
  24. UnitTypeReleases // 5 Releases
  25. UnitTypeWiki // 6 Wiki
  26. UnitTypeSettings // 7 Settings
  27. UnitTypeExternalWiki // 8 ExternalWiki
  28. UnitTypeExternalTracker // 9 ExternalTracker
  29. )
  30. // Repo describes a repository
  31. type Repo struct {
  32. ID int64
  33. EnableWiki, EnableExternalWiki, EnableIssues, EnableExternalTracker, EnablePulls bool
  34. ExternalWikiURL, ExternalTrackerURL, ExternalTrackerFormat, ExternalTrackerStyle string
  35. }
  36. func addUnitsToTables(x *xorm.Engine) error {
  37. var repos []Repo
  38. err := x.Table("repository").Find(&repos)
  39. if err != nil {
  40. return fmt.Errorf("Query repositories: %v", err)
  41. }
  42. sess := x.NewSession()
  43. defer sess.Close()
  44. if err := sess.Begin(); err != nil {
  45. return err
  46. }
  47. var repoUnit RepoUnit
  48. if err := sess.CreateTable(&repoUnit); err != nil {
  49. return fmt.Errorf("CreateTable RepoUnit: %v", err)
  50. }
  51. if err := sess.CreateUniques(&repoUnit); err != nil {
  52. return fmt.Errorf("CreateUniques RepoUnit: %v", err)
  53. }
  54. if err := sess.CreateIndexes(&repoUnit); err != nil {
  55. return fmt.Errorf("CreateIndexes RepoUnit: %v", err)
  56. }
  57. for _, repo := range repos {
  58. for i := 1; i <= 9; i++ {
  59. if (i == UnitTypeWiki || i == UnitTypeExternalWiki) && !repo.EnableWiki {
  60. continue
  61. }
  62. if i == UnitTypeExternalWiki && !repo.EnableExternalWiki {
  63. continue
  64. }
  65. if i == UnitTypePRs && !repo.EnablePulls {
  66. continue
  67. }
  68. if (i == UnitTypeIssues || i == UnitTypeExternalTracker) && !repo.EnableIssues {
  69. continue
  70. }
  71. if i == UnitTypeExternalTracker && !repo.EnableExternalTracker {
  72. continue
  73. }
  74. var config = make(map[string]string)
  75. switch i {
  76. case UnitTypeExternalTracker:
  77. config["ExternalTrackerURL"] = repo.ExternalTrackerURL
  78. config["ExternalTrackerFormat"] = repo.ExternalTrackerFormat
  79. if len(repo.ExternalTrackerStyle) == 0 {
  80. repo.ExternalTrackerStyle = markdown.IssueNameStyleNumeric
  81. }
  82. config["ExternalTrackerStyle"] = repo.ExternalTrackerStyle
  83. case UnitTypeExternalWiki:
  84. config["ExternalWikiURL"] = repo.ExternalWikiURL
  85. }
  86. if _, err = sess.Insert(&RepoUnit{
  87. RepoID: repo.ID,
  88. Type: i,
  89. Index: i,
  90. Config: config,
  91. }); err != nil {
  92. return fmt.Errorf("Insert repo unit: %v", err)
  93. }
  94. }
  95. }
  96. if err := sess.Commit(); err != nil {
  97. return err
  98. }
  99. return nil
  100. }