Skip to content

Commit 39ef6f8

Browse files
authored
Create Proper Migration Tests (#15116)
* Create Proper Migration tests Unfortunately our testing regime has so far meant that migrations do not get proper testing. This PR begins the process of creating migration tests for this. * Add test for v176 * fix mssql drop db Signed-off-by: Andrew Thornton <[email protected]>
1 parent 750ac52 commit 39ef6f8

File tree

17 files changed

+1038
-18
lines changed

17 files changed

+1038
-18
lines changed

Makefile

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ LDFLAGS := $(LDFLAGS) -X "main.MakeVersion=$(MAKE_VERSION)" -X "main.Version=$(G
8989

9090
LINUX_ARCHS ?= linux/amd64,linux/386,linux/arm-5,linux/arm-6,linux/arm64
9191

92-
GO_PACKAGES ?= $(filter-out code.gitea.io/gitea/integrations/migration-test,$(filter-out code.gitea.io/gitea/integrations,$(shell $(GO) list -mod=vendor ./... | grep -v /vendor/)))
92+
GO_PACKAGES ?= $(filter-out code.gitea.io/gitea/models/migrations code.gitea.io/gitea/integrations/migration-test code.gitea.io/gitea/integrations,$(shell $(GO) list -mod=vendor ./... | grep -v /vendor/))
9393

9494
FOMANTIC_CONFIGS := semantic.json web_src/fomantic/theme.config.less web_src/fomantic/_site/globals/site.variables
9595
FOMANTIC_DEST := web_src/fomantic/build/semantic.js web_src/fomantic/build/semantic.css
@@ -399,8 +399,9 @@ test-sqlite\#%: integrations.sqlite.test generate-ini-sqlite
399399
GITEA_ROOT="$(CURDIR)" GITEA_CONF=integrations/sqlite.ini ./integrations.sqlite.test -test.run $(subst .,/,$*)
400400

401401
.PHONY: test-sqlite-migration
402-
test-sqlite-migration: migrations.sqlite.test generate-ini-sqlite
402+
test-sqlite-migration: migrations.sqlite.test migrations.individual.sqlite.test generate-ini-sqlite
403403
GITEA_ROOT="$(CURDIR)" GITEA_CONF=integrations/sqlite.ini ./migrations.sqlite.test
404+
GITEA_ROOT="$(CURDIR)" GITEA_CONF=integrations/sqlite.ini ./migrations.individual.sqlite.test
404405

405406
generate-ini-mysql:
406407
sed -e 's|{{TEST_MYSQL_HOST}}|${TEST_MYSQL_HOST}|g' \
@@ -419,8 +420,9 @@ test-mysql\#%: integrations.mysql.test generate-ini-mysql
419420
GITEA_ROOT="$(CURDIR)" GITEA_CONF=integrations/mysql.ini ./integrations.mysql.test -test.run $(subst .,/,$*)
420421

421422
.PHONY: test-mysql-migration
422-
test-mysql-migration: migrations.mysql.test generate-ini-mysql
423+
test-mysql-migration: migrations.mysql.test migrations.individual.mysql.test generate-ini-mysql
423424
GITEA_ROOT="$(CURDIR)" GITEA_CONF=integrations/mysql.ini ./migrations.mysql.test
425+
GITEA_ROOT="$(CURDIR)" GITEA_CONF=integrations/mysql.ini ./migrations.individual.mysql.test
424426

425427
generate-ini-mysql8:
426428
sed -e 's|{{TEST_MYSQL8_HOST}}|${TEST_MYSQL8_HOST}|g' \
@@ -439,8 +441,9 @@ test-mysql8\#%: integrations.mysql8.test generate-ini-mysql8
439441
GITEA_ROOT="$(CURDIR)" GITEA_CONF=integrations/mysql8.ini ./integrations.mysql8.test -test.run $(subst .,/,$*)
440442

441443
.PHONY: test-mysql8-migration
442-
test-mysql8-migration: migrations.mysql8.test generate-ini-mysql8
444+
test-mysql8-migration: migrations.mysql8.test migrations.individual.mysql8.test generate-ini-mysql8
443445
GITEA_ROOT="$(CURDIR)" GITEA_CONF=integrations/mysql8.ini ./migrations.mysql8.test
446+
GITEA_ROOT="$(CURDIR)" GITEA_CONF=integrations/mysql8.ini ./migrations.individual.mysql8.test
444447

445448
generate-ini-pgsql:
446449
sed -e 's|{{TEST_PGSQL_HOST}}|${TEST_PGSQL_HOST}|g' \
@@ -460,8 +463,9 @@ test-pgsql\#%: integrations.pgsql.test generate-ini-pgsql
460463
GITEA_ROOT="$(CURDIR)" GITEA_CONF=integrations/pgsql.ini ./integrations.pgsql.test -test.run $(subst .,/,$*)
461464

462465
.PHONY: test-pgsql-migration
463-
test-pgsql-migration: migrations.pgsql.test generate-ini-pgsql
466+
test-pgsql-migration: migrations.pgsql.test migrations.individual.pgsql.test generate-ini-pgsql
464467
GITEA_ROOT="$(CURDIR)" GITEA_CONF=integrations/pgsql.ini ./migrations.pgsql.test
468+
GITEA_ROOT="$(CURDIR)" GITEA_CONF=integrations/pgsql.ini ./migrations.individual.pgsql.test
465469

466470
generate-ini-mssql:
467471
sed -e 's|{{TEST_MSSQL_HOST}}|${TEST_MSSQL_HOST}|g' \
@@ -480,8 +484,9 @@ test-mssql\#%: integrations.mssql.test generate-ini-mssql
480484
GITEA_ROOT="$(CURDIR)" GITEA_CONF=integrations/mssql.ini ./integrations.mssql.test -test.run $(subst .,/,$*)
481485

482486
.PHONY: test-mssql-migration
483-
test-mssql-migration: migrations.mssql.test generate-ini-mssql
487+
test-mssql-migration: migrations.mssql.test migrations.individual.mssql.test generate-ini-mssql
484488
GITEA_ROOT="$(CURDIR)" GITEA_CONF=integrations/mssql.ini ./migrations.mssql.test -test.failfast
489+
GITEA_ROOT="$(CURDIR)" GITEA_CONF=integrations/mssql.ini ./migrations.individual.mssql.test -test.failfast
485490

486491
.PHONY: bench-sqlite
487492
bench-sqlite: integrations.sqlite.test generate-ini-sqlite
@@ -541,6 +546,26 @@ migrations.mssql.test: $(GO_SOURCES)
541546
migrations.sqlite.test: $(GO_SOURCES)
542547
$(GO) test $(GOTESTFLAGS) -c code.gitea.io/gitea/integrations/migration-test -o migrations.sqlite.test -tags '$(TEST_TAGS)'
543548

549+
.PHONY: migrations.individual.mysql.test
550+
migrations.individual.mysql.test: $(GO_SOURCES)
551+
$(GO) test $(GOTESTFLAGS) -c code.gitea.io/gitea/models/migrations -o migrations.individual.mysql.test
552+
553+
.PHONY: migrations.individual.mysql8.test
554+
migrations.individual.mysql8.test: $(GO_SOURCES)
555+
$(GO) test $(GOTESTFLAGS) -c code.gitea.io/gitea/models/migrations -o migrations.individual.mysql8.test
556+
557+
.PHONY: migrations.individual.pgsql.test
558+
migrations.individual.pgsql.test: $(GO_SOURCES)
559+
$(GO) test $(GOTESTFLAGS) -c code.gitea.io/gitea/models/migrations -o migrations.individual.pgsql.test
560+
561+
.PHONY: migrations.individual.mssql.test
562+
migrations.individual.mssql.test: $(GO_SOURCES)
563+
$(GO) test $(GOTESTFLAGS) -c code.gitea.io/gitea/models/migrations -o migrations.individual.mssql.test
564+
565+
.PHONY: migrations.individual.sqlite.test
566+
migrations.individual.sqlite.test: $(GO_SOURCES)
567+
$(GO) test $(GOTESTFLAGS) -c code.gitea.io/gitea/models/migrations -o migrations.individual.sqlite.test -tags '$(TEST_TAGS)'
568+
544569
.PHONY: check
545570
check: test
546571

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Issue_Label 1 should not be deleted
2+
-
3+
id: 1
4+
issue_id: 1
5+
label_id: 1
6+
7+
# Issue_label 2 should be deleted
8+
-
9+
id: 2
10+
issue_id: 5
11+
label_id: 99
12+
13+
# Issue_Label 3 should not be deleted
14+
-
15+
id: 3
16+
issue_id: 2
17+
label_id: 1
18+
19+
# Issue_Label 4 should not be deleted
20+
-
21+
id: 4
22+
issue_id: 2
23+
label_id: 4
24+
25+
-
26+
id: 5
27+
issue_id: 2
28+
label_id: 87
29+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
-
2+
id: 1
3+
repo_id: 1
4+
org_id: 0
5+
name: label1
6+
color: '#abcdef'
7+
num_issues: 2
8+
num_closed_issues: 0
9+
10+
-
11+
id: 2
12+
repo_id: 1
13+
org_id: 0
14+
name: label2
15+
color: '#000000'
16+
num_issues: 1
17+
num_closed_issues: 1
18+
-
19+
id: 3
20+
repo_id: 0
21+
org_id: 3
22+
name: orglabel3
23+
color: '#abcdef'
24+
num_issues: 0
25+
num_closed_issues: 0
26+
27+
-
28+
id: 4
29+
repo_id: 0
30+
org_id: 3
31+
name: orglabel4
32+
color: '#000000'
33+
num_issues: 1
34+
num_closed_issues: 0
35+
36+
-
37+
id: 5
38+
repo_id: 10
39+
org_id: 0
40+
name: pull-test-label
41+
color: '#000000'
42+
num_issues: 0
43+
num_closed_issues: 0
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.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,9 +819,24 @@ func dropTableColumns(sess *xorm.Session, tableName string, columnNames ...strin
819819
}
820820
for _, constraint := range constraints {
821821
if _, err := sess.Exec(fmt.Sprintf("ALTER TABLE `%s` DROP CONSTRAINT `%s`", tableName, constraint)); err != nil {
822-
return fmt.Errorf("Drop table `%s` constraint `%s`: %v", tableName, constraint, err)
822+
return fmt.Errorf("Drop table `%s` default constraint `%s`: %v", tableName, constraint, err)
823823
}
824824
}
825+
sql = fmt.Sprintf("SELECT DISTINCT Name FROM SYS.INDEXES INNER JOIN SYS.INDEX_COLUMNS ON INDEXES.INDEX_ID = INDEX_COLUMNS.INDEX_ID AND INDEXES.OBJECT_ID = INDEX_COLUMNS.OBJECT_ID WHERE INDEXES.OBJECT_ID = OBJECT_ID('%[1]s') AND INDEX_COLUMNS.COLUMN_ID IN (SELECT column_id FROM sys.columns WHERE lower(NAME) IN (%[2]s) AND object_id = OBJECT_ID('%[1]s'))",
826+
tableName, strings.ReplaceAll(cols, "`", "'"))
827+
constraints = make([]string, 0)
828+
if err := sess.SQL(sql).Find(&constraints); err != nil {
829+
return fmt.Errorf("Find constraints: %v", err)
830+
}
831+
for _, constraint := range constraints {
832+
if _, err := sess.Exec(fmt.Sprintf("ALTER TABLE `%s` DROP CONSTRAINT IF EXISTS `%s`", tableName, constraint)); err != nil {
833+
return fmt.Errorf("Drop table `%s` index constraint `%s`: %v", tableName, constraint, err)
834+
}
835+
if _, err := sess.Exec(fmt.Sprintf("DROP INDEX IF EXISTS `%[2]s` ON `%[1]s`", tableName, constraint)); err != nil {
836+
return fmt.Errorf("Drop index `%[2]s` on `%[1]s`: %v", tableName, constraint, err)
837+
}
838+
}
839+
825840
if _, err := sess.Exec(fmt.Sprintf("ALTER TABLE `%s` DROP COLUMN %s", tableName, cols)); err != nil {
826841
return fmt.Errorf("Drop table `%s` columns %v: %v", tableName, columnNames, err)
827842
}

0 commit comments

Comments
 (0)