fix(migrator): honor DatabaseURL connection string in v3 migrator

The v3->v4 migrator built its DSN from component fields only, ignoring
DatabaseURL - so configs that set only a connection string (required
for postgres unix sockets, e.g. "host=/var/run/postgresql dbname=...")
fell back to a host/port DSN and failed to connect. The migrator now
reads DatabaseURL and passes it to gorm verbatim, matching the main
client. Closes #42.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
pull/3582/head
Tomas Dvorak 2 weeks ago
parent 395b708b9d
commit e1575e3d97

@ -18,6 +18,7 @@ type database struct {
Port int Port int
Charset string Charset string
UnixSocket bool UnixSocket bool
DatabaseURL string
} }
// system 系统通用配置 // system 系统通用配置

@ -34,6 +34,16 @@ func Init() error {
confDBType = "mysql" confDBType = "mysql"
} }
// A full connection string overrides component fields - required for
// postgres unix sockets ("host=/var/run/postgresql dbname=..."), which
// the component fields cannot express.
if conf.DatabaseConfig.DatabaseURL != "" {
dialect := confDBType
if dialect == "UNSET" || dialect == "sqlite" {
dialect = "sqlite3"
}
db, err = gorm.Open(dialect, conf.DatabaseConfig.DatabaseURL)
} else {
switch confDBType { switch confDBType {
case "UNSET", "sqlite": case "UNSET", "sqlite":
// 未指定数据库或者明确指定为 sqlite 时,使用 SQLite 数据库 // 未指定数据库或者明确指定为 sqlite 时,使用 SQLite 数据库
@ -65,6 +75,7 @@ func Init() error {
default: default:
return fmt.Errorf("unsupported database type %q", confDBType) return fmt.Errorf("unsupported database type %q", confDBType)
} }
}
//db.SetLogger(util.Log()) //db.SetLogger(util.Log())
if err != nil { if err != nil {

Loading…
Cancel
Save