Skip to content

Commit c24f4b3

Browse files
lunnywxiaoguang
andauthored
Add migrations tests (#34456)
Fix #34455 Co-authored-by: wxiaoguang <[email protected]>
1 parent bf338bb commit c24f4b3

File tree

4 files changed

+99
-2
lines changed

4 files changed

+99
-2
lines changed

models/migrations/v1_23/v302.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@ func AddIndexToActionTaskStoppedLogExpired(x *xorm.Engine) error {
1414
Stopped timeutil.TimeStamp `xorm:"index(stopped_log_expired)"`
1515
LogExpired bool `xorm:"index(stopped_log_expired)"`
1616
}
17-
return x.Sync(new(ActionTask))
17+
_, err := x.SyncWithOptions(xorm.SyncOptions{
18+
IgnoreDropIndices: true,
19+
}, new(ActionTask))
20+
return err
1821
}

models/migrations/v1_23/v302_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package v1_23 //nolint
5+
6+
import (
7+
"testing"
8+
9+
"code.gitea.io/gitea/models/migrations/base"
10+
"code.gitea.io/gitea/modules/timeutil"
11+
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
func Test_AddIndexToActionTaskStoppedLogExpired(t *testing.T) {
16+
type ActionTask struct {
17+
ID int64
18+
JobID int64
19+
Attempt int64
20+
RunnerID int64 `xorm:"index"`
21+
Status int `xorm:"index"`
22+
Started timeutil.TimeStamp `xorm:"index"`
23+
Stopped timeutil.TimeStamp `xorm:"index(stopped_log_expired)"`
24+
25+
RepoID int64 `xorm:"index"`
26+
OwnerID int64 `xorm:"index"`
27+
CommitSHA string `xorm:"index"`
28+
IsForkPullRequest bool
29+
30+
Token string `xorm:"-"`
31+
TokenHash string `xorm:"UNIQUE"` // sha256 of token
32+
TokenSalt string
33+
TokenLastEight string `xorm:"index token_last_eight"`
34+
35+
LogFilename string // file name of log
36+
LogInStorage bool // read log from database or from storage
37+
LogLength int64 // lines count
38+
LogSize int64 // blob size
39+
LogIndexes []int64 `xorm:"LONGBLOB"` // line number to offset
40+
LogExpired bool `xorm:"index(stopped_log_expired)"` // files that are too old will be deleted
41+
42+
Created timeutil.TimeStamp `xorm:"created"`
43+
Updated timeutil.TimeStamp `xorm:"updated index"`
44+
}
45+
46+
// Prepare and load the testing database
47+
x, deferable := base.PrepareTestEnv(t, 0, new(ActionTask))
48+
defer deferable()
49+
50+
assert.NoError(t, AddIndexToActionTaskStoppedLogExpired(x))
51+
}

models/migrations/v1_23/v304.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,8 @@ func AddIndexForReleaseSha1(x *xorm.Engine) error {
99
type Release struct {
1010
Sha1 string `xorm:"INDEX VARCHAR(64)"`
1111
}
12-
return x.Sync(new(Release))
12+
_, err := x.SyncWithOptions(xorm.SyncOptions{
13+
IgnoreDropIndices: true,
14+
}, new(Release))
15+
return err
1316
}

models/migrations/v1_23/v304_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package v1_23 //nolint
5+
6+
import (
7+
"testing"
8+
9+
"code.gitea.io/gitea/models/migrations/base"
10+
"code.gitea.io/gitea/modules/timeutil"
11+
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
func Test_AddIndexForReleaseSha1(t *testing.T) {
16+
type Release struct {
17+
ID int64 `xorm:"pk autoincr"`
18+
RepoID int64 `xorm:"INDEX UNIQUE(n)"`
19+
PublisherID int64 `xorm:"INDEX"`
20+
TagName string `xorm:"INDEX UNIQUE(n)"`
21+
OriginalAuthor string
22+
OriginalAuthorID int64 `xorm:"index"`
23+
LowerTagName string
24+
Target string
25+
Title string
26+
Sha1 string `xorm:"VARCHAR(64)"`
27+
NumCommits int64
28+
Note string `xorm:"TEXT"`
29+
IsDraft bool `xorm:"NOT NULL DEFAULT false"`
30+
IsPrerelease bool `xorm:"NOT NULL DEFAULT false"`
31+
IsTag bool `xorm:"NOT NULL DEFAULT false"` // will be true only if the record is a tag and has no related releases
32+
CreatedUnix timeutil.TimeStamp `xorm:"INDEX"`
33+
}
34+
35+
// Prepare and load the testing database
36+
x, deferable := base.PrepareTestEnv(t, 0, new(Release))
37+
defer deferable()
38+
39+
assert.NoError(t, AddIndexForReleaseSha1(x))
40+
}

0 commit comments

Comments
 (0)