init
This commit is contained in:
39
local/utl/db/db.go
Normal file
39
local/utl/db/db.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"os"
|
||||
"rockhu-bot/local/model"
|
||||
"time"
|
||||
|
||||
"go.uber.org/dig"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func Start(di *dig.Container) {
|
||||
db, err := gorm.Open(postgres.New(postgres.Config{
|
||||
DSN: os.Getenv("CONNECTION_STRINGS"),
|
||||
PreferSimpleProtocol: true,
|
||||
}), &gorm.Config{
|
||||
NowFunc: func() time.Time {
|
||||
utc, _ := time.LoadLocation("")
|
||||
return time.Now().In(utc)
|
||||
}})
|
||||
if err != nil {
|
||||
panic("failed to connect database")
|
||||
}
|
||||
err = di.Provide(func() *gorm.DB {
|
||||
return db
|
||||
})
|
||||
if err != nil {
|
||||
panic("failed to bind database")
|
||||
}
|
||||
Migrate(db)
|
||||
}
|
||||
|
||||
func Migrate(db *gorm.DB) {
|
||||
err := db.AutoMigrate(&model.ConcertModel{})
|
||||
if err != nil {
|
||||
panic("failed to migrate model.ApiModel")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user