chore: seed migration objects batch 5
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"hash/fnv"
|
||||
"hotgo/internal/dao"
|
||||
"hotgo/internal/model/entity"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
@@ -20,14 +21,7 @@ var tenantDbGroups sync.Map
|
||||
|
||||
const registryDbGroup = "imRegistry"
|
||||
|
||||
// RegistryDB returns the tenant registry DB. It falls back to the default DB for
|
||||
// single-database deployments where database.imRegistry is not configured.
|
||||
func RegistryDB(ctx context.Context) gdb.DB {
|
||||
if configs, err := gdb.GetConfigGroup(registryDbGroup); err == nil && len(configs) > 0 {
|
||||
return g.DB(registryDbGroup)
|
||||
}
|
||||
return g.DB()
|
||||
}
|
||||
var registryDbGroupOnce sync.Once
|
||||
|
||||
// DB resolves a tenant registry record and returns the corresponding business DB.
|
||||
func DB(ctx context.Context, tenantId int64) (db gdb.DB, tenant *entity.ImTenant, err error) {
|
||||
@@ -35,7 +29,7 @@ func DB(ctx context.Context, tenantId int64) (db gdb.DB, tenant *entity.ImTenant
|
||||
return nil, nil, gerror.New("租户ID不能为空")
|
||||
}
|
||||
|
||||
if err = RegistryDB(ctx).Model(dao.ImTenant.Table()).Safe().Ctx(ctx).WherePri(tenantId).Scan(&tenant); err != nil {
|
||||
if err = RegistryDB().Model(dao.ImTenant.Table()).Safe().Ctx(ctx).WherePri(tenantId).Scan(&tenant); err != nil {
|
||||
return nil, nil, gerror.Wrap(err, "获取租户信息失败")
|
||||
}
|
||||
if tenant == nil || tenant.Id <= 0 {
|
||||
@@ -65,9 +59,40 @@ func DB(ctx context.Context, tenantId int64) (db gdb.DB, tenant *entity.ImTenant
|
||||
return db, tenant, nil
|
||||
}
|
||||
|
||||
// RegistryDB returns the dedicated tenant registry connection, or the default
|
||||
// HotGo database when a separate registry group is not configured.
|
||||
func RegistryDB() gdb.DB {
|
||||
registryDbGroupOnce.Do(func() {
|
||||
ensureRegistryDBGroup()
|
||||
})
|
||||
if configs, err := gdb.GetConfigGroup(registryDbGroup); err == nil && len(configs) > 0 {
|
||||
return g.DB(registryDbGroup)
|
||||
}
|
||||
return g.DB()
|
||||
}
|
||||
|
||||
func ensureRegistryDBGroup() {
|
||||
if configs, err := gdb.GetConfigGroup(registryDbGroup); err == nil && len(configs) > 0 {
|
||||
configs[0].Prefix = ""
|
||||
_ = gdb.SetConfigGroup(registryDbGroup, configs)
|
||||
return
|
||||
}
|
||||
|
||||
configs, err := gdb.GetConfigGroup("default")
|
||||
if err != nil || len(configs) == 0 {
|
||||
return
|
||||
}
|
||||
cloned := make(gdb.ConfigGroup, len(configs))
|
||||
copy(cloned, configs)
|
||||
for i := range cloned {
|
||||
cloned[i].Prefix = ""
|
||||
}
|
||||
_ = gdb.SetConfigGroup(registryDbGroup, cloned)
|
||||
}
|
||||
|
||||
func tenantLink(ctx context.Context, tenant *entity.ImTenant) (string, error) {
|
||||
if tenant.DbDsn != "" {
|
||||
return tenant.DbDsn, nil
|
||||
return normalizeMySQLTimeOptions(tenant.DbDsn)
|
||||
}
|
||||
if tenant.DbName == "" {
|
||||
return "", gerror.New("租户业务库未配置")
|
||||
@@ -115,10 +140,31 @@ func replaceMySQLDatabase(link string, dbName string) (string, error) {
|
||||
dbStart += 2
|
||||
queryStart := strings.Index(link[dbStart:], "?")
|
||||
if queryStart < 0 {
|
||||
return link[:dbStart] + dbName, nil
|
||||
return normalizeMySQLTimeOptions(link[:dbStart] + dbName)
|
||||
}
|
||||
queryStart += dbStart
|
||||
return link[:dbStart] + dbName + link[queryStart:], nil
|
||||
return normalizeMySQLTimeOptions(link[:dbStart] + dbName + link[queryStart:])
|
||||
}
|
||||
|
||||
func normalizeMySQLTimeOptions(link string) (string, error) {
|
||||
if !strings.HasPrefix(link, "mysql:") {
|
||||
if !strings.Contains(link, "@tcp(") {
|
||||
return "", gerror.New("租户业务库仅支持 MySQL")
|
||||
}
|
||||
link = "mysql:" + link
|
||||
}
|
||||
parts := strings.SplitN(link, "?", 2)
|
||||
values := make(url.Values)
|
||||
if len(parts) == 2 {
|
||||
var err error
|
||||
values, err = url.ParseQuery(parts[1])
|
||||
if err != nil {
|
||||
return "", gerror.Wrap(err, "租户业务库连接参数格式不正确")
|
||||
}
|
||||
}
|
||||
values.Set("loc", "UTC")
|
||||
values.Set("parseTime", "true")
|
||||
return parts[0] + "?" + values.Encode(), nil
|
||||
}
|
||||
|
||||
func hash(s string) uint64 {
|
||||
|
||||
Reference in New Issue
Block a user