48 lines
1007 B
Go
48 lines
1007 B
Go
package api
|
|
|
|
import (
|
|
"time"
|
|
|
|
"gitlab.celogeek.com/photos/api/internal/photos/models"
|
|
"gorm.io/driver/mysql"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func (s *Service) Migrate() {
|
|
tx := s.DB.Set("gorm:table_options", "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")
|
|
tx.AutoMigrate(&models.Account{})
|
|
}
|
|
|
|
func (s *Service) DBConfig() {
|
|
s.Config.Mysql.Params = map[string]string{
|
|
"charset": "utf8mb4",
|
|
}
|
|
s.Config.Mysql.Collation = "utf8mb4_bin"
|
|
s.Config.Mysql.ParseTime = true
|
|
s.Config.Mysql.Loc = time.UTC
|
|
s.Config.Mysql.Net = "tcp"
|
|
s.Config.Mysql.InterpolateParams = true
|
|
|
|
mysql.CreateClauses = []string{"INSERT", "VALUES", "ON CONFLICT", "RETURNING"}
|
|
}
|
|
|
|
func (s *Service) DBConnect() {
|
|
db, err := gorm.Open(mysql.Open(s.Config.Mysql.FormatDSN()), &gorm.Config{
|
|
Logger: s.GormLogger,
|
|
SkipDefaultTransaction: true,
|
|
PrepareStmt: true,
|
|
})
|
|
|
|
if err != nil {
|
|
s.Logger.Fatal(err)
|
|
}
|
|
|
|
s.DB = db
|
|
}
|
|
|
|
func (s *Service) SetupDB() {
|
|
s.DBConfig()
|
|
s.DBConnect()
|
|
s.Migrate()
|
|
}
|