Skip to content

Commit

Permalink
Merge pull request #1149 from traPtitech/feat/add_migrate
Browse files Browse the repository at this point in the history
add migrate
  • Loading branch information
kaitoyama committed Dec 17, 2023
2 parents af7fa95 + 5d81be9 commit e412a04
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 28 deletions.
10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,19 @@ require (
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
gorm.io/gorm v1.22.5
gorm.io/gorm v1.25.5
)

require (
github.com/jinzhu/now v1.1.4 // indirect
gorm.io/driver/mysql v1.2.3
github.com/jinzhu/now v1.1.5 // indirect
gorm.io/driver/mysql v1.5.2
)

require (
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/prometheus/client_golang v1.12.1
gopkg.in/guregu/null.v4 v4.0.0
gorm.io/plugin/prometheus v0.0.0-20210820101226-2a49866f83ee
)

require github.com/go-gormigrate/gormigrate/v2 v2.1.1 // indirect
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeME
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gormigrate/gormigrate/v2 v2.1.1 h1:eGS0WTFRV30r103lU8JNXY27KbviRnqqIDobW3EV3iY=
github.com/go-gormigrate/gormigrate/v2 v2.1.1/go.mod h1:L7nJ620PFDKei9QOhJzqA8kRCk+E3UbV2f5gv+1ndLc=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY=
Expand All @@ -101,6 +103,8 @@ github.com/go-playground/validator/v10 v10.10.1 h1:uA0+amWMiglNZKZ9FJRKUAe9U3RX9
github.com/go-playground/validator/v10 v10.10.1/go.mod h1:i+3WkQ1FvaUjjxh1kSvIA4dMGDBiPU55YFDl0WbKdWU=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
Expand Down Expand Up @@ -191,6 +195,8 @@ github.com/jinzhu/now v1.1.2/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/
github.com/jinzhu/now v1.1.3/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jinzhu/now v1.1.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas=
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
Expand Down Expand Up @@ -705,10 +711,15 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/mysql v1.2.3 h1:cZqzlOfg5Kf1VIdLC1D9hT6Cy9BgxhExLj/2tIgUe7Y=
gorm.io/driver/mysql v1.2.3/go.mod h1:qsiz+XcAyMrS6QY+X3M9R6b/lKM1imKmcuK9kac5LTo=
gorm.io/driver/mysql v1.5.2 h1:QC2HRskSE75wBuOxe0+iCkyJZ+RqpudsQtqkp+IMuXs=
gorm.io/driver/mysql v1.5.2/go.mod h1:pQLhh1Ut/WUAySdTHwBpBv6+JKcj+ua4ZFx1QQTBzb8=
gorm.io/gorm v1.21.13/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0=
gorm.io/gorm v1.22.4/go.mod h1:1aeVC+pe9ZmvKZban/gW4QPra7PRoTEssyc922qCAkk=
gorm.io/gorm v1.22.5 h1:lYREBgc02Be/5lSCTuysZZDb6ffL2qrat6fg9CFbvXU=
gorm.io/gorm v1.22.5/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls=
gorm.io/gorm v1.25.5/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
gorm.io/plugin/prometheus v0.0.0-20210820101226-2a49866f83ee h1:UhePWayrSEXncx1x7UdBNvUfbuxrQOk9LV44glIJElQ=
gorm.io/plugin/prometheus v0.0.0-20210820101226-2a49866f83ee/go.mod h1:tu0ajC4OlzwAK6aSL4NQjAvvbBeWcVbv2X285icuEm0=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func main() {
panic(err)
}

err = model.Migrate()
_, err = model.Migrate()
if err != nil {
panic(err)
}
Expand Down
24 changes: 24 additions & 0 deletions model/current.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package model

import (
"github.com/go-gormigrate/gormigrate/v2"
)

// Migrations is all db migrations
func Migrations() []*gormigrate.Migration {
return []*gormigrate.Migration{}
}

func AllTables() []interface{} {
return []interface{}{
&Questionnaires{},
&Questions{},
&Respondents{},
&Responses{},
&Administrators{},
&Options{},
&ScaleLabels{},
&Targets{},
&Validations{},
}
}
42 changes: 21 additions & 21 deletions model/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,14 @@ import (
"fmt"
"os"

"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"gorm.io/plugin/prometheus"
)

var (
db *gorm.DB
allTables = []interface{}{
Questionnaires{},
Questions{},
Respondents{},
Responses{},
Administrators{},
Options{},
ScaleLabels{},
Targets{},
Validations{},
}
)
var db *gorm.DB

// EstablishConnection DBと接続
func EstablishConnection(isProduction bool) error {
Expand Down Expand Up @@ -83,12 +71,24 @@ func EstablishConnection(isProduction bool) error {
return nil
}

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

return nil
m.InitSchema(func(db *gorm.DB) error {
init = true

return db.AutoMigrate(AllTables()...)
})
err = m.Migrate()
return
}

// Migrate DBのMigrationを行う
// func Migrate() error {
// err := db.AutoMigrate(allTables...)
// if err != nil {
// return fmt.Errorf("failed in table's migration: %w", err)
// }

// return nil
// }
4 changes: 2 additions & 2 deletions model/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ var (
targetImpl = new(Target)
)

//TestMain テストのmain
// TestMain テストのmain
func TestMain(m *testing.M) {
err := EstablishConnection(true)
if err != nil {
panic(err)
}

err = Migrate()
_, err = Migrate()
if err != nil {
panic(err)
}
Expand Down

0 comments on commit e412a04

Please sign in to comment.