Skip to content

Commit 4e57b75

Browse files
committed
Merge remote-tracking branch 'origin/dev-281'
2 parents e439bff + e210002 commit 4e57b75

File tree

6 files changed

+45
-71
lines changed

6 files changed

+45
-71
lines changed

Diff for: server/initialize/gorm_mysql.go

+11-19
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,33 @@ import (
1212
// GormMysql 初始化Mysql数据库
1313
// Author [piexlmax](https://github.com/piexlmax)
1414
// Author [SliverHorn](https://github.com/SliverHorn)
15+
// Author [ByteZhou-2018](https://github.com/ByteZhou-2018)
1516
func GormMysql() *gorm.DB {
1617
m := global.GVA_CONFIG.Mysql
17-
if m.Dbname == "" {
18-
return nil
19-
}
20-
mysqlConfig := mysql.Config{
21-
DSN: m.Dsn(), // DSN data source name
22-
DefaultStringSize: 191, // string 类型字段的默认长度
23-
SkipInitializeWithVersion: false, // 根据版本自动配置
24-
}
25-
if db, err := gorm.Open(mysql.New(mysqlConfig), internal.Gorm.Config(m.Prefix, m.Singular)); err != nil {
26-
return nil
27-
} else {
28-
db.InstanceSet("gorm:table_options", "ENGINE="+m.Engine)
29-
sqlDB, _ := db.DB()
30-
sqlDB.SetMaxIdleConns(m.MaxIdleConns)
31-
sqlDB.SetMaxOpenConns(m.MaxOpenConns)
32-
return db
33-
}
18+
return initMysqlDatabase(m)
3419
}
3520

36-
// GormMysqlByConfig 初始化Mysql数据库用过传入配置
21+
// GormMysqlByConfig 通过传入配置初始化Mysql数据库
3722
func GormMysqlByConfig(m config.Mysql) *gorm.DB {
23+
return initMysqlDatabase(m)
24+
}
25+
26+
// initMysqlDatabase 初始化Mysql数据库的辅助函数
27+
func initMysqlDatabase(m config.Mysql) *gorm.DB {
3828
if m.Dbname == "" {
3929
return nil
4030
}
31+
4132
mysqlConfig := mysql.Config{
4233
DSN: m.Dsn(), // DSN data source name
4334
DefaultStringSize: 191, // string 类型字段的默认长度
4435
SkipInitializeWithVersion: false, // 根据版本自动配置
4536
}
37+
4638
if db, err := gorm.Open(mysql.New(mysqlConfig), internal.Gorm.Config(m.Prefix, m.Singular)); err != nil {
4739
panic(err)
4840
} else {
49-
db.InstanceSet("gorm:table_options", "ENGINE=InnoDB")
41+
db.InstanceSet("gorm:table_options", "ENGINE="+m.Engine)
5042
sqlDB, _ := db.DB()
5143
sqlDB.SetMaxIdleConns(m.MaxIdleConns)
5244
sqlDB.SetMaxOpenConns(m.MaxOpenConns)

Diff for: server/initialize/gorm_oracle.go

+8-15
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,25 @@ import (
1515
// 如果需要Oracle库 放开import里的注释 把下方 mysql.Config 改为 oracle.Config ; mysql.New 改为 oracle.New
1616
func GormOracle() *gorm.DB {
1717
m := global.GVA_CONFIG.Oracle
18-
if m.Dbname == "" {
19-
return nil
20-
}
21-
oracleConfig := mysql.Config{
22-
DSN: m.Dsn(), // DSN data source name
23-
DefaultStringSize: 191, // string 类型字段的默认长度
24-
}
25-
if db, err := gorm.Open(mysql.New(oracleConfig), internal.Gorm.Config(m.Prefix, m.Singular)); err != nil {
26-
panic(err)
27-
} else {
28-
sqlDB, _ := db.DB()
29-
sqlDB.SetMaxIdleConns(m.MaxIdleConns)
30-
sqlDB.SetMaxOpenConns(m.MaxOpenConns)
31-
return db
32-
}
18+
return initOracleDatabase(m)
3319
}
3420

3521
// GormOracleByConfig 初始化Oracle数据库用过传入配置
3622
func GormOracleByConfig(m config.Oracle) *gorm.DB {
23+
return initOracleDatabase(m)
24+
}
25+
26+
// initOracleDatabase 初始化Oracle数据库的辅助函数
27+
func initOracleDatabase(m config.Oracle) *gorm.DB {
3728
if m.Dbname == "" {
3829
return nil
3930
}
31+
4032
oracleConfig := mysql.Config{
4133
DSN: m.Dsn(), // DSN data source name
4234
DefaultStringSize: 191, // string 类型字段的默认长度
4335
}
36+
4437
if db, err := gorm.Open(mysql.New(oracleConfig), internal.Gorm.Config(m.Prefix, m.Singular)); err != nil {
4538
panic(err)
4639
} else {

Diff for: server/initialize/gorm_pgsql.go

+7-16
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,16 @@ import (
1313
// Author [SliverHorn](https://github.com/SliverHorn)
1414
func GormPgSql() *gorm.DB {
1515
p := global.GVA_CONFIG.Pgsql
16-
if p.Dbname == "" {
17-
return nil
18-
}
19-
pgsqlConfig := postgres.Config{
20-
DSN: p.Dsn(), // DSN data source name
21-
PreferSimpleProtocol: false,
22-
}
23-
if db, err := gorm.Open(postgres.New(pgsqlConfig), internal.Gorm.Config(p.Prefix, p.Singular)); err != nil {
24-
return nil
25-
} else {
26-
sqlDB, _ := db.DB()
27-
sqlDB.SetMaxIdleConns(p.MaxIdleConns)
28-
sqlDB.SetMaxOpenConns(p.MaxOpenConns)
29-
return db
30-
}
16+
return initPgSqlDatabase(p)
3117
}
3218

33-
// GormPgSqlByConfig 初始化 Postgresql 数据库 通过参数
19+
// GormPgSqlByConfig 初始化 Postgresql 数据库 通过指定参数
3420
func GormPgSqlByConfig(p config.Pgsql) *gorm.DB {
21+
return initPgSqlDatabase(p)
22+
}
23+
24+
// initPgSqlDatabase 初始化 Postgresql 数据库的辅助函数
25+
func initPgSqlDatabase(p config.Pgsql) *gorm.DB {
3526
if p.Dbname == "" {
3627
return nil
3728
}

Diff for: server/initialize/gorm_sqlite.go

+6-12
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,16 @@ import (
1111
// GormSqlite 初始化Sqlite数据库
1212
func GormSqlite() *gorm.DB {
1313
s := global.GVA_CONFIG.Sqlite
14-
if s.Dbname == "" {
15-
return nil
16-
}
17-
18-
if db, err := gorm.Open(sqlite.Open(s.Dsn()), internal.Gorm.Config(s.Prefix, s.Singular)); err != nil {
19-
panic(err)
20-
} else {
21-
sqlDB, _ := db.DB()
22-
sqlDB.SetMaxIdleConns(s.MaxIdleConns)
23-
sqlDB.SetMaxOpenConns(s.MaxOpenConns)
24-
return db
25-
}
14+
return initSqliteDatabase(s)
2615
}
2716

2817
// GormSqliteByConfig 初始化Sqlite数据库用过传入配置
2918
func GormSqliteByConfig(s config.Sqlite) *gorm.DB {
19+
return initSqliteDatabase(s)
20+
}
21+
22+
// initSqliteDatabase 初始化Sqlite数据库辅助函数
23+
func initSqliteDatabase(s config.Sqlite) *gorm.DB {
3024
if s.Dbname == "" {
3125
return nil
3226
}

Diff for: server/service/system/auto_code_package_test.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package system
22

33
import (
44
"context"
5-
model "github.com/flipped-aurora/gin-vue-admin/server/model/system"
6-
"github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
75
"reflect"
86
"testing"
7+
8+
model "github.com/flipped-aurora/gin-vue-admin/server/model/system"
9+
"github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
910
)
1011

1112
func Test_autoCodePackage_Create(t *testing.T) {
@@ -53,9 +54,10 @@ func Test_autoCodePackage_Create(t *testing.T) {
5354

5455
func Test_autoCodePackage_templates(t *testing.T) {
5556
type args struct {
56-
ctx context.Context
57-
entity model.SysAutoCodePackage
58-
info request.AutoCode
57+
ctx context.Context
58+
entity model.SysAutoCodePackage
59+
info request.AutoCode
60+
isPackage bool
5961
}
6062
tests := []struct {
6163
name string
@@ -78,14 +80,15 @@ func Test_autoCodePackage_templates(t *testing.T) {
7880
Abbreviation: "user",
7981
HumpPackageName: "user",
8082
},
83+
isPackage: false,
8184
},
8285
wantErr: false,
8386
},
8487
}
8588
for _, tt := range tests {
8689
t.Run(tt.name, func(t *testing.T) {
8790
s := &autoCodePackage{}
88-
gotCode, gotEnter, gotCreates, err := s.templates(tt.args.ctx, tt.args.entity, tt.args.info)
91+
gotCode, gotEnter, gotCreates, err := s.templates(tt.args.ctx, tt.args.entity, tt.args.info, tt.args.isPackage)
8992
if (err != nil) != tt.wantErr {
9093
t.Errorf("templates() error = %v, wantErr %v", err, tt.wantErr)
9194
return

Diff for: server/utils/captcha/redis.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"time"
66

77
"github.com/flipped-aurora/gin-vue-admin/server/global"
8-
"github.com/mojocn/base64Captcha"
98
"go.uber.org/zap"
109
)
1110

@@ -23,8 +22,10 @@ type RedisStore struct {
2322
Context context.Context
2423
}
2524

26-
func (rs *RedisStore) UseWithCtx(ctx context.Context) base64Captcha.Store {
27-
rs.Context = ctx
25+
func (rs *RedisStore) UseWithCtx(ctx context.Context) *RedisStore {
26+
if ctx == nil {
27+
rs.Context = ctx
28+
}
2829
return rs
2930
}
3031

0 commit comments

Comments
 (0)