Skip to content

Commit e412a04

Browse files
authored
Merge pull request #1149 from traPtitech/feat/add_migrate
add migrate
2 parents af7fa95 + 5d81be9 commit e412a04

File tree

6 files changed

+65
-28
lines changed

6 files changed

+65
-28
lines changed

go.mod

+6-4
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,19 @@ require (
5050
github.com/prometheus/client_model v0.2.0 // indirect
5151
github.com/prometheus/common v0.32.1 // indirect
5252
github.com/prometheus/procfs v0.7.3 // indirect
53-
gorm.io/gorm v1.22.5
53+
gorm.io/gorm v1.25.5
5454
)
5555

5656
require (
57-
github.com/jinzhu/now v1.1.4 // indirect
58-
gorm.io/driver/mysql v1.2.3
57+
github.com/jinzhu/now v1.1.5 // indirect
58+
gorm.io/driver/mysql v1.5.2
5959
)
6060

6161
require (
62-
github.com/go-sql-driver/mysql v1.6.0 // indirect
62+
github.com/go-sql-driver/mysql v1.7.0 // indirect
6363
github.com/prometheus/client_golang v1.12.1
6464
gopkg.in/guregu/null.v4 v4.0.0
6565
gorm.io/plugin/prometheus v0.0.0-20210820101226-2a49866f83ee
6666
)
67+
68+
require github.com/go-gormigrate/gormigrate/v2 v2.1.1 // indirect

go.sum

+11
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeME
8585
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
8686
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
8787
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
88+
github.com/go-gormigrate/gormigrate/v2 v2.1.1 h1:eGS0WTFRV30r103lU8JNXY27KbviRnqqIDobW3EV3iY=
89+
github.com/go-gormigrate/gormigrate/v2 v2.1.1/go.mod h1:L7nJ620PFDKei9QOhJzqA8kRCk+E3UbV2f5gv+1ndLc=
8890
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
8991
github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
9092
github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY=
@@ -101,6 +103,8 @@ github.com/go-playground/validator/v10 v10.10.1 h1:uA0+amWMiglNZKZ9FJRKUAe9U3RX9
101103
github.com/go-playground/validator/v10 v10.10.1/go.mod h1:i+3WkQ1FvaUjjxh1kSvIA4dMGDBiPU55YFDl0WbKdWU=
102104
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
103105
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
106+
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
107+
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
104108
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
105109
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
106110
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
@@ -191,6 +195,8 @@ github.com/jinzhu/now v1.1.2/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/
191195
github.com/jinzhu/now v1.1.3/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
192196
github.com/jinzhu/now v1.1.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas=
193197
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
198+
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
199+
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
194200
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
195201
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
196202
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
@@ -705,10 +711,15 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
705711
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
706712
gorm.io/driver/mysql v1.2.3 h1:cZqzlOfg5Kf1VIdLC1D9hT6Cy9BgxhExLj/2tIgUe7Y=
707713
gorm.io/driver/mysql v1.2.3/go.mod h1:qsiz+XcAyMrS6QY+X3M9R6b/lKM1imKmcuK9kac5LTo=
714+
gorm.io/driver/mysql v1.5.2 h1:QC2HRskSE75wBuOxe0+iCkyJZ+RqpudsQtqkp+IMuXs=
715+
gorm.io/driver/mysql v1.5.2/go.mod h1:pQLhh1Ut/WUAySdTHwBpBv6+JKcj+ua4ZFx1QQTBzb8=
708716
gorm.io/gorm v1.21.13/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0=
709717
gorm.io/gorm v1.22.4/go.mod h1:1aeVC+pe9ZmvKZban/gW4QPra7PRoTEssyc922qCAkk=
710718
gorm.io/gorm v1.22.5 h1:lYREBgc02Be/5lSCTuysZZDb6ffL2qrat6fg9CFbvXU=
711719
gorm.io/gorm v1.22.5/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
720+
gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
721+
gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls=
722+
gorm.io/gorm v1.25.5/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
712723
gorm.io/plugin/prometheus v0.0.0-20210820101226-2a49866f83ee h1:UhePWayrSEXncx1x7UdBNvUfbuxrQOk9LV44glIJElQ=
713724
gorm.io/plugin/prometheus v0.0.0-20210820101226-2a49866f83ee/go.mod h1:tu0ajC4OlzwAK6aSL4NQjAvvbBeWcVbv2X285icuEm0=
714725
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func main() {
3434
panic(err)
3535
}
3636

37-
err = model.Migrate()
37+
_, err = model.Migrate()
3838
if err != nil {
3939
panic(err)
4040
}

model/current.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package model
2+
3+
import (
4+
"github.com/go-gormigrate/gormigrate/v2"
5+
)
6+
7+
// Migrations is all db migrations
8+
func Migrations() []*gormigrate.Migration {
9+
return []*gormigrate.Migration{}
10+
}
11+
12+
func AllTables() []interface{} {
13+
return []interface{}{
14+
&Questionnaires{},
15+
&Questions{},
16+
&Respondents{},
17+
&Responses{},
18+
&Administrators{},
19+
&Options{},
20+
&ScaleLabels{},
21+
&Targets{},
22+
&Validations{},
23+
}
24+
}

model/db.go

+21-21
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,14 @@ import (
44
"fmt"
55
"os"
66

7+
"github.com/go-gormigrate/gormigrate/v2"
78
"gorm.io/driver/mysql"
89
"gorm.io/gorm"
910
"gorm.io/gorm/logger"
1011
"gorm.io/plugin/prometheus"
1112
)
1213

13-
var (
14-
db *gorm.DB
15-
allTables = []interface{}{
16-
Questionnaires{},
17-
Questions{},
18-
Respondents{},
19-
Responses{},
20-
Administrators{},
21-
Options{},
22-
ScaleLabels{},
23-
Targets{},
24-
Validations{},
25-
}
26-
)
14+
var db *gorm.DB
2715

2816
// EstablishConnection DBと接続
2917
func EstablishConnection(isProduction bool) error {
@@ -83,12 +71,24 @@ func EstablishConnection(isProduction bool) error {
8371
return nil
8472
}
8573

86-
// Migrate DBのMigrationを行う
87-
func Migrate() error {
88-
err := db.AutoMigrate(allTables...)
89-
if err != nil {
90-
return fmt.Errorf("failed in table's migration: %w", err)
91-
}
74+
func Migrate() (init bool, err error) {
75+
m := gormigrate.New(db.Session(&gorm.Session{}), gormigrate.DefaultOptions, Migrations())
9276

93-
return nil
77+
m.InitSchema(func(db *gorm.DB) error {
78+
init = true
79+
80+
return db.AutoMigrate(AllTables()...)
81+
})
82+
err = m.Migrate()
83+
return
9484
}
85+
86+
// Migrate DBのMigrationを行う
87+
// func Migrate() error {
88+
// err := db.AutoMigrate(allTables...)
89+
// if err != nil {
90+
// return fmt.Errorf("failed in table's migration: %w", err)
91+
// }
92+
93+
// return nil
94+
// }

model/db_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ var (
2323
targetImpl = new(Target)
2424
)
2525

26-
//TestMain テストのmain
26+
// TestMain テストのmain
2727
func TestMain(m *testing.M) {
2828
err := EstablishConnection(true)
2929
if err != nil {
3030
panic(err)
3131
}
3232

33-
err = Migrate()
33+
_, err = Migrate()
3434
if err != nil {
3535
panic(err)
3636
}

0 commit comments

Comments
 (0)