Skip to content

Commit

Permalink
fix: [#270] Optimize postgres DNS (#309)
Browse files Browse the repository at this point in the history
* fix: [#270] Optimize postgres DNS

* Fix unit test
  • Loading branch information
hwbrzzl authored Oct 5, 2023
1 parent d174dcb commit 9d8d413
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 10 deletions.
4 changes: 2 additions & 2 deletions database/db/dsn.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func (d *DsnImpl) Postgresql(config databasecontract.Config) string {
sslmode := d.config.GetString("database.connections." + d.connection + ".sslmode")
timezone := d.config.GetString("database.connections." + d.connection + ".timezone")

return fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s",
host, config.Username, config.Password, config.Database, config.Port, sslmode, timezone)
return fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=%s&timezone=%s",
config.Username, config.Password, host, config.Port, config.Database, sslmode, timezone)
}

func (d *DsnImpl) Sqlite(config databasecontract.Config) string {
Expand Down
4 changes: 2 additions & 2 deletions database/db/dsn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ func (s *DsnTestSuite) TestPostgresql() {
s.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.sslmode", connection)).Return(sslmode).Once()
s.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.timezone", connection)).Return(timezone).Once()

s.Equal(fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s",
testHost, testUsername, testPassword, testDatabase, testPort, sslmode, timezone), dsn.Postgresql(testConfig))
s.Equal(fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=%s&timezone=%s",
testUsername, testPassword, testHost, testPort, testDatabase, sslmode, timezone), dsn.Postgresql(testConfig))
}

func (s *DsnTestSuite) TestSqlite() {
Expand Down
4 changes: 2 additions & 2 deletions database/gorm/dialector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func (s *DialectorTestSuite) TestPostgresql() {
Return("UTC").Once()
dialectors, err := dialector.Make([]databasecontract.Config{s.config})
s.Equal(postgres.New(postgres.Config{
DSN: fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=%s",
s.config.Host, s.config.Username, s.config.Password, s.config.Database, s.config.Port, "disable", "UTC"),
DSN: fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=%s&timezone=%s",
s.config.Username, s.config.Password, s.config.Host, s.config.Port, s.config.Database, "disable", "UTC"),
}), dialectors[0])
s.Nil(err)
}
Expand Down
4 changes: 1 addition & 3 deletions database/gorm/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/goravel/framework/support/debug"
)

func TestCopyStruct(t *testing.T) {
Expand All @@ -15,7 +13,7 @@ func TestCopyStruct(t *testing.T) {
}

data := copyStruct(Data{Name: "name", age: 18})
debug.Dump(data)

assert.Equal(t, "name", data.Field(0).Interface().(string))
assert.Panics(t, func() {
data.Field(1).Interface()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ require (
golang.org/x/oauth2 v0.10.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/text v0.13.0
golang.org/x/tools v0.9.1 // indirect
google.golang.org/api v0.126.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
Expand Down

0 comments on commit 9d8d413

Please sign in to comment.