feat(database): 自动迁移表结构并优化数据库初始化函数 将 `AutoMigrate` 逻辑从各数据库初始化方法中提取至统一的 `NewDatabase` 方法内, 避免重复代码。同时修改 `Databases`、`Etcd`、`Memory` 和 `RedisCache` 函数签名, 使其返回实例而非通过参数传递,提高代码可读性和一致性。 ```
28 lines
619 B
Go
28 lines
619 B
Go
package with
|
|
|
|
import (
|
|
"git.apinb.com/bsm-sdk/core/conf"
|
|
"git.apinb.com/bsm-sdk/core/database"
|
|
"git.apinb.com/bsm-sdk/core/print"
|
|
"git.apinb.com/bsm-sdk/core/types"
|
|
"git.apinb.com/bsm-sdk/core/vars"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func Databases(cfg *conf.DBConf, opts *types.SqlOptions) *gorm.DB {
|
|
if cfg == nil || len(cfg.Source) == 0 {
|
|
panic("No Database Source Found !")
|
|
}
|
|
|
|
// print inform.
|
|
print.Info("[BSM - %s] Databases: %v", vars.ServiceKey, cfg)
|
|
|
|
var err error
|
|
db, err := database.NewDatabase(cfg.Driver, cfg.Source, opts)
|
|
if err != nil {
|
|
print.Error("Database Init Failed !")
|
|
panic(err)
|
|
}
|
|
return db
|
|
}
|