Skip to content

Commit

Permalink
feat: support customize dsn for mysql and pg (#6031)
Browse files Browse the repository at this point in the history
* support for unixsocket to connect to mysql

* feat: customize dsn for mysql and pg

---------

Co-authored-by: Andy Hsu <[email protected]>
  • Loading branch information
Mars160 and xhofe authored Feb 23, 2024
1 parent 424ab2d commit 1f83550
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
7 changes: 7 additions & 0 deletions internal/bootstrap/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,21 @@ func InitDB() {
}
case "mysql":
{
//[username[:password]@][protocol[(address)]]/dbname[?param1=value1&...&paramN=valueN]
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local&tls=%s",
database.User, database.Password, database.Host, database.Port, database.Name, database.SSLMode)
if database.DSN != "" {
dsn = database.DSN
}
dB, err = gorm.Open(mysql.Open(dsn), gormConfig)
}
case "postgres":
{
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=Asia/Shanghai",
database.Host, database.User, database.Password, database.Name, database.Port, database.SSLMode)
if database.DSN != "" {
dsn = database.DSN
}
dB, err = gorm.Open(postgres.Open(dsn), gormConfig)
}
default:
Expand Down
1 change: 1 addition & 0 deletions internal/conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Database struct {
DBFile string `json:"db_file" env:"FILE"`
TablePrefix string `json:"table_prefix" env:"TABLE_PREFIX"`
SSLMode string `json:"ssl_mode" env:"SSL_MODE"`
DSN string `json:"dsn" env:"DSN"`
}

type Scheme struct {
Expand Down

0 comments on commit 1f83550

Please sign in to comment.