Skip to content

Commit 4b187f5

Browse files
lafrikslunny
authored andcommitted
Fix migration order v1.3 (#3157)
* FIx migration order * Fix migration function file names to match version * Fix typo * Add note about ignored errors * Fix typo, remove wrong comment
1 parent aee45d0 commit 4b187f5

File tree

10 files changed

+276
-267
lines changed

10 files changed

+276
-267
lines changed

models/migrations/migrations.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ type Version struct {
5959
Version int64
6060
}
6161

62+
func emptyMigration(x *xorm.Engine) error {
63+
return nil
64+
}
65+
6266
// This is a sequence of migrations. Add new migrations to the bottom of the list.
6367
// If you want to "retire" a migration, remove it from the top of the list and
6468
// update minDBVersion accordingly
@@ -127,17 +131,17 @@ var migrations = []Migration{
127131
// v38 -> v39
128132
NewMigration("remove commits and settings unit types", removeCommitsUnitType),
129133
// v39 -> v40
130-
NewMigration("adds time tracking and stopwatches", addTimetracking),
134+
NewMigration("fix protected branch can push value to false", fixProtectedBranchCanPushValue),
131135
// v40 -> v41
132-
NewMigration("migrate protected branch struct", migrateProtectedBranchStruct),
136+
NewMigration("add tags to releases and sync existing repositories", releaseAddColumnIsTagAndSyncTags),
133137
// v41 -> v42
134-
NewMigration("add default value to user prohibit_login", addDefaultValueToUserProhibitLogin),
138+
NewMigration("remove duplicate unit types", removeDuplicateUnitTypes),
135139
// v42 -> v43
136-
NewMigration("add tags to releases and sync existing repositories", releaseAddColumnIsTagAndSyncTags),
140+
NewMigration("empty step", emptyMigration),
137141
// v43 -> v44
138-
NewMigration("fix protected branch can push value to false", fixProtectedBranchCanPushValue),
142+
NewMigration("empty step", emptyMigration),
139143
// v44 -> v45
140-
NewMigration("remove duplicate unit types", removeDuplicateUnitTypes),
144+
NewMigration("empty step", emptyMigration),
141145
// v45 -> v46
142146
NewMigration("remove index column from repo_unit table", removeIndexColumnFromRepoUnitTable),
143147
// v46 -> v47
@@ -146,6 +150,12 @@ var migrations = []Migration{
146150
NewMigration("add deleted branches", addDeletedBranch),
147151
// v48 -> v49
148152
NewMigration("add repo indexer status", addRepoIndexerStatus),
153+
// v49 -> v50
154+
NewMigration("adds time tracking and stopwatches", addTimetracking),
155+
// v50 -> v51
156+
NewMigration("migrate protected branch struct", migrateProtectedBranchStruct),
157+
// v51 -> v52
158+
NewMigration("add default value to user prohibit_login", addDefaultValueToUserProhibitLogin),
149159
}
150160

151161
// Migrate database to current version

models/migrations/v39.go

Lines changed: 9 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,69 +6,21 @@ package migrations
66

77
import (
88
"fmt"
9-
"time"
10-
11-
"code.gitea.io/gitea/modules/setting"
129

1310
"github.com/go-xorm/xorm"
1411
)
1512

16-
func addTimetracking(x *xorm.Engine) error {
17-
// RepoUnit describes all units of a repository
18-
type RepoUnit struct {
19-
ID int64
20-
RepoID int64 `xorm:"INDEX(s)"`
21-
Type int `xorm:"INDEX(s)"`
22-
Index int
23-
Config map[string]interface{} `xorm:"JSON"`
24-
CreatedUnix int64 `xorm:"INDEX CREATED"`
25-
Created time.Time `xorm:"-"`
26-
}
27-
28-
// Stopwatch see models/issue_stopwatch.go
29-
type Stopwatch struct {
30-
ID int64 `xorm:"pk autoincr"`
31-
IssueID int64 `xorm:"INDEX"`
32-
UserID int64 `xorm:"INDEX"`
33-
Created time.Time `xorm:"-"`
34-
CreatedUnix int64
35-
}
36-
37-
// TrackedTime see models/issue_tracked_time.go
38-
type TrackedTime struct {
39-
ID int64 `xorm:"pk autoincr" json:"id"`
40-
IssueID int64 `xorm:"INDEX" json:"issue_id"`
41-
UserID int64 `xorm:"INDEX" json:"user_id"`
42-
Created time.Time `xorm:"-" json:"created"`
43-
CreatedUnix int64 `json:"-"`
44-
Time int64 `json:"time"`
13+
func fixProtectedBranchCanPushValue(x *xorm.Engine) error {
14+
type ProtectedBranch struct {
15+
CanPush bool `xorm:"NOT NULL DEFAULT false"`
4516
}
4617

47-
if err := x.Sync2(new(Stopwatch)); err != nil {
18+
if err := x.Sync2(new(ProtectedBranch)); err != nil {
4819
return fmt.Errorf("Sync2: %v", err)
4920
}
50-
if err := x.Sync2(new(TrackedTime)); err != nil {
51-
return fmt.Errorf("Sync2: %v", err)
52-
}
53-
//Updating existing issue units
54-
units := make([]*RepoUnit, 0, 100)
55-
err := x.Where("`type` = ?", V16UnitTypeIssues).Find(&units)
56-
if err != nil {
57-
return fmt.Errorf("Query repo units: %v", err)
58-
}
59-
for _, unit := range units {
60-
if unit.Config == nil {
61-
unit.Config = make(map[string]interface{})
62-
}
63-
if _, ok := unit.Config["EnableTimetracker"]; !ok {
64-
unit.Config["EnableTimetracker"] = setting.Service.DefaultEnableTimetracking
65-
}
66-
if _, ok := unit.Config["AllowOnlyContributorsToTrackTime"]; !ok {
67-
unit.Config["AllowOnlyContributorsToTrackTime"] = setting.Service.DefaultAllowOnlyContributorsToTrackTime
68-
}
69-
if _, err := x.ID(unit.ID).Cols("config").Update(unit); err != nil {
70-
return err
71-
}
72-
}
73-
return nil
21+
22+
_, err := x.Cols("can_push").Update(&ProtectedBranch{
23+
CanPush: false,
24+
})
25+
return err
7426
}

models/migrations/v40.go

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,52 @@ package migrations
66

77
import (
88
"fmt"
9-
"time"
109

10+
"code.gitea.io/git"
11+
"code.gitea.io/gitea/models"
1112
"code.gitea.io/gitea/modules/log"
12-
"code.gitea.io/gitea/modules/setting"
1313

1414
"github.com/go-xorm/xorm"
1515
)
1616

17-
func migrateProtectedBranchStruct(x *xorm.Engine) error {
18-
type ProtectedBranch struct {
19-
ID int64 `xorm:"pk autoincr"`
20-
RepoID int64 `xorm:"UNIQUE(s)"`
21-
BranchName string `xorm:"UNIQUE(s)"`
22-
CanPush bool
23-
Created time.Time `xorm:"-"`
24-
CreatedUnix int64
25-
Updated time.Time `xorm:"-"`
26-
UpdatedUnix int64
27-
}
17+
// ReleaseV39 describes the added field for Release
18+
type ReleaseV39 struct {
19+
IsTag bool `xorm:"NOT NULL DEFAULT false"`
20+
}
2821

29-
var pbs []ProtectedBranch
30-
err := x.Find(&pbs)
31-
if err != nil {
32-
return err
22+
// TableName will be invoked by XORM to customrize the table name
23+
func (*ReleaseV39) TableName() string {
24+
return "release"
25+
}
26+
27+
func releaseAddColumnIsTagAndSyncTags(x *xorm.Engine) error {
28+
if err := x.Sync2(new(ReleaseV39)); err != nil {
29+
return fmt.Errorf("Sync2: %v", err)
3330
}
3431

35-
for _, pb := range pbs {
36-
if pb.CanPush {
37-
if _, err = x.ID(pb.ID).Delete(new(ProtectedBranch)); err != nil {
38-
return err
39-
}
32+
// For the sake of SQLite3, we can't use x.Iterate here.
33+
offset := 0
34+
pageSize := 20
35+
for {
36+
repos := make([]*models.Repository, 0, pageSize)
37+
if err := x.Table("repository").Asc("id").Limit(pageSize, offset).Find(&repos); err != nil {
38+
return fmt.Errorf("select repos [offset: %d]: %v", offset, err)
4039
}
41-
}
40+
for _, repo := range repos {
41+
gitRepo, err := git.OpenRepository(repo.RepoPath())
42+
if err != nil {
43+
log.Warn("OpenRepository: %v", err)
44+
continue
45+
}
4246

43-
switch {
44-
case setting.UseSQLite3:
45-
log.Warn("Unable to drop columns in SQLite")
46-
case setting.UseMySQL, setting.UsePostgreSQL, setting.UseMSSQL, setting.UseTiDB:
47-
if _, err := x.Exec("ALTER TABLE protected_branch DROP COLUMN can_push"); err != nil {
48-
return fmt.Errorf("DROP COLUMN can_push: %v", err)
47+
if err = models.SyncReleasesWithTags(repo, gitRepo); err != nil {
48+
log.Warn("SyncReleasesWithTags: %v", err)
49+
}
4950
}
50-
default:
51-
log.Fatal(4, "Unrecognized DB")
51+
if len(repos) < pageSize {
52+
break
53+
}
54+
offset += pageSize
5255
}
53-
5456
return nil
5557
}

models/migrations/v41.go

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,63 @@ package migrations
77
import (
88
"fmt"
99

10-
"code.gitea.io/gitea/models"
11-
1210
"github.com/go-xorm/xorm"
1311
)
1412

15-
func addDefaultValueToUserProhibitLogin(x *xorm.Engine) (err error) {
16-
user := &models.User{
17-
ProhibitLogin: false,
13+
func removeDuplicateUnitTypes(x *xorm.Engine) error {
14+
// RepoUnit describes all units of a repository
15+
type RepoUnit struct {
16+
RepoID int64
17+
Type int
18+
}
19+
20+
// Enumerate all the unit types
21+
const (
22+
UnitTypeCode = iota + 1 // 1 code
23+
UnitTypeIssues // 2 issues
24+
UnitTypePullRequests // 3 PRs
25+
UnitTypeReleases // 4 Releases
26+
UnitTypeWiki // 5 Wiki
27+
UnitTypeExternalWiki // 6 ExternalWiki
28+
UnitTypeExternalTracker // 7 ExternalTracker
29+
)
30+
31+
var externalIssueRepoUnits []RepoUnit
32+
err := x.Where("type = ?", UnitTypeExternalTracker).Find(&externalIssueRepoUnits)
33+
if err != nil {
34+
return fmt.Errorf("Query repositories: %v", err)
35+
}
36+
37+
var externalWikiRepoUnits []RepoUnit
38+
err = x.Where("type = ?", UnitTypeExternalWiki).Find(&externalWikiRepoUnits)
39+
if err != nil {
40+
return fmt.Errorf("Query repositories: %v", err)
1841
}
1942

20-
if _, err := x.Where("`prohibit_login` IS NULL").Cols("prohibit_login").Update(user); err != nil {
43+
sess := x.NewSession()
44+
defer sess.Close()
45+
46+
if err := sess.Begin(); err != nil {
2147
return err
2248
}
2349

24-
dialect := x.Dialect().DriverName()
25-
26-
switch dialect {
27-
case "mysql":
28-
_, err = x.Exec("ALTER TABLE user MODIFY `prohibit_login` tinyint(1) NOT NULL DEFAULT 0")
29-
case "postgres":
30-
_, err = x.Exec("ALTER TABLE \"user\" ALTER COLUMN `prohibit_login` SET NOT NULL, ALTER COLUMN `prohibit_login` SET DEFAULT false")
31-
case "mssql":
32-
// xorm already set DEFAULT 0 for data type BIT in mssql
33-
_, err = x.Exec(`ALTER TABLE [user] ALTER COLUMN "prohibit_login" BIT NOT NULL`)
34-
case "sqlite3":
50+
for _, repoUnit := range externalIssueRepoUnits {
51+
if _, err = sess.Delete(&RepoUnit{
52+
RepoID: repoUnit.RepoID,
53+
Type: UnitTypeIssues,
54+
}); err != nil {
55+
return fmt.Errorf("Delete repo unit: %v", err)
56+
}
3557
}
3658

37-
if err != nil {
38-
return fmt.Errorf("Error changing user prohibit_login column definition: %v", err)
59+
for _, repoUnit := range externalWikiRepoUnits {
60+
if _, err = sess.Delete(&RepoUnit{
61+
RepoID: repoUnit.RepoID,
62+
Type: UnitTypeWiki,
63+
}); err != nil {
64+
return fmt.Errorf("Delete repo unit: %v", err)
65+
}
3966
}
4067

41-
return err
68+
return sess.Commit()
4269
}

models/migrations/v42.go

Lines changed: 0 additions & 57 deletions
This file was deleted.

models/migrations/v43.go

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)