未验证 提交 fba9f412 编写于 作者: Sliver_Horn's avatar Sliver_Horn 提交者: GitHub

Merge pull request #1081 from pnck/main

fix: 修复多库模式不能使用 pg 的问题
...@@ -11,9 +11,9 @@ type Server struct { ...@@ -11,9 +11,9 @@ type Server struct {
// auto // auto
AutoCode Autocode `mapstructure:"autocode" json:"autocode" yaml:"autocode"` AutoCode Autocode `mapstructure:"autocode" json:"autocode" yaml:"autocode"`
// gorm // gorm
Mysql Mysql `mapstructure:"mysql" json:"mysql" yaml:"mysql"` Mysql Mysql `mapstructure:"mysql" json:"mysql" yaml:"mysql"`
Pgsql Pgsql `mapstructure:"pgsql" json:"pgsql" yaml:"pgsql"` Pgsql Pgsql `mapstructure:"pgsql" json:"pgsql" yaml:"pgsql"`
DBList []DB `mapstructure:"db-list" json:"db-list" yaml:"db-list"` DBList []SpecializedDB `mapstructure:"db-list" json:"db-list" yaml:"db-list"`
// oss // oss
Local Local `mapstructure:"local" json:"local" yaml:"local"` Local Local `mapstructure:"local" json:"local" yaml:"local"`
Qiniu Qiniu `mapstructure:"qiniu" json:"qiniu" yaml:"qiniu"` Qiniu Qiniu `mapstructure:"qiniu" json:"qiniu" yaml:"qiniu"`
......
package config package config
type DB struct { type DsnProvider interface {
Disable bool `mapstructure:"disable" json:"disable" yaml:"disable"` Dsn() string
Type string `mapstructure:"type" json:"type" yaml:"type"` }
AliasName string `mapstructure:"alias-name" json:"alias-name" yaml:"alias-name"`
// Embeded 结构体可以压平到上一层,从而保持 config 文件的结构和原来一样
// 见 playground: https://go.dev/play/p/KIcuhqEoxmY
// GeneralDB 也被 Pgsql 和 Mysql 原样使用
type GeneralDB struct {
Path string `mapstructure:"path" json:"path" yaml:"path"` // 服务器地址:端口 Path string `mapstructure:"path" json:"path" yaml:"path"` // 服务器地址:端口
Port string `mapstructure:"port" json:"port" yaml:"port"` //:端口 Port string `mapstructure:"port" json:"port" yaml:"port"` //:端口
Config string `mapstructure:"config" json:"config" yaml:"config"` // 高级配置 Config string `mapstructure:"config" json:"config" yaml:"config"` // 高级配置
...@@ -13,9 +18,12 @@ type DB struct { ...@@ -13,9 +18,12 @@ type DB struct {
MaxIdleConns int `mapstructure:"max-idle-conns" json:"max-idle-conns" yaml:"max-idle-conns"` // 空闲中的最大连接数 MaxIdleConns int `mapstructure:"max-idle-conns" json:"max-idle-conns" yaml:"max-idle-conns"` // 空闲中的最大连接数
MaxOpenConns int `mapstructure:"max-open-conns" json:"max-open-conns" yaml:"max-open-conns"` // 打开到数据库的最大连接数 MaxOpenConns int `mapstructure:"max-open-conns" json:"max-open-conns" yaml:"max-open-conns"` // 打开到数据库的最大连接数
LogMode string `mapstructure:"log-mode" json:"log-mode" yaml:"log-mode"` // 是否开启Gorm全局日志 LogMode string `mapstructure:"log-mode" json:"log-mode" yaml:"log-mode"` // 是否开启Gorm全局日志
LogZap bool `mapstructure:"log-zap" json:"log-zap" yaml:"log-zap"` LogZap bool `mapstructure:"log-zap" json:"log-zap" yaml:"log-zap"` // 是否通过zap写入日志文件
} }
func (m *DB) Dsn() string { type SpecializedDB struct {
return m.Username + ":" + m.Password + "@tcp(" + m.Path + ":" + m.Port + ")/" + m.Dbname + "?" + m.Config Disable bool `mapstructure:"disable" json:"disable" yaml:"disable"`
Type string `mapstructure:"type" json:"type" yaml:"type"`
AliasName string `mapstructure:"alias-name" json:"alias-name" yaml:"alias-name"`
GeneralDB `yaml:",inline" mapstructure:",squash"`
} }
package config package config
type Mysql struct { type Mysql struct {
Path string `mapstructure:"path" json:"path" yaml:"path"` // 服务器地址 GeneralDB `yaml:",inline" mapstructure:",squash"`
Port string `mapstructure:"port" json:"port" yaml:"port"` // 端口
Config string `mapstructure:"config" json:"config" yaml:"config"` // 高级配置
Dbname string `mapstructure:"db-name" json:"db-name" yaml:"db-name"` // 数据库名
Username string `mapstructure:"username" json:"username" yaml:"username"` // 数据库用户名
Password string `mapstructure:"password" json:"password" yaml:"password"` // 数据库密码
MaxIdleConns int `mapstructure:"max-idle-conns" json:"max-idle-conns" yaml:"max-idle-conns"` // 空闲中的最大连接数
MaxOpenConns int `mapstructure:"max-open-conns" json:"max-open-conns" yaml:"max-open-conns"` // 打开到数据库的最大连接数
LogMode string `mapstructure:"log-mode" json:"log-mode" yaml:"log-mode"` // 是否开启Gorm全局日志
LogZap bool `mapstructure:"log-zap" json:"log-zap" yaml:"log-zap"` // 是否通过zap写入日志文件
} }
func (m *Mysql) Dsn() string { func (m *Mysql) Dsn() string {
......
package config package config
type Pgsql struct { type Pgsql struct {
Path string `mapstructure:"path" json:"path" yaml:"path"` // 服务器地址:端口 GeneralDB `yaml:",inline" mapstructure:",squash"`
Port string `mapstructure:"port" json:"port" yaml:"port"` //:端口
Config string `mapstructure:"config" json:"config" yaml:"config"` // 高级配置
Dbname string `mapstructure:"db-name" json:"db-name" yaml:"db-name"` // 数据库名
Username string `mapstructure:"username" json:"username" yaml:"username"` // 数据库用户名
Password string `mapstructure:"password" json:"password" yaml:"password"` // 数据库密码
MaxIdleConns int `mapstructure:"max-idle-conns" json:"max-idle-conns" yaml:"max-idle-conns"` // 空闲中的最大连接数
MaxOpenConns int `mapstructure:"max-open-conns" json:"max-open-conns" yaml:"max-open-conns"` // 打开到数据库的最大连接数
LogMode string `mapstructure:"log-mode" json:"log-mode" yaml:"log-mode"` // 是否开启Gorm全局日志
LogZap bool `mapstructure:"log-zap" json:"log-zap" yaml:"log-zap"` // 是否通过zap写入日志文件
} }
// Dsn 基于配置文件获取 dsn // Dsn 基于配置文件获取 dsn
......
package initialize package initialize
import ( import (
"github.com/flipped-aurora/gin-vue-admin/server/config"
"github.com/flipped-aurora/gin-vue-admin/server/global" "github.com/flipped-aurora/gin-vue-admin/server/global"
"gorm.io/gorm" "gorm.io/gorm"
) )
...@@ -15,9 +16,9 @@ func DBList() { ...@@ -15,9 +16,9 @@ func DBList() {
} }
switch info.Type { switch info.Type {
case "mysql": case "mysql":
dbMap[info.Dbname] = GormMysqlByConfig(info) dbMap[info.GeneralDB.Dbname] = GormMysqlByConfig(config.Mysql{GeneralDB: info.GeneralDB})
case "pgsql": case "pgsql":
dbMap[info.Dbname] = GormPgSqlByConfig(info) dbMap[info.GeneralDB.Dbname] = GormPgSqlByConfig(config.Pgsql{GeneralDB: info.GeneralDB})
default: default:
continue continue
} }
......
...@@ -32,7 +32,7 @@ func GormMysql() *gorm.DB { ...@@ -32,7 +32,7 @@ func GormMysql() *gorm.DB {
} }
// GormMysqlByConfig 初始化Mysql数据库用过传入配置 // GormMysqlByConfig 初始化Mysql数据库用过传入配置
func GormMysqlByConfig(m config.DB) *gorm.DB { func GormMysqlByConfig(m config.Mysql) *gorm.DB {
if m.Dbname == "" { if m.Dbname == "" {
return nil return nil
} }
......
...@@ -31,7 +31,7 @@ func GormPgSql() *gorm.DB { ...@@ -31,7 +31,7 @@ func GormPgSql() *gorm.DB {
} }
// GormPgSqlByConfig 初始化 Postgresql 数据库 通过参数 // GormPgSqlByConfig 初始化 Postgresql 数据库 通过参数
func GormPgSqlByConfig(p config.DB) *gorm.DB { func GormPgSqlByConfig(p config.Pgsql) *gorm.DB {
if p.Dbname == "" { if p.Dbname == "" {
return nil return nil
} }
......
...@@ -43,15 +43,17 @@ func (i *InitDB) PgsqlEmptyDsn() string { ...@@ -43,15 +43,17 @@ func (i *InitDB) PgsqlEmptyDsn() string {
// Author [SliverHorn](https://github.com/SliverHorn) // Author [SliverHorn](https://github.com/SliverHorn)
func (i *InitDB) ToMysqlConfig() config.Mysql { func (i *InitDB) ToMysqlConfig() config.Mysql {
return config.Mysql{ return config.Mysql{
Path: i.Host, GeneralDB: config.GeneralDB{
Port: i.Port, Path: i.Host,
Dbname: i.DBName, Port: i.Port,
Username: i.UserName, Dbname: i.DBName,
Password: i.Password, Username: i.UserName,
MaxIdleConns: 10, Password: i.Password,
MaxOpenConns: 100, MaxIdleConns: 10,
LogMode: "error", MaxOpenConns: 100,
Config: "charset=utf8mb4&parseTime=True&loc=Local", LogMode: "error",
Config: "charset=utf8mb4&parseTime=True&loc=Local",
},
} }
} }
...@@ -59,14 +61,16 @@ func (i *InitDB) ToMysqlConfig() config.Mysql { ...@@ -59,14 +61,16 @@ func (i *InitDB) ToMysqlConfig() config.Mysql {
// Author [SliverHorn](https://github.com/SliverHorn) // Author [SliverHorn](https://github.com/SliverHorn)
func (i *InitDB) ToPgsqlConfig() config.Pgsql { func (i *InitDB) ToPgsqlConfig() config.Pgsql {
return config.Pgsql{ return config.Pgsql{
Path: i.Host, GeneralDB: config.GeneralDB{
Port: i.Port, Path: i.Host,
Dbname: i.DBName, Port: i.Port,
Username: i.UserName, Dbname: i.DBName,
Password: i.Password, Username: i.UserName,
MaxIdleConns: 10, Password: i.Password,
MaxOpenConns: 100, MaxIdleConns: 10,
LogMode: "error", MaxOpenConns: 100,
Config: "sslmode=disable TimeZone=Asia/Shanghai", LogMode: "error",
Config: "sslmode=disable TimeZone=Asia/Shanghai",
},
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册