-
Notifications
You must be signed in to change notification settings - Fork 249
/
database.go
311 lines (295 loc) · 12.5 KB
/
database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package database
import (
gocache "github.com/unionj-cloud/go-doudou/v2/toolkit/gocache/lib/cache"
"github.com/wubin1989/clickhouse"
"github.com/wubin1989/sqlite"
"github.com/wubin1989/sqlserver"
"log"
"os"
"strings"
"time"
"github.com/wubin1989/gorm"
"github.com/wubin1989/gorm/logger"
"github.com/wubin1989/gorm/schema"
"github.com/wubin1989/mysql"
"github.com/wubin1989/postgres"
"github.com/wubin1989/prometheus"
"github.com/unionj-cloud/go-doudou/v2/framework/cache"
"github.com/unionj-cloud/go-doudou/v2/framework/config"
"github.com/unionj-cloud/go-doudou/v2/toolkit/caches"
"github.com/unionj-cloud/go-doudou/v2/toolkit/cast"
"github.com/unionj-cloud/go-doudou/v2/toolkit/errorx"
"github.com/unionj-cloud/go-doudou/v2/toolkit/stringutils"
"github.com/unionj-cloud/go-doudou/v2/toolkit/zlogger"
)
const (
DriverMysql = "mysql"
DriverPostgres = "postgres"
DriverSqlite = "sqlite"
DriverSqlserver = "sqlserver"
DriverTidb = "tidb"
DriverClickhouse = "clickhouse"
)
var Db *gorm.DB
func init() {
if cast.ToBoolOrDefault(config.GddDBDisableAutoConfigure.Load(), config.DefaultGddDBDisableAutoConfigure) {
return
}
slowThreshold, err := time.ParseDuration(config.GddDBLogSlowThreshold.Load())
if err != nil {
zlogger.Debug().Msgf("Parse %s %s as time.Duration failed: %s, use default %s instead.\n", string(config.GddDBLogSlowThreshold), config.GddDBLogSlowThreshold.Load(), err.Error(), config.DefaultGddDBLogSlowThreshold)
slowThreshold, _ = time.ParseDuration(config.DefaultGddDBLogSlowThreshold)
}
logLevel := config.DefaultGddDBLogLevel
if stringutils.IsNotEmpty(config.GddDBLogLevel.Load()) {
switch strings.ToLower(config.GddDBLogLevel.Load()) {
case "silent":
logLevel = logger.Silent
case "error":
logLevel = logger.Error
case "warn":
logLevel = logger.Warn
case "info":
logLevel = logger.Info
}
}
newLogger := logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
logger.Config{
SlowThreshold: slowThreshold,
LogLevel: logLevel,
IgnoreRecordNotFoundError: cast.ToBoolOrDefault(config.GddDBLogIgnoreRecordNotFoundError.Load(), config.DefaultGddDBLogIgnoreRecordNotFoundError),
ParameterizedQueries: cast.ToBoolOrDefault(config.GddDBLogParameterizedQueries.Load(), config.DefaultGddDBLogParameterizedQueries),
Colorful: false,
},
)
gormConf := &gorm.Config{
Logger: newLogger,
DisableForeignKeyConstraintWhenMigrating: true,
SkipDefaultTransaction: cast.ToBoolOrDefault(config.GddDBSkipDefaultTransaction.Load(), config.DefaultGddDBSkipDefaultTransaction),
}
tablePrefix := strings.TrimSuffix(config.GddDBTablePrefix.LoadOrDefault(config.DefaultGddDBTablePrefix), ".")
if stringutils.IsNotEmpty(tablePrefix) {
gormConf.NamingStrategy = schema.NamingStrategy{
TablePrefix: tablePrefix + ".",
}
}
dsn := config.GddDBDsn.Load()
if stringutils.IsEmpty(dsn) {
return
}
driver := config.GddDBDriver.Load()
if stringutils.IsEmpty(driver) {
errorx.Panic("Database driver is missing")
}
switch driver {
case DriverMysql, DriverTidb:
conf := mysql.Config{
DSN: dsn, // data source name
SkipInitializeWithVersion: cast.ToBoolOrDefault(config.GddDBMysqlSkipInitializeWithVersion.Load(), config.DefaultGddDBMysqlSkipInitializeWithVersion),
DefaultStringSize: uint(cast.ToIntOrDefault(config.GddDBMysqlDefaultStringSize.Load(), config.DefaultGddDBMysqlDefaultStringSize)),
DisableWithReturning: cast.ToBoolOrDefault(config.GddDBMysqlDisableWithReturning.Load(), config.DefaultGddDBMysqlDisableWithReturning),
DisableDatetimePrecision: cast.ToBoolOrDefault(config.GddDBMysqlDisableDatetimePrecision.Load(), config.DefaultGddDBMysqlDisableDatetimePrecision),
DontSupportRenameIndex: cast.ToBoolOrDefault(config.GddDBMysqlDontSupportRenameIndex.Load(), config.DefaultGddDBMysqlDontSupportRenameIndex),
DontSupportRenameColumn: cast.ToBoolOrDefault(config.GddDBMysqlDontSupportRenameColumn.Load(), config.DefaultGddDBMysqlDontSupportRenameColumn),
DontSupportForShareClause: cast.ToBoolOrDefault(config.GddDBMysqlDontSupportForShareClause.Load(), config.DefaultGddDBMysqlDontSupportForShareClause),
DontSupportNullAsDefaultValue: cast.ToBoolOrDefault(config.GddDBMysqlDontSupportNullAsDefaultValue.Load(), config.DefaultGddDBMysqlDontSupportNullAsDefaultValue),
DontSupportRenameColumnUnique: cast.ToBoolOrDefault(config.GddDBMysqlDontSupportRenameColumnUnique.Load(), config.DefaultGddDBMysqlDontSupportRenameColumnUnique),
}
Db, err = gorm.Open(mysql.New(conf), gormConf)
case DriverPostgres:
conf := postgres.Config{
DSN: dsn,
PreferSimpleProtocol: cast.ToBoolOrDefault(config.GddDBPostgresPreferSimpleProtocol.Load(), config.DefaultGddDBPostgresPreferSimpleProtocol),
WithoutReturning: cast.ToBoolOrDefault(config.GddDBPostgresWithoutReturning.Load(), config.DefaultGddDBPostgresWithoutReturning),
}
Db, err = gorm.Open(postgres.New(conf), gormConf)
case DriverSqlite:
Db, err = gorm.Open(sqlite.Open(dsn), gormConf)
case DriverSqlserver:
Db, err = gorm.Open(sqlserver.Open(dsn), gormConf)
case DriverClickhouse:
Db, err = gorm.Open(clickhouse.Open(dsn), gormConf)
default:
errorx.Panic("Not support driver")
}
if err != nil {
errorx.Panic(err.Error())
}
sqlDB, err := Db.DB()
if err != nil {
errorx.Panic(err.Error())
}
// SetMaxIdleConns sets the maximum number of connections in the idle connection pool.
sqlDB.SetMaxIdleConns(cast.ToIntOrDefault(config.GddDBMaxIdleConns.Load(), config.DefaultGddDBMaxIdleConns))
// SetMaxOpenConns sets the maximum number of open connections to the database.
sqlDB.SetMaxOpenConns(cast.ToIntOrDefault(config.GddDBMaxOpenConns.Load(), config.DefaultGddDBMaxOpenConns))
maxLifetime, err := time.ParseDuration(config.GddDBConnMaxLifetime.Load())
if err != nil {
zlogger.Debug().Msgf("Parse %s %s as time.Duration failed: %s, use default %d instead.\n", string(config.GddDBConnMaxLifetime), config.GddDBConnMaxLifetime.Load(), err.Error(), config.DefaultGddDBConnMaxLifetime)
maxLifetime = config.DefaultGddDBConnMaxLifetime
}
// SetConnMaxLifetime sets the maximum amount of time a connection may be reused.
sqlDB.SetConnMaxLifetime(maxLifetime)
maxIdleTime, err := time.ParseDuration(config.GddDBConnMaxIdleTime.Load())
if err != nil {
zlogger.Debug().Msgf("Parse %s %s as time.Duration failed: %s, use default %d instead.\n", string(config.GddDBConnMaxIdleTime), config.GddDBConnMaxIdleTime.Load(), err.Error(), config.DefaultGddDBConnMaxIdleTime)
maxIdleTime = config.DefaultGddDBConnMaxIdleTime
}
sqlDB.SetConnMaxIdleTime(maxIdleTime)
if cast.ToBoolOrDefault(config.GddDbPrometheusEnable.Load(), config.DefaultGddDbPrometheusEnable) &&
stringutils.IsNotEmpty(config.GddDbPrometheusDBName.LoadOrDefault(config.DefaultGddDbPrometheusDBName)) {
var collectors []prometheus.MetricsCollector
switch driver {
case DriverMysql, DriverTidb:
collectors = append(collectors, &prometheus.MySQL{})
case DriverPostgres:
collectors = append(collectors, &prometheus.Postgres{})
}
ConfigureMetrics(Db, config.GddDbPrometheusDBName.LoadOrDefault(config.DefaultGddDbPrometheusDBName),
cast.ToUInt32OrDefault(config.GddDbPrometheusRefreshInterval.Load(), config.DefaultGddDbPrometheusRefreshInterval),
nil, collectors...)
}
if cast.ToBoolOrDefault(config.GddDbCacheEnable.Load(), config.DefaultGddDbCacheEnable) && cache.CacheManager != nil {
ConfigureDBCache(Db, cache.CacheManager)
}
}
func NewDb(conf config.Config) (db *gorm.DB) {
slowThreshold, err := time.ParseDuration(conf.Db.Log.SlowThreshold)
if err != nil {
errorx.Panic(err.Error())
}
logLevel := logger.Warn
if stringutils.IsNotEmpty(conf.Db.Log.Level) {
switch strings.ToLower(conf.Db.Log.Level) {
case "silent":
logLevel = logger.Silent
case "error":
logLevel = logger.Error
case "warn":
logLevel = logger.Warn
case "info":
logLevel = logger.Info
}
}
newLogger := logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
logger.Config{
SlowThreshold: slowThreshold,
LogLevel: logLevel,
IgnoreRecordNotFoundError: conf.Db.Log.IgnoreRecordNotFoundError,
ParameterizedQueries: conf.Db.Log.ParameterizedQueries,
Colorful: false,
},
)
gormConf := &gorm.Config{
Logger: newLogger,
DisableForeignKeyConstraintWhenMigrating: true,
SkipDefaultTransaction: cast.ToBoolOrDefault(config.GddDBSkipDefaultTransaction.Load(), config.DefaultGddDBSkipDefaultTransaction),
}
tablePrefix := strings.TrimSuffix(conf.Db.Table.Prefix, ".")
if stringutils.IsNotEmpty(tablePrefix) {
gormConf.NamingStrategy = schema.NamingStrategy{
TablePrefix: tablePrefix + ".",
}
}
dsn := conf.Db.Dsn
if stringutils.IsEmpty(dsn) {
return
}
driver := conf.Db.Driver
if stringutils.IsEmpty(driver) {
errorx.Panic("Database driver is missing")
}
switch driver {
case DriverMysql, DriverTidb:
conf := mysql.Config{
DSN: dsn, // data source name
SkipInitializeWithVersion: conf.Db.Mysql.SkipInitializeWithVersion,
DefaultStringSize: uint(conf.Db.Mysql.DefaultStringSize),
DisableWithReturning: conf.Db.Mysql.DisableWithReturning,
DisableDatetimePrecision: conf.Db.Mysql.DisableDatetimePrecision,
DontSupportRenameIndex: conf.Db.Mysql.DontSupportRenameIndex,
DontSupportRenameColumn: conf.Db.Mysql.DontSupportRenameColumn,
DontSupportForShareClause: conf.Db.Mysql.DontSupportForShareClause,
DontSupportNullAsDefaultValue: conf.Db.Mysql.DontSupportNullAsDefaultValue,
DontSupportRenameColumnUnique: conf.Db.Mysql.DontSupportRenameColumnUnique,
}
db, err = gorm.Open(mysql.New(conf), gormConf)
case DriverPostgres:
conf := postgres.Config{
DSN: dsn,
PreferSimpleProtocol: conf.Db.Postgres.PreferSimpleProtocol,
WithoutReturning: conf.Db.Postgres.WithoutReturning,
}
db, err = gorm.Open(postgres.New(conf), gormConf)
case DriverSqlite:
db, err = gorm.Open(sqlite.Open(dsn), gormConf)
case DriverSqlserver:
db, err = gorm.Open(sqlserver.Open(dsn), gormConf)
case DriverClickhouse:
db, err = gorm.Open(clickhouse.Open(dsn), gormConf)
default:
errorx.Panic("Not support driver")
}
if err != nil {
errorx.Panic(err.Error())
}
sqlDB, err := db.DB()
if err != nil {
errorx.Panic(err.Error())
}
// SetMaxIdleConns sets the maximum number of connections in the idle connection pool.
sqlDB.SetMaxIdleConns(conf.Db.Pool.MaxIdleConns)
// SetMaxOpenConns sets the maximum number of open connections to the database.
sqlDB.SetMaxOpenConns(conf.Db.Pool.MaxOpenConns)
maxLifetime := time.Duration(-1)
if stringutils.IsNotEmpty(conf.Db.Pool.ConnMaxLifetime) {
if value, err := time.ParseDuration(conf.Db.Pool.ConnMaxLifetime); err == nil {
maxLifetime = value
} else {
zlogger.Error().Err(err).Msg(err.Error())
}
}
// SetConnMaxLifetime sets the maximum amount of time a connection may be reused.
sqlDB.SetConnMaxLifetime(maxLifetime)
maxIdleTime := time.Duration(-1)
if stringutils.IsNotEmpty(conf.Db.Pool.ConnMaxIdleTime) {
if value, err := time.ParseDuration(conf.Db.Pool.ConnMaxIdleTime); err == nil {
maxIdleTime = value
} else {
zlogger.Error().Err(err).Msg(err.Error())
}
}
sqlDB.SetConnMaxIdleTime(maxIdleTime)
if conf.Db.Prometheus.Enable &&
stringutils.IsNotEmpty(conf.Db.Prometheus.DBName) {
var collectors []prometheus.MetricsCollector
switch driver {
case DriverMysql, DriverTidb:
collectors = append(collectors, &prometheus.MySQL{})
case DriverPostgres:
collectors = append(collectors, &prometheus.Postgres{})
}
ConfigureMetrics(db, conf.Db.Prometheus.DBName, uint32(conf.Db.Prometheus.RefreshInterval),
nil, collectors...)
}
if conf.Db.Cache.Enable && stringutils.IsNotEmpty(conf.Cache.Stores) {
ConfigureDBCache(db, cache.NewCacheManager(conf))
}
return
}
func ConfigureMetrics(db *gorm.DB, dbName string, refreshInterval uint32, labels map[string]string, collectors ...prometheus.MetricsCollector) {
db.Use(prometheus.New(prometheus.Config{
DBName: dbName, // `DBName` as metrics label
RefreshInterval: refreshInterval, // refresh metrics interval (default 15 seconds)
MetricsCollector: collectors,
Labels: labels,
}))
}
func ConfigureDBCache(db *gorm.DB, cacheManager gocache.CacheInterface[any]) {
db.Use(&caches.Caches{Conf: &caches.Config{
Easer: true,
Cacher: NewCacherAdapter(cacheManager),
}})
}