Add MySQL initialization and user model size constraint
This commit is contained in:
parent
d83f6c1145
commit
82e7fd1d01
@ -5,6 +5,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/dungnt11/todoms_golang/global"
|
"github.com/dungnt11/todoms_golang/global"
|
||||||
|
"github.com/dungnt11/todoms_golang/internal/models"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"gorm.io/driver/mysql"
|
"gorm.io/driver/mysql"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
@ -45,3 +46,18 @@ func setPool(db *gorm.DB) {
|
|||||||
sqlDb.SetMaxOpenConns(m.MaxOpenConns)
|
sqlDb.SetMaxOpenConns(m.MaxOpenConns)
|
||||||
sqlDb.SetConnMaxLifetime(time.Duration(m.ConnMaxLifetime) * time.Second)
|
sqlDb.SetConnMaxLifetime(time.Duration(m.ConnMaxLifetime) * time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func defaultInitMysql() {
|
||||||
|
isInitUser := global.Mdb.Migrator().HasTable(&models.User{})
|
||||||
|
isInitProduct := global.Mdb.Migrator().HasTable(&models.Product{})
|
||||||
|
|
||||||
|
if !isInitUser {
|
||||||
|
global.Mdb.AutoMigrate(&models.User{})
|
||||||
|
global.Logger.Info("Initialized User table")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !isInitProduct {
|
||||||
|
global.Mdb.AutoMigrate(&models.Product{})
|
||||||
|
global.Logger.Info("Initialized Product table")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -38,6 +38,8 @@ func Run() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defaultInitMysql()
|
||||||
|
|
||||||
// Khởi tạo router
|
// Khởi tạo router
|
||||||
r := provideRouter()
|
r := provideRouter()
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
// User đại diện cho người dùng trong hệ thống
|
// User đại diện cho người dùng trong hệ thống
|
||||||
type User struct {
|
type User struct {
|
||||||
ID uint `json:"id" gorm:"primaryKey"`
|
ID uint `json:"id" gorm:"primaryKey"`
|
||||||
Username string `json:"username" gorm:"uniqueIndex;not null"`
|
Username string `json:"username" gorm:"uniqueIndex;not null;size:255"`
|
||||||
Password string `json:"-" gorm:"not null"` // Không trả về password trong JSON
|
Password string `json:"-" gorm:"not null"` // Không trả về password trong JSON
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
Loading…
x
Reference in New Issue
Block a user