From cfc47256532d1613515c903f3e76a260df7b446c Mon Sep 17 00:00:00 2001 From: steden <1470804@qq.com> Date: Mon, 6 Jan 2025 19:03:48 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E8=BF=81=E7=A7=BB=E6=96=B9=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dbConfig.go | 3 +++ internalContext.go | 7 ++++--- tableSet.go | 12 +++++++----- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/dbConfig.go b/dbConfig.go index 4434f0c..e4ba405 100644 --- a/dbConfig.go +++ b/dbConfig.go @@ -2,6 +2,7 @@ package data import ( "fmt" + "github.com/farseer-go/fs/container" "gorm.io/gorm" ) @@ -14,6 +15,8 @@ type dbConfig struct { PoolMinSize int ConnectionString string databaseName string // 数据库名称 + Migrate string // code first + migrated bool // 是否包含自动创建数据库 } // GetDriver 获取对应驱动 diff --git a/internalContext.go b/internalContext.go index ebf44e5..395f067 100644 --- a/internalContext.go +++ b/internalContext.go @@ -36,8 +36,8 @@ type IGetInternalContext interface { type internalContext struct { dbConfig *dbConfig // 数据库配置 IsolationLevel sql.IsolationLevel // 事务等级 - dbName string // 库名 - nameReplacer *strings.Replacer // 替换dbName、tableName + //dbName string // 库名 + nameReplacer *strings.Replacer // 替换dbName、tableName } // RegisterInternalContext 注册内部上下文 @@ -56,6 +56,7 @@ func RegisterInternalContext(key string, configString string) { } config.DataType = strings.ToLower(config.DataType) config.keyName = key + config.migrated = strings.Contains(configString, "Migrate=") // 获取数据库名称 switch config.DataType { @@ -96,7 +97,7 @@ func RegisterInternalContext(key string, configString string) { // 注册上下文 ins := &internalContext{dbConfig: &config} - ins.dbName = config.databaseName + //ins.dbName = config.databaseName ins.nameReplacer = strings.NewReplacer("{database}", config.databaseName) container.RegisterInstance[core.ITransaction](ins, key) diff --git a/tableSet.go b/tableSet.go index f1ec9d3..b6a7d96 100644 --- a/tableSet.go +++ b/tableSet.go @@ -59,9 +59,7 @@ func (receiver *TableSet[Table]) Init(dbContext *internalContext, param map[stri //_ = db.Statement.Parse(t) // db.Statement.Model //tableName := db.Statement.Schema.Table tableName := reflect.TypeOf(t).Name() - if strings.HasSuffix(tableName, "PO") { - tableName = tableName[:len(tableName)-2] - } + tableName = strings.TrimSuffix(tableName, "PO") tableName = schema.NamingStrategy{IdentifierMaxLength: 64}.ColumnName("", tableName) //tableName = snakeString(tableName) receiver.tableName = tableName @@ -73,8 +71,12 @@ func (receiver *TableSet[Table]) Init(dbContext *internalContext, param map[stri ts.dbName = receiver.dbName ts.nameReplacer = receiver.nameReplacer - // 自动创建表 - if migrate, exists := param["migrate"]; exists { + migrate, exists := param["migrate"] + if !exists { + exists = receiver.dbContext.dbConfig.migrated + migrate = receiver.dbContext.dbConfig.Migrate + } + if exists { // 创建表 ts.CreateTable(migrate) // 创建索引