2026-04-09 10:01:23 +08:00
|
|
|
package bootstrap
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2026-04-26 20:52:22 +08:00
|
|
|
"log"
|
2026-04-09 10:01:23 +08:00
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
2026-08-21 00:41:07 +08:00
|
|
|
"code.tczkiot.com/wlw/ai-agent/internal/pkg/config"
|
2026-04-09 10:01:23 +08:00
|
|
|
|
|
|
|
|
"github.com/mlogclub/simple/sqls"
|
2026-08-20 21:46:55 +08:00
|
|
|
"gorm.io/driver/postgres"
|
2026-04-09 10:01:23 +08:00
|
|
|
"gorm.io/gorm"
|
|
|
|
|
|
|
|
|
|
// "gorm.io/gorm/logger"
|
2026-04-26 20:52:22 +08:00
|
|
|
"gorm.io/gorm/logger"
|
2026-04-09 10:01:23 +08:00
|
|
|
"gorm.io/gorm/schema"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func InitDB(cfg config.DBConfig) (*gorm.DB, error) {
|
2026-08-20 21:46:55 +08:00
|
|
|
dialector, err := newDialector(cfg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
db, err := gorm.Open(dialector, &gorm.Config{
|
2026-04-26 20:52:22 +08:00
|
|
|
Logger: logger.New(
|
|
|
|
|
log.New(os.Stdout, "\r\n", log.LstdFlags),
|
|
|
|
|
logger.Config{
|
|
|
|
|
SlowThreshold: time.Second,
|
|
|
|
|
LogLevel: logger.Warn,
|
|
|
|
|
IgnoreRecordNotFoundError: true,
|
|
|
|
|
Colorful: true,
|
|
|
|
|
},
|
|
|
|
|
),
|
2026-04-09 10:01:23 +08:00
|
|
|
NamingStrategy: schema.NamingStrategy{
|
|
|
|
|
TablePrefix: "t_",
|
|
|
|
|
SingularTable: true,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sqlDB, err := db.DB()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if cfg.MaxIdleConns > 0 {
|
|
|
|
|
sqlDB.SetMaxIdleConns(cfg.MaxIdleConns)
|
|
|
|
|
}
|
|
|
|
|
if cfg.MaxOpenConns > 0 {
|
|
|
|
|
sqlDB.SetMaxOpenConns(cfg.MaxOpenConns)
|
|
|
|
|
}
|
|
|
|
|
if cfg.ConnMaxIdleTimeSeconds > 0 {
|
|
|
|
|
sqlDB.SetConnMaxIdleTime(time.Duration(cfg.ConnMaxIdleTimeSeconds) * time.Second)
|
|
|
|
|
}
|
|
|
|
|
if cfg.ConnMaxLifetimeSeconds > 0 {
|
|
|
|
|
sqlDB.SetConnMaxLifetime(time.Duration(cfg.ConnMaxLifetimeSeconds) * time.Second)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sqls.SetDB(db)
|
|
|
|
|
return db, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-28 22:23:13 +08:00
|
|
|
// UseDatabase scopes the host application's connection to AI Agent tables.
|
|
|
|
|
// The connection pool is shared, while the naming strategy is copied so the
|
|
|
|
|
// host database naming rules remain untouched.
|
|
|
|
|
func UseDatabase(database *gorm.DB, tablePrefix string) error {
|
|
|
|
|
moduleDB, err := ScopedDatabase(database, tablePrefix)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2026-08-20 21:46:55 +08:00
|
|
|
}
|
2026-08-28 22:23:13 +08:00
|
|
|
sqls.SetDB(moduleDB)
|
|
|
|
|
return nil
|
2026-08-20 21:46:55 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-28 22:23:13 +08:00
|
|
|
// ScopedDatabase shares the host connection pool while applying the AI Agent
|
|
|
|
|
// table prefix to an isolated GORM session.
|
|
|
|
|
func ScopedDatabase(database *gorm.DB, tablePrefix string) (*gorm.DB, error) {
|
|
|
|
|
if database == nil {
|
|
|
|
|
return nil, fmt.Errorf("database is required")
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
2026-08-28 22:23:13 +08:00
|
|
|
moduleDB := database.Session(&gorm.Session{NewDB: true})
|
|
|
|
|
moduleConfig := *moduleDB.Config
|
|
|
|
|
moduleConfig.NamingStrategy = schema.NamingStrategy{
|
|
|
|
|
TablePrefix: tablePrefix,
|
|
|
|
|
SingularTable: true,
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
2026-08-28 22:23:13 +08:00
|
|
|
moduleDB.Config = &moduleConfig
|
|
|
|
|
return moduleDB, nil
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-08-28 22:23:13 +08:00
|
|
|
func newDialector(cfg config.DBConfig) (gorm.Dialector, error) {
|
|
|
|
|
switch strings.ToLower(strings.TrimSpace(cfg.Type)) {
|
|
|
|
|
case "postgres", "postgresql":
|
|
|
|
|
return postgres.Open(cfg.DSN), nil
|
|
|
|
|
default:
|
|
|
|
|
return nil, fmt.Errorf("unsupported db type %q: only postgres is supported", cfg.Type)
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
}
|