feat: 添加数据模型和存储层
- model: JobTemplate、SubmitJobRequest、JobHistoryQuery 等模型定义 - store: NewGormDB MySQL 连接池,使用 zap 日志替代 GORM 默认日志 - store: TemplateStore CRUD 操作,支持 GORM AutoMigrate - NewGormDB 接受 gormLevel 参数,由上层传入配置值 - 完整 TDD 测试覆盖 Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
44
internal/store/mysql.go
Normal file
44
internal/store/mysql.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"gcy_hpc_server/internal/logger"
|
||||
"gcy_hpc_server/internal/model"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// NewGormDB opens a GORM MySQL connection with sensible defaults.
|
||||
func NewGormDB(dsn string, zapLogger *zap.Logger, gormLevel string) (*gorm.DB, error) {
|
||||
gormCfg := &gorm.Config{
|
||||
Logger: logger.NewGormLogger(zapLogger, gormLevel),
|
||||
}
|
||||
db, err := gorm.Open(mysql.Open(dsn), gormCfg)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open gorm mysql: %w", err)
|
||||
}
|
||||
|
||||
sqlDB, err := db.DB()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get underlying sql.DB: %w", err)
|
||||
}
|
||||
|
||||
sqlDB.SetMaxOpenConns(25)
|
||||
sqlDB.SetMaxIdleConns(5)
|
||||
sqlDB.SetConnMaxLifetime(5 * time.Minute)
|
||||
|
||||
if err := sqlDB.Ping(); err != nil {
|
||||
return nil, fmt.Errorf("failed to ping mysql: %w", err)
|
||||
}
|
||||
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// AutoMigrate runs GORM auto-migration for all models.
|
||||
func AutoMigrate(db *gorm.DB) error {
|
||||
return db.AutoMigrate(&model.JobTemplate{})
|
||||
}
|
||||
Reference in New Issue
Block a user