Skip to content

Commit dbc396d

Browse files
committed
Add test for v176
Signed-off-by: Andrew Thornton <[email protected]>
1 parent a64dbd7 commit dbc396d

File tree

7 files changed

+374
-0
lines changed

7 files changed

+374
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# type Comment struct {
2+
# ID int64 `xorm:"pk autoincr"`
3+
# Type int `xorm:"INDEX"`
4+
# IssueID int64 `xorm:"INDEX"`
5+
# LabelID int64
6+
# }
7+
#
8+
# we are only interested in type 7
9+
#
10+
11+
-
12+
id: 1 # Should remain
13+
type: 6
14+
issue_id: 1
15+
label_id: 0
16+
should_remain: true
17+
-
18+
id: 2 # Should remain
19+
type: 7
20+
issue_id: 1 # repo_id: 1
21+
label_id: 1 # repo_id: 1
22+
should_remain: true
23+
-
24+
id: 3 # Should remain
25+
type: 7
26+
issue_id: 2 # repo_id: 2 owner_id: 1
27+
label_id: 2 # org_id: 1
28+
should_remain: true
29+
-
30+
id: 4 # Should be DELETED
31+
type: 7
32+
issue_id: 1 # repo_id: 1
33+
label_id: 3 # repo_id: 2
34+
should_remain: false
35+
-
36+
id: 5 # Should remain
37+
type: 7
38+
issue_id: 3 # repo_id: 1
39+
label_id: 1 # repo_id: 1
40+
should_remain: true
41+
-
42+
id: 6 # Should be DELETED
43+
type: 7
44+
issue_id: 3 # repo_id: 1 owner_id: 2
45+
label_id: 2 # org_id: 1
46+
should_remain: false
47+
-
48+
id: 7 # Should be DELETED
49+
type: 7
50+
issue_id: 3 # repo_id: 1 owner_id: 2
51+
label_id: 5 # repo_id: 3
52+
should_remain: false
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# type Issue struct {
2+
# ID int64 `xorm:"pk autoincr"`
3+
# RepoID int64 `xorm:"INDEX UNIQUE(repo_index)"`
4+
# Index int64 `xorm:"UNIQUE(repo_index)"` // Index in one repository.
5+
# }
6+
-
7+
id: 1
8+
repo_id: 1
9+
index: 1
10+
-
11+
id: 2
12+
repo_id: 2
13+
index: 1
14+
-
15+
id: 3
16+
repo_id: 1
17+
index: 2
18+
-
19+
id: 4
20+
repo_id: 3
21+
index: 1
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# type IssueLabel struct {
2+
# ID int64 `xorm:"pk autoincr"`
3+
# IssueID int64 `xorm:"UNIQUE(s)"`
4+
# LabelID int64 `xorm:"UNIQUE(s)"`
5+
# }
6+
-
7+
id: 1 # Should remain - matches comment 2
8+
issue_id: 1
9+
label_id: 1
10+
should_remain: true
11+
-
12+
id: 2 # Should remain
13+
issue_id: 2
14+
label_id: 2
15+
should_remain: true
16+
-
17+
id: 3 # Should be deleted
18+
issue_id: 1
19+
label_id: 3
20+
should_remain: false
21+
-
22+
id: 4 # Should remain
23+
issue_id: 3
24+
label_id: 1
25+
should_remain: true
26+
-
27+
id: 5 # Should be deleted
28+
issue_id: 3
29+
label_id: 2
30+
should_remain: false
31+
-
32+
id: 6 # Should be deleted
33+
issue_id: 3
34+
label_id: 5
35+
should_remain: false
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# type Label struct {
2+
# ID int64 `xorm:"pk autoincr"`
3+
# RepoID int64 `xorm:"INDEX"`
4+
# OrgID int64 `xorm:"INDEX"`
5+
# }
6+
-
7+
id: 1
8+
repo_id: 1
9+
org_id: 0
10+
-
11+
id: 2
12+
repo_id: 0
13+
org_id: 1
14+
-
15+
id: 3
16+
repo_id: 2
17+
org_id: 0
18+
-
19+
id: 4
20+
repo_id: 1
21+
org_id: 0
22+
-
23+
id: 5
24+
repo_id: 3
25+
org_id: 0
26+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# type Repository struct {
2+
# ID int64 `xorm:"pk autoincr"`
3+
# OwnerID int64 `xorm:"UNIQUE(s) index"`
4+
# LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"`
5+
# }
6+
-
7+
id: 1
8+
owner_id: 2
9+
lower_name: "repo1"
10+
-
11+
id: 2
12+
owner_id: 1
13+
lower_name: "repo2"
14+
-
15+
id: 3
16+
owner_id: 2
17+
lower_name: "repo3"

models/migrations/migrations_test.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package migrations
66

77
import (
8+
"database/sql"
89
"fmt"
910
"os"
1011
"path"
@@ -17,6 +18,7 @@ import (
1718
"code.gitea.io/gitea/modules/base"
1819
"code.gitea.io/gitea/modules/setting"
1920
"code.gitea.io/gitea/modules/timeutil"
21+
"code.gitea.io/gitea/modules/util"
2022
"github.com/stretchr/testify/assert"
2123
"github.com/unknwon/com"
2224
"xorm.io/xorm"
@@ -98,6 +100,97 @@ func SetEngine() (*xorm.Engine, error) {
98100
return x, nil
99101
}
100102

103+
func deleteDB() error {
104+
switch {
105+
case setting.Database.UseSQLite3:
106+
if err := util.Remove(setting.Database.Path); err != nil {
107+
return err
108+
}
109+
return os.MkdirAll(path.Dir(setting.Database.Path), os.ModePerm)
110+
111+
case setting.Database.UseMySQL:
112+
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/",
113+
setting.Database.User, setting.Database.Passwd, setting.Database.Host))
114+
if err != nil {
115+
return err
116+
}
117+
defer db.Close()
118+
119+
if _, err = db.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", setting.Database.Name)); err != nil {
120+
return err
121+
}
122+
123+
if _, err = db.Exec(fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s", setting.Database.Name)); err != nil {
124+
return err
125+
}
126+
return nil
127+
case setting.Database.UsePostgreSQL:
128+
db, err := sql.Open("postgres", fmt.Sprintf("postgres://%s:%s@%s/?sslmode=%s",
129+
setting.Database.User, setting.Database.Passwd, setting.Database.Host, setting.Database.SSLMode))
130+
if err != nil {
131+
return err
132+
}
133+
defer db.Close()
134+
135+
if _, err = db.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", setting.Database.Name)); err != nil {
136+
return err
137+
}
138+
139+
if _, err = db.Exec(fmt.Sprintf("CREATE DATABASE %s", setting.Database.Name)); err != nil {
140+
return err
141+
}
142+
db.Close()
143+
144+
// Check if we need to setup a specific schema
145+
if len(setting.Database.Schema) != 0 {
146+
db, err = sql.Open("postgres", fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=%s",
147+
setting.Database.User, setting.Database.Passwd, setting.Database.Host, setting.Database.Name, setting.Database.SSLMode))
148+
if err != nil {
149+
return err
150+
}
151+
defer db.Close()
152+
153+
schrows, err := db.Query(fmt.Sprintf("SELECT 1 FROM information_schema.schemata WHERE schema_name = '%s'", setting.Database.Schema))
154+
if err != nil {
155+
return err
156+
}
157+
defer schrows.Close()
158+
159+
if !schrows.Next() {
160+
// Create and setup a DB schema
161+
_, err = db.Exec(fmt.Sprintf("CREATE SCHEMA %s", setting.Database.Schema))
162+
if err != nil {
163+
return err
164+
}
165+
}
166+
167+
// Make the user's default search path the created schema; this will affect new connections
168+
_, err = db.Exec(fmt.Sprintf(`ALTER USER "%s" SET search_path = %s`, setting.Database.User, setting.Database.Schema))
169+
if err != nil {
170+
return err
171+
}
172+
return nil
173+
}
174+
case setting.Database.UseMSSQL:
175+
host, port := setting.ParseMSSQLHostPort(setting.Database.Host)
176+
db, err := sql.Open("mssql", fmt.Sprintf("server=%s; port=%s; database=%s; user id=%s; password=%s;",
177+
host, port, "master", setting.Database.User, setting.Database.Passwd))
178+
if err != nil {
179+
return err
180+
}
181+
defer db.Close()
182+
183+
if _, err = db.Exec("DROP DATABASE IF EXISTS [%s]", setting.Database.Name); err != nil {
184+
return err
185+
}
186+
if _, err = db.Exec("CREATE DATABASE [%s]", setting.Database.Name); err != nil {
187+
return err
188+
}
189+
}
190+
191+
return nil
192+
}
193+
101194
// prepareTestEnv prepares the test environment. The skip parameter should usually be 0. Provide models to be sync'd
102195
// with the database - in particular any models you expect fixtures to be loaded from.
103196
//
@@ -112,6 +205,11 @@ func prepareTestEnv(t *testing.T, skip int, syncModels ...interface{}) (*xorm.En
112205
assert.NoError(t, com.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"),
113206
setting.RepoRootPath))
114207

208+
if err := deleteDB(); err != nil {
209+
t.Errorf("unable to reset database: %v", err)
210+
return nil, deferFn
211+
}
212+
115213
x, err := SetEngine()
116214
assert.NoError(t, err)
117215
if x != nil {

0 commit comments

Comments
 (0)