2017-02-21 00:46:14 +00:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2017-02-04 15:53:46 +00:00
|
|
|
package migrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2017-09-16 17:17:57 +00:00
|
|
|
"code.gitea.io/gitea/modules/markup"
|
2017-02-04 15:53:46 +00:00
|
|
|
|
|
|
|
"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
|
2017-09-12 06:48:13 +00:00
|
|
|
Config map[string]interface{} `xorm:"JSON"`
|
|
|
|
CreatedUnix int64 `xorm:"INDEX CREATED"`
|
|
|
|
Created time.Time `xorm:"-"`
|
2017-02-04 15:53:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enumerate all the unit types
|
|
|
|
const (
|
2017-07-17 02:04:43 +00:00
|
|
|
V16UnitTypeCode = iota + 1 // 1 code
|
|
|
|
V16UnitTypeIssues // 2 issues
|
|
|
|
V16UnitTypePRs // 3 PRs
|
|
|
|
V16UnitTypeCommits // 4 Commits
|
|
|
|
V16UnitTypeReleases // 5 Releases
|
|
|
|
V16UnitTypeWiki // 6 Wiki
|
|
|
|
V16UnitTypeSettings // 7 Settings
|
|
|
|
V16UnitTypeExternalWiki // 8 ExternalWiki
|
|
|
|
V16UnitTypeExternalTracker // 9 ExternalTracker
|
2017-02-04 15:53:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
2017-02-15 01:32:03 +00:00
|
|
|
err := x.Table("repository").Select("*").Find(&repos)
|
2017-02-04 15:53:46 +00:00
|
|
|
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
|
2017-03-10 15:43:51 +00:00
|
|
|
if exist, err := sess.IsTableExist(&repoUnit); err != nil {
|
|
|
|
return fmt.Errorf("IsExist RepoUnit: %v", err)
|
|
|
|
} else if exist {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-04 15:53:46 +00:00
|
|
|
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++ {
|
2017-07-17 02:04:43 +00:00
|
|
|
if (i == V16UnitTypeWiki || i == V16UnitTypeExternalWiki) && !repo.EnableWiki {
|
2017-02-04 15:53:46 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-07-17 02:04:43 +00:00
|
|
|
if i == V16UnitTypeExternalWiki && !repo.EnableExternalWiki {
|
2017-02-04 15:53:46 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-07-17 02:04:43 +00:00
|
|
|
if i == V16UnitTypePRs && !repo.EnablePulls {
|
2017-02-04 15:53:46 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-07-17 02:04:43 +00:00
|
|
|
if (i == V16UnitTypeIssues || i == V16UnitTypeExternalTracker) && !repo.EnableIssues {
|
2017-02-04 15:53:46 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-07-17 02:04:43 +00:00
|
|
|
if i == V16UnitTypeExternalTracker && !repo.EnableExternalTracker {
|
2017-02-04 15:53:46 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-09-12 06:48:13 +00:00
|
|
|
var config = make(map[string]interface{})
|
2017-02-04 15:53:46 +00:00
|
|
|
switch i {
|
2017-07-17 02:04:43 +00:00
|
|
|
case V16UnitTypeExternalTracker:
|
2017-02-04 15:53:46 +00:00
|
|
|
config["ExternalTrackerURL"] = repo.ExternalTrackerURL
|
|
|
|
config["ExternalTrackerFormat"] = repo.ExternalTrackerFormat
|
|
|
|
if len(repo.ExternalTrackerStyle) == 0 {
|
2017-09-16 17:17:57 +00:00
|
|
|
repo.ExternalTrackerStyle = markup.IssueNameStyleNumeric
|
2017-02-04 15:53:46 +00:00
|
|
|
}
|
|
|
|
config["ExternalTrackerStyle"] = repo.ExternalTrackerStyle
|
2017-07-17 02:04:43 +00:00
|
|
|
case V16UnitTypeExternalWiki:
|
2017-02-04 15:53:46 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-19 08:08:30 +00:00
|
|
|
return sess.Commit()
|
2017-02-04 15:53:46 +00:00
|
|
|
}
|