Skip to content

Commit fba9f41

Browse files
authored
Merge pull request flipped-aurora#1081 from pnck/main
fix: 修复多库模式不能使用 pg 的问题
2 parents 118f9b0 + bf72d4f commit fba9f41

File tree

8 files changed

+47
-52
lines changed

8 files changed

+47
-52
lines changed

server/config/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ type Server struct {
1111
// auto
1212
AutoCode Autocode `mapstructure:"autocode" json:"autocode" yaml:"autocode"`
1313
// gorm
14-
Mysql Mysql `mapstructure:"mysql" json:"mysql" yaml:"mysql"`
15-
Pgsql Pgsql `mapstructure:"pgsql" json:"pgsql" yaml:"pgsql"`
16-
DBList []DB `mapstructure:"db-list" json:"db-list" yaml:"db-list"`
14+
Mysql Mysql `mapstructure:"mysql" json:"mysql" yaml:"mysql"`
15+
Pgsql Pgsql `mapstructure:"pgsql" json:"pgsql" yaml:"pgsql"`
16+
DBList []SpecializedDB `mapstructure:"db-list" json:"db-list" yaml:"db-list"`
1717
// oss
1818
Local Local `mapstructure:"local" json:"local" yaml:"local"`
1919
Qiniu Qiniu `mapstructure:"qiniu" json:"qiniu" yaml:"qiniu"`

server/config/db_list.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package config
22

3-
type DB struct {
4-
Disable bool `mapstructure:"disable" json:"disable" yaml:"disable"`
5-
Type string `mapstructure:"type" json:"type" yaml:"type"`
6-
AliasName string `mapstructure:"alias-name" json:"alias-name" yaml:"alias-name"`
3+
type DsnProvider interface {
4+
Dsn() string
5+
}
6+
7+
// Embeded 结构体可以压平到上一层,从而保持 config 文件的结构和原来一样
8+
// 见 playground: https://go.dev/play/p/KIcuhqEoxmY
9+
10+
// GeneralDB 也被 Pgsql 和 Mysql 原样使用
11+
type GeneralDB struct {
712
Path string `mapstructure:"path" json:"path" yaml:"path"` // 服务器地址:端口
813
Port string `mapstructure:"port" json:"port" yaml:"port"` //:端口
914
Config string `mapstructure:"config" json:"config" yaml:"config"` // 高级配置
@@ -13,9 +18,12 @@ type DB struct {
1318
MaxIdleConns int `mapstructure:"max-idle-conns" json:"max-idle-conns" yaml:"max-idle-conns"` // 空闲中的最大连接数
1419
MaxOpenConns int `mapstructure:"max-open-conns" json:"max-open-conns" yaml:"max-open-conns"` // 打开到数据库的最大连接数
1520
LogMode string `mapstructure:"log-mode" json:"log-mode" yaml:"log-mode"` // 是否开启Gorm全局日志
16-
LogZap bool `mapstructure:"log-zap" json:"log-zap" yaml:"log-zap"`
21+
LogZap bool `mapstructure:"log-zap" json:"log-zap" yaml:"log-zap"` // 是否通过zap写入日志文件
1722
}
1823

19-
func (m *DB) Dsn() string {
20-
return m.Username + ":" + m.Password + "@tcp(" + m.Path + ":" + m.Port + ")/" + m.Dbname + "?" + m.Config
24+
type SpecializedDB struct {
25+
Disable bool `mapstructure:"disable" json:"disable" yaml:"disable"`
26+
Type string `mapstructure:"type" json:"type" yaml:"type"`
27+
AliasName string `mapstructure:"alias-name" json:"alias-name" yaml:"alias-name"`
28+
GeneralDB `yaml:",inline" mapstructure:",squash"`
2129
}

server/config/gorm_mysql.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
package config
22

33
type Mysql struct {
4-
Path string `mapstructure:"path" json:"path" yaml:"path"` // 服务器地址
5-
Port string `mapstructure:"port" json:"port" yaml:"port"` // 端口
6-
Config string `mapstructure:"config" json:"config" yaml:"config"` // 高级配置
7-
Dbname string `mapstructure:"db-name" json:"db-name" yaml:"db-name"` // 数据库名
8-
Username string `mapstructure:"username" json:"username" yaml:"username"` // 数据库用户名
9-
Password string `mapstructure:"password" json:"password" yaml:"password"` // 数据库密码
10-
MaxIdleConns int `mapstructure:"max-idle-conns" json:"max-idle-conns" yaml:"max-idle-conns"` // 空闲中的最大连接数
11-
MaxOpenConns int `mapstructure:"max-open-conns" json:"max-open-conns" yaml:"max-open-conns"` // 打开到数据库的最大连接数
12-
LogMode string `mapstructure:"log-mode" json:"log-mode" yaml:"log-mode"` // 是否开启Gorm全局日志
13-
LogZap bool `mapstructure:"log-zap" json:"log-zap" yaml:"log-zap"` // 是否通过zap写入日志文件
4+
GeneralDB `yaml:",inline" mapstructure:",squash"`
145
}
156

167
func (m *Mysql) Dsn() string {

server/config/gorm_pgsql.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
package config
22

33
type Pgsql struct {
4-
Path string `mapstructure:"path" json:"path" yaml:"path"` // 服务器地址:端口
5-
Port string `mapstructure:"port" json:"port" yaml:"port"` //:端口
6-
Config string `mapstructure:"config" json:"config" yaml:"config"` // 高级配置
7-
Dbname string `mapstructure:"db-name" json:"db-name" yaml:"db-name"` // 数据库名
8-
Username string `mapstructure:"username" json:"username" yaml:"username"` // 数据库用户名
9-
Password string `mapstructure:"password" json:"password" yaml:"password"` // 数据库密码
10-
MaxIdleConns int `mapstructure:"max-idle-conns" json:"max-idle-conns" yaml:"max-idle-conns"` // 空闲中的最大连接数
11-
MaxOpenConns int `mapstructure:"max-open-conns" json:"max-open-conns" yaml:"max-open-conns"` // 打开到数据库的最大连接数
12-
LogMode string `mapstructure:"log-mode" json:"log-mode" yaml:"log-mode"` // 是否开启Gorm全局日志
13-
LogZap bool `mapstructure:"log-zap" json:"log-zap" yaml:"log-zap"` // 是否通过zap写入日志文件
4+
GeneralDB `yaml:",inline" mapstructure:",squash"`
145
}
156

167
// Dsn 基于配置文件获取 dsn

server/initialize/db_list.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package initialize
22

33
import (
4+
"github.com/flipped-aurora/gin-vue-admin/server/config"
45
"github.com/flipped-aurora/gin-vue-admin/server/global"
56
"gorm.io/gorm"
67
)
@@ -15,9 +16,9 @@ func DBList() {
1516
}
1617
switch info.Type {
1718
case "mysql":
18-
dbMap[info.Dbname] = GormMysqlByConfig(info)
19+
dbMap[info.GeneralDB.Dbname] = GormMysqlByConfig(config.Mysql{GeneralDB: info.GeneralDB})
1920
case "pgsql":
20-
dbMap[info.Dbname] = GormPgSqlByConfig(info)
21+
dbMap[info.GeneralDB.Dbname] = GormPgSqlByConfig(config.Pgsql{GeneralDB: info.GeneralDB})
2122
default:
2223
continue
2324
}

server/initialize/gorm_mysql.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func GormMysql() *gorm.DB {
3232
}
3333

3434
// GormMysqlByConfig 初始化Mysql数据库用过传入配置
35-
func GormMysqlByConfig(m config.DB) *gorm.DB {
35+
func GormMysqlByConfig(m config.Mysql) *gorm.DB {
3636
if m.Dbname == "" {
3737
return nil
3838
}

server/initialize/gorm_pgsql.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func GormPgSql() *gorm.DB {
3131
}
3232

3333
// GormPgSqlByConfig 初始化 Postgresql 数据库 通过参数
34-
func GormPgSqlByConfig(p config.DB) *gorm.DB {
34+
func GormPgSqlByConfig(p config.Pgsql) *gorm.DB {
3535
if p.Dbname == "" {
3636
return nil
3737
}

server/model/system/request/sys_init.go

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,30 +43,34 @@ func (i *InitDB) PgsqlEmptyDsn() string {
4343
// Author [SliverHorn](https://github.com/SliverHorn)
4444
func (i *InitDB) ToMysqlConfig() config.Mysql {
4545
return config.Mysql{
46-
Path: i.Host,
47-
Port: i.Port,
48-
Dbname: i.DBName,
49-
Username: i.UserName,
50-
Password: i.Password,
51-
MaxIdleConns: 10,
52-
MaxOpenConns: 100,
53-
LogMode: "error",
54-
Config: "charset=utf8mb4&parseTime=True&loc=Local",
46+
GeneralDB: config.GeneralDB{
47+
Path: i.Host,
48+
Port: i.Port,
49+
Dbname: i.DBName,
50+
Username: i.UserName,
51+
Password: i.Password,
52+
MaxIdleConns: 10,
53+
MaxOpenConns: 100,
54+
LogMode: "error",
55+
Config: "charset=utf8mb4&parseTime=True&loc=Local",
56+
},
5557
}
5658
}
5759

5860
// ToPgsqlConfig 转换 config.Pgsql
5961
// Author [SliverHorn](https://github.com/SliverHorn)
6062
func (i *InitDB) ToPgsqlConfig() config.Pgsql {
6163
return config.Pgsql{
62-
Path: i.Host,
63-
Port: i.Port,
64-
Dbname: i.DBName,
65-
Username: i.UserName,
66-
Password: i.Password,
67-
MaxIdleConns: 10,
68-
MaxOpenConns: 100,
69-
LogMode: "error",
70-
Config: "sslmode=disable TimeZone=Asia/Shanghai",
64+
GeneralDB: config.GeneralDB{
65+
Path: i.Host,
66+
Port: i.Port,
67+
Dbname: i.DBName,
68+
Username: i.UserName,
69+
Password: i.Password,
70+
MaxIdleConns: 10,
71+
MaxOpenConns: 100,
72+
LogMode: "error",
73+
Config: "sslmode=disable TimeZone=Asia/Shanghai",
74+
},
7175
}
7276
}

0 commit comments

Comments
 (0)