2017-02-21 01:46:14 +01: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 16:53:46 +01:00
|
|
|
package migrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2017-09-16 19:17:57 +02:00
|
|
|
"code.gitea.io/gitea/modules/markup"
|
2017-02-04 16:53:46 +01:00
|
|
|
|
|
|
|
"github.com/go-xorm/xorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Enumerate all the unit types
|
|
|
|
const (
|
2017-07-17 04:04:43 +02: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 16:53:46 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func addUnitsToTables(x *xorm.Engine) error {
|
2017-10-08 13:08:18 +02:00
|
|
|
// 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]interface{} `xorm:"JSON"`
|
|
|
|
CreatedUnix int64 `xorm:"INDEX CREATED"`
|
|
|
|
Created time.Time `xorm:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Repo describes a repository
|
|
|
|
type Repo struct {
|
|
|
|
ID int64
|
|
|
|
EnableWiki, EnableExternalWiki, EnableIssues, EnableExternalTracker, EnablePulls bool
|
|
|
|
ExternalWikiURL, ExternalTrackerURL, ExternalTrackerFormat, ExternalTrackerStyle string
|
|
|
|
}
|
|
|
|
|
2017-02-04 16:53:46 +01:00
|
|
|
var repos []Repo
|
2017-02-15 02:32:03 +01:00
|
|
|
err := x.Table("repository").Select("*").Find(&repos)
|
2017-02-04 16:53:46 +01: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 16:43:51 +01:00
|
|
|
if exist, err := sess.IsTableExist(&repoUnit); err != nil {
|
|
|
|
return fmt.Errorf("IsExist RepoUnit: %v", err)
|
|
|
|
} else if exist {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-04 16:53:46 +01: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 04:04:43 +02:00
|
|
|
if (i == V16UnitTypeWiki || i == V16UnitTypeExternalWiki) && !repo.EnableWiki {
|
2017-02-04 16:53:46 +01:00
|
|
|
continue
|
|
|
|
}
|
2017-07-17 04:04:43 +02:00
|
|
|
if i == V16UnitTypeExternalWiki && !repo.EnableExternalWiki {
|
2017-02-04 16:53:46 +01:00
|
|
|
continue
|
|
|
|
}
|
2017-07-17 04:04:43 +02:00
|
|
|
if i == V16UnitTypePRs && !repo.EnablePulls {
|
2017-02-04 16:53:46 +01:00
|
|
|
continue
|
|
|
|
}
|
2017-07-17 04:04:43 +02:00
|
|
|
if (i == V16UnitTypeIssues || i == V16UnitTypeExternalTracker) && !repo.EnableIssues {
|
2017-02-04 16:53:46 +01:00
|
|
|
continue
|
|
|
|
}
|
2017-07-17 04:04:43 +02:00
|
|
|
if i == V16UnitTypeExternalTracker && !repo.EnableExternalTracker {
|
2017-02-04 16:53:46 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-09-12 08:48:13 +02:00
|
|
|
var config = make(map[string]interface{})
|
2017-02-04 16:53:46 +01:00
|
|
|
switch i {
|
2017-07-17 04:04:43 +02:00
|
|
|
case V16UnitTypeExternalTracker:
|
2017-02-04 16:53:46 +01:00
|
|
|
config["ExternalTrackerURL"] = repo.ExternalTrackerURL
|
|
|
|
config["ExternalTrackerFormat"] = repo.ExternalTrackerFormat
|
|
|
|
if len(repo.ExternalTrackerStyle) == 0 {
|
2017-09-16 19:17:57 +02:00
|
|
|
repo.ExternalTrackerStyle = markup.IssueNameStyleNumeric
|
2017-02-04 16:53:46 +01:00
|
|
|
}
|
|
|
|
config["ExternalTrackerStyle"] = repo.ExternalTrackerStyle
|
2017-07-17 04:04:43 +02:00
|
|
|
case V16UnitTypeExternalWiki:
|
2017-02-04 16:53:46 +01: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 10:08:30 +02:00
|
|
|
return sess.Commit()
|
2017-02-04 16:53:46 +01:00
|
|
|
}
|