summaryrefslogtreecommitdiffstats
path: root/models/migrations/v16.go
blob: de430070778545aab8aa0083798e8c57327ee4ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package migrations

import (
	"fmt"
	"time"

	"code.gitea.io/gitea/modules/markdown"

	"github.com/go-xorm/xorm"
)

// RepoUnit describes all units of a repository
type RepoUnit struct {
	ID          int64
	RepoID      int64 `xorm:"INDEX(s)"`
	Type        int   `xorm:"INDEX(s)"`
	Index       int
	Config      map[string]string `xorm:"JSON"`
	CreatedUnix int64             `xorm:"INDEX CREATED"`
	Created     time.Time         `xorm:"-"`
}

// Enumerate all the unit types
const (
	UnitTypeCode            = iota + 1 // 1 code
	UnitTypeIssues                     // 2 issues
	UnitTypePRs                        // 3 PRs
	UnitTypeCommits                    // 4 Commits
	UnitTypeReleases                   // 5 Releases
	UnitTypeWiki                       // 6 Wiki
	UnitTypeSettings                   // 7 Settings
	UnitTypeExternalWiki               // 8 ExternalWiki
	UnitTypeExternalTracker            // 9 ExternalTracker
)

// Repo describes a repository
type Repo struct {
	ID                                                                               int64
	EnableWiki, EnableExternalWiki, EnableIssues, EnableExternalTracker, EnablePulls bool
	ExternalWikiURL, ExternalTrackerURL, ExternalTrackerFormat, ExternalTrackerStyle string
}

func addUnitsToTables(x *xorm.Engine) error {
	var repos []Repo
	err := x.Table("repository").Select("*").Find(&repos)
	if err != nil {
		return fmt.Errorf("Query repositories: %v", err)
	}

	sess := x.NewSession()
	defer sess.Close()

	if err := sess.Begin(); err != nil {
		return err
	}

	var repoUnit RepoUnit
	if err := sess.CreateTable(&repoUnit); err != nil {
		return fmt.Errorf("CreateTable RepoUnit: %v", err)
	}

	if err := sess.CreateUniques(&repoUnit); err != nil {
		return fmt.Errorf("CreateUniques RepoUnit: %v", err)
	}

	if err := sess.CreateIndexes(&repoUnit); err != nil {
		return fmt.Errorf("CreateIndexes RepoUnit: %v", err)
	}

	for _, repo := range repos {
		for i := 1; i <= 9; i++ {
			if (i == UnitTypeWiki || i == UnitTypeExternalWiki) && !repo.EnableWiki {
				continue
			}
			if i == UnitTypeExternalWiki && !repo.EnableExternalWiki {
				continue
			}
			if i == UnitTypePRs && !repo.EnablePulls {
				continue
			}
			if (i == UnitTypeIssues || i == UnitTypeExternalTracker) && !repo.EnableIssues {
				continue
			}
			if i == UnitTypeExternalTracker && !repo.EnableExternalTracker {
				continue
			}

			var config = make(map[string]string)
			switch i {
			case UnitTypeExternalTracker:
				config["ExternalTrackerURL"] = repo.ExternalTrackerURL
				config["ExternalTrackerFormat"] = repo.ExternalTrackerFormat
				if len(repo.ExternalTrackerStyle) == 0 {
					repo.ExternalTrackerStyle = markdown.IssueNameStyleNumeric
				}
				config["ExternalTrackerStyle"] = repo.ExternalTrackerStyle
			case UnitTypeExternalWiki:
				config["ExternalWikiURL"] = repo.ExternalWikiURL
			}

			if _, err = sess.Insert(&RepoUnit{
				RepoID: repo.ID,
				Type:   i,
				Index:  i,
				Config: config,
			}); err != nil {
				return fmt.Errorf("Insert repo unit: %v", err)
			}
		}
	}

	if err := sess.Commit(); err != nil {
		return err
	}

	return nil
}