Skip to content

Commit b3e094e

Browse files
committed
Fix migration hell
1 parent 9337688 commit b3e094e

File tree

2 files changed

+65
-42
lines changed

2 files changed

+65
-42
lines changed

models/migrations/v54.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package migrations
6+
7+
import (
8+
"fmt"
9+
10+
"code.gitea.io/gitea/modules/util"
11+
12+
"github.com/go-xorm/xorm"
13+
)
14+
15+
func addPullRequestOptions(x *xorm.Engine) error {
16+
// RepoUnit describes all units of a repository
17+
type RepoUnit struct {
18+
ID int64
19+
RepoID int64 `xorm:"INDEX(s)"`
20+
Type int `xorm:"INDEX(s)"`
21+
Config map[string]interface{} `xorm:"JSON"`
22+
CreatedUnix util.TimeStamp `xorm:"INDEX CREATED"`
23+
}
24+
25+
sess := x.NewSession()
26+
defer sess.Close()
27+
if err := sess.Begin(); err != nil {
28+
return err
29+
}
30+
31+
//Updating existing issue units
32+
units := make([]*RepoUnit, 0, 100)
33+
if err := sess.Where("`type` = ?", V16UnitTypePRs).Find(&units); err != nil {
34+
return fmt.Errorf("Query repo units: %v", err)
35+
}
36+
for _, unit := range units {
37+
if unit.Config == nil {
38+
unit.Config = make(map[string]interface{})
39+
}
40+
if _, ok := unit.Config["IgnoreWhitespaceConflicts"]; !ok {
41+
unit.Config["IgnoreWhitespaceConflicts"] = false
42+
}
43+
if _, ok := unit.Config["AllowMerge"]; !ok {
44+
unit.Config["AllowMerge"] = true
45+
}
46+
if _, ok := unit.Config["AllowRebase"]; !ok {
47+
unit.Config["AllowRebase"] = true
48+
}
49+
if _, ok := unit.Config["AllowSquash"]; !ok {
50+
unit.Config["AllowSquash"] = true
51+
}
52+
if _, err := sess.ID(unit.ID).Cols("config").Update(unit); err != nil {
53+
return err
54+
}
55+
}
56+
return sess.Commit()
57+
}

models/migrations/v55.go

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2017 The Gitea Authors. All rights reserved.
1+
// Copyright 2018 The Gitea Authors. All rights reserved.
22
// Use of this source code is governed by a MIT-style
33
// license that can be found in the LICENSE file.
44

@@ -7,51 +7,17 @@ package migrations
77
import (
88
"fmt"
99

10-
"code.gitea.io/gitea/modules/util"
11-
10+
"code.gitea.io/gitea/models"
1211
"github.com/go-xorm/xorm"
1312
)
1413

15-
func addPullRequestOptions(x *xorm.Engine) error {
16-
// RepoUnit describes all units of a repository
17-
type RepoUnit struct {
18-
ID int64
19-
RepoID int64 `xorm:"INDEX(s)"`
20-
Type int `xorm:"INDEX(s)"`
21-
Config map[string]interface{} `xorm:"JSON"`
22-
CreatedUnix util.TimeStamp `xorm:"INDEX CREATED"`
23-
}
24-
25-
sess := x.NewSession()
26-
defer sess.Close()
27-
if err := sess.Begin(); err != nil {
28-
return err
14+
func addModeToDeploKeys(x *xorm.Engine) error {
15+
type DeployKey struct {
16+
Mode models.AccessMode `xorm:"NOT NULL DEFAULT 1"`
2917
}
3018

31-
//Updating existing issue units
32-
units := make([]*RepoUnit, 0, 100)
33-
if err := sess.Where("`type` = ?", V16UnitTypePRs).Find(&units); err != nil {
34-
return fmt.Errorf("Query repo units: %v", err)
35-
}
36-
for _, unit := range units {
37-
if unit.Config == nil {
38-
unit.Config = make(map[string]interface{})
39-
}
40-
if _, ok := unit.Config["IgnoreWhitespaceConflicts"]; !ok {
41-
unit.Config["IgnoreWhitespaceConflicts"] = false
42-
}
43-
if _, ok := unit.Config["AllowMerge"]; !ok {
44-
unit.Config["AllowMerge"] = true
45-
}
46-
if _, ok := unit.Config["AllowRebase"]; !ok {
47-
unit.Config["AllowRebase"] = true
48-
}
49-
if _, ok := unit.Config["AllowSquash"]; !ok {
50-
unit.Config["AllowSquash"] = true
51-
}
52-
if _, err := sess.ID(unit.ID).Cols("config").Update(unit); err != nil {
53-
return err
54-
}
19+
if err := x.Sync2(new(DeployKey)); err != nil {
20+
return fmt.Errorf("Sync2: %v", err)
5521
}
56-
return sess.Commit()
22+
return nil
5723
}

0 commit comments

Comments
 (0)