Skip to content

Commit b2674c6

Browse files
committed
feat: enable DSN encryption, sql log toggle and auto migrate flag
- chooseDB decrypts SQL_DSN via common.DecryptDSNPassword - InitDB / InitLogDB use common.SqlLogEnabled instead of DebugEnabled for db.Debug() - Register GORM crypto strategy AES plugin on main DB - Migration gated by common.IsAutoMigrate (matching dev's startup flow)
1 parent bfc2f98 commit b2674c6

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

model/main.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ func chooseDB(envName string, isLog bool) (*gorm.DB, error) {
121121
}()
122122
dsn := os.Getenv(envName)
123123
if dsn != "" {
124+
var err error
125+
dsn, err = common.DecryptDSNPassword(dsn)
126+
if err != nil {
127+
common.FatalLog("Failed to decrypt DSN password: " + err.Error())
128+
}
124129
if strings.HasPrefix(dsn, "postgres://") || strings.HasPrefix(dsn, "postgresql://") {
125130
// Use PostgreSQL
126131
common.SysLog("using PostgreSQL as database")
@@ -177,7 +182,10 @@ func chooseDB(envName string, isLog bool) (*gorm.DB, error) {
177182
func InitDB() (err error) {
178183
db, err := chooseDB("SQL_DSN", false)
179184
if err == nil {
180-
if common.DebugEnabled {
185+
if common.SqlLogEnabled {
186+
db = db.Debug()
187+
}
188+
if common.IsAutoMigrate {
181189
db = db.Debug()
182190
}
183191
DB = db
@@ -187,6 +195,9 @@ func InitDB() (err error) {
187195
panic(err)
188196
}
189197
}
198+
if err = RegisterGormCryptoStrategyAES(DB); err != nil {
199+
return err
200+
}
190201
sqlDB, err := DB.DB()
191202
if err != nil {
192203
return err
@@ -195,7 +206,7 @@ func InitDB() (err error) {
195206
sqlDB.SetMaxOpenConns(common.GetEnvOrDefault("SQL_MAX_OPEN_CONNS", 1000))
196207
sqlDB.SetConnMaxLifetime(time.Second * time.Duration(common.GetEnvOrDefault("SQL_MAX_LIFETIME", 60)))
197208

198-
if !common.IsMasterNode {
209+
if !common.IsAutoMigrate {
199210
return nil
200211
}
201212
if common.UsingMySQL {
@@ -217,7 +228,7 @@ func InitLogDB() (err error) {
217228
}
218229
db, err := chooseDB("LOG_SQL_DSN", true)
219230
if err == nil {
220-
if common.DebugEnabled {
231+
if common.SqlLogEnabled {
221232
db = db.Debug()
222233
}
223234
LOG_DB = db
@@ -235,7 +246,7 @@ func InitLogDB() (err error) {
235246
sqlDB.SetMaxOpenConns(common.GetEnvOrDefault("SQL_MAX_OPEN_CONNS", 1000))
236247
sqlDB.SetConnMaxLifetime(time.Second * time.Duration(common.GetEnvOrDefault("SQL_MAX_LIFETIME", 60)))
237248

238-
if !common.IsMasterNode {
249+
if !common.IsAutoMigrate {
239250
return nil
240251
}
241252
common.SysLog("database migration started")

0 commit comments

Comments
 (0)