diff options
Diffstat (limited to 'models/migrations/migrations.go')
-rw-r--r-- | models/migrations/migrations.go | 100 |
1 files changed, 98 insertions, 2 deletions
diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 08a503e6..0d17cf26 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -11,6 +11,7 @@ import ( "io/ioutil" "os" "path" + "path/filepath" "strings" "time" @@ -65,6 +66,8 @@ var migrations = []Migration{ NewMigration("trim action compare URL prefix", trimCommitActionAppUrlPrefix), // V5 -> V6:v0.6.3 NewMigration("generate issue-label from issue", issueToIssueLabel), // V6 -> V7:v0.6.4 NewMigration("refactor attachment table", attachmentRefactor), // V7 -> V8:v0.6.4 + NewMigration("rename pull request fields", renamePullRequestFields), // V8 -> V9:v0.6.16 + NewMigration("clean up migrate repo info", cleanUpMigrateRepoInfo), // V9 -> V10:v0.6.20 } // Migrate database to current version @@ -453,7 +456,7 @@ func trimCommitActionAppUrlPrefix(x *xorm.Engine) error { pushCommits = new(PushCommits) if err = json.Unmarshal(action["content"], pushCommits); err != nil { - return fmt.Errorf("unmarshal action content[%s]: %v", actID, err) + return fmt.Errorf("unmarshal action content[%d]: %v", actID, err) } infos := strings.Split(pushCommits.CompareUrl, "/") @@ -464,7 +467,7 @@ func trimCommitActionAppUrlPrefix(x *xorm.Engine) error { p, err := json.Marshal(pushCommits) if err != nil { - return fmt.Errorf("marshal action content[%s]: %v", actID, err) + return fmt.Errorf("marshal action content[%d]: %v", actID, err) } if _, err = sess.Id(actID).Update(&Action{ @@ -606,3 +609,96 @@ func attachmentRefactor(x *xorm.Engine) error { return sess.Commit() } + +func renamePullRequestFields(x *xorm.Engine) (err error) { + type PullRequest struct { + ID int64 `xorm:"pk autoincr"` + PullID int64 `xorm:"INDEX"` + PullIndex int64 + HeadBarcnh string + + IssueID int64 `xorm:"INDEX"` + Index int64 + HeadBranch string + } + + if err = x.Sync(new(PullRequest)); err != nil { + return fmt.Errorf("sync: %v", err) + } + + results, err := x.Query("SELECT `id`,`pull_id`,`pull_index`,`head_barcnh` FROM `pull_request`") + if err != nil { + if strings.Contains(err.Error(), "no such column") { + return nil + } + return fmt.Errorf("select pull requests: %v", err) + } + + sess := x.NewSession() + defer sessionRelease(sess) + if err = sess.Begin(); err != nil { + return err + } + + var pull *PullRequest + for _, pr := range results { + pull = &PullRequest{ + ID: com.StrTo(pr["id"]).MustInt64(), + IssueID: com.StrTo(pr["pull_id"]).MustInt64(), + Index: com.StrTo(pr["pull_index"]).MustInt64(), + HeadBranch: string(pr["head_barcnh"]), + } + if _, err = sess.Id(pull.ID).Update(pull); err != nil { + return err + } + } + + return sess.Commit() +} + +func cleanUpMigrateRepoInfo(x *xorm.Engine) (err error) { + type ( + User struct { + ID int64 `xorm:"pk autoincr"` + LowerName string + } + Repository struct { + ID int64 `xorm:"pk autoincr"` + OwnerID int64 + LowerName string + } + ) + + repos := make([]*Repository, 0, 25) + if err = x.Where("is_mirror=?", false).Find(&repos); err != nil { + return fmt.Errorf("select all non-mirror repositories: %v", err) + } + var user *User + for _, repo := range repos { + user = &User{ID: repo.OwnerID} + has, err := x.Get(user) + if err != nil { + return fmt.Errorf("get owner of repository[%d - %d]: %v", repo.ID, repo.OwnerID, err) + } else if !has { + continue + } + + configPath := filepath.Join(setting.RepoRootPath, user.LowerName, repo.LowerName+".git/config") + + // In case repository file is somehow missing. + if !com.IsFile(configPath) { + continue + } + + cfg, err := ini.Load(configPath) + if err != nil { + return fmt.Errorf("open config file: %v", err) + } + cfg.DeleteSection("remote \"origin\"") + if err = cfg.SaveToIndent(configPath, "\t"); err != nil { + return fmt.Errorf("save config file: %v", err) + } + } + + return nil +} |