From 21716c4340f9c21c55633fbcf6f8559804b986b9 Mon Sep 17 00:00:00 2001 From: yanweidong Date: Tue, 23 Sep 2025 16:17:03 +0800 Subject: [PATCH] =?UTF-8?q?```=20feat(database):=20=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E8=BF=81=E7=A7=BB=E8=A1=A8=E7=BB=93=E6=9E=84=E5=B9=B6=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E6=95=B0=E6=8D=AE=E5=BA=93=E5=88=9D=E5=A7=8B=E5=8C=96?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将 `AutoMigrate` 逻辑从各数据库初始化方法中提取至统一的 `NewDatabase` 方法内, 避免重复代码。同时修改 `Databases`、`Etcd`、`Memory` 和 `RedisCache` 函数签名, 使其返回实例而非通过参数传递,提高代码可读性和一致性。 ``` --- database/new.go | 22 ++++++++-------------- with/databases.go | 6 +++--- with/etcd.go | 2 +- with/memory.go | 3 ++- with/redis.go | 4 +++- 5 files changed, 17 insertions(+), 20 deletions(-) diff --git a/database/new.go b/database/new.go index 815fbd6..7e9c302 100644 --- a/database/new.go +++ b/database/new.go @@ -34,6 +34,14 @@ func NewDatabase(driver string, dsn []string, options *types.SqlOptions) (db *go return nil, err } + // auto migrate table. + if len(MigrateTables) > 0 { + err = db.AutoMigrate(MigrateTables...) + if err != nil { + return nil, err + } + } + return db, nil } @@ -74,13 +82,6 @@ func NewMysql(dsn []string, options *types.SqlOptions) (gormDb *gorm.DB, err err // SetConnMaxLifetime 设置了连接可复用的最大时间。 sqlDB.SetConnMaxLifetime(options.ConnMaxLifetime) - if len(MigrateTables) > 0 { - err = gormDb.AutoMigrate(MigrateTables...) - if err != nil { - return nil, err - } - } - return gormDb, nil } @@ -119,12 +120,5 @@ func NewPostgres(dsn []string, options *types.SqlOptions) (gormDb *gorm.DB, err // SetConnMaxLifetime 设置了连接可复用的最大时间。 sqlDB.SetConnMaxLifetime(options.ConnMaxLifetime) - if len(MigrateTables) > 0 { - err = gormDb.AutoMigrate(MigrateTables...) - if err != nil { - return nil, err - } - } - return } diff --git a/with/databases.go b/with/databases.go index 2a9c203..037d692 100644 --- a/with/databases.go +++ b/with/databases.go @@ -9,7 +9,7 @@ import ( "gorm.io/gorm" ) -func Databases(cfg *conf.DBConf, db *gorm.DB, opts *types.SqlOptions) { +func Databases(cfg *conf.DBConf, opts *types.SqlOptions) *gorm.DB { if cfg == nil || len(cfg.Source) == 0 { panic("No Database Source Found !") } @@ -18,10 +18,10 @@ func Databases(cfg *conf.DBConf, db *gorm.DB, opts *types.SqlOptions) { print.Info("[BSM - %s] Databases: %v", vars.ServiceKey, cfg) var err error - db, err = database.NewDatabase(cfg.Driver, cfg.Source, opts) + db, err := database.NewDatabase(cfg.Driver, cfg.Source, opts) if err != nil { print.Error("Database Init Failed !") panic(err) } - return + return db } diff --git a/with/etcd.go b/with/etcd.go index 0524584..33cfd09 100644 --- a/with/etcd.go +++ b/with/etcd.go @@ -11,7 +11,7 @@ import ( clientv3 "go.etcd.io/etcd/client/v3" ) -func Etcd(cfg *conf.EtcdConf, cli *clientv3.Client) { +func Etcd(cfg *conf.EtcdConf) (cli *clientv3.Client){ if cfg == nil || len(cfg.Endpoints) == 0 { return } diff --git a/with/memory.go b/with/memory.go index 2236fc7..7a43a19 100644 --- a/with/memory.go +++ b/with/memory.go @@ -9,7 +9,7 @@ import ( "github.com/allegro/bigcache/v3" ) -func Memory(cli *bigcache.BigCache, opts *bigcache.Config) { +func Memory(opts *bigcache.Config) (cli *bigcache.BigCache) { if opts == nil { opts = &bigcache.Config{ Shards: 1024, @@ -32,4 +32,5 @@ func Memory(cli *bigcache.BigCache, opts *bigcache.Config) { } print.Success("[BSM - %s] Memory Cache: Shards=%d, MaxEntrySize=%d", vars.ServiceKey, opts.Shards, opts.MaxEntrySize) + return } diff --git a/with/redis.go b/with/redis.go index 73217bd..901b768 100644 --- a/with/redis.go +++ b/with/redis.go @@ -6,11 +6,13 @@ import ( "git.apinb.com/bsm-sdk/core/vars" ) -func RedisCache(cfg string, cli *redis.RedisClient) { +func RedisCache(cfg string) (cli *redis.RedisClient) { if cfg != "" { cli = redis.New(cfg, vars.ServiceKey) // print inform. print.Info("[BSM - %s] Cache: %s, DBIndex: %d", vars.ServiceKey, cfg, cli.DB) } + + return }