Skip to content

Commit

Permalink
feat: [#272] The Orm module can set DSN directly (#763)
Browse files Browse the repository at this point in the history
* feat: [#272] The Orm module can set DSN directly

* fix test

* remove sqlite
  • Loading branch information
hwbrzzl authored Dec 16, 2024
1 parent 44c5de1 commit 541d09f
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions contracts/database/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func (d Driver) String() string {

// Config Used in config/database.go
type Config struct {
Dsn string
Host string
Port int
Database string
Expand Down
3 changes: 3 additions & 0 deletions database/db/config_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ func (c *ConfigBuilder) fillDefault(configs []database.Config) []database.FullCo
Singular: c.config.GetBool(fmt.Sprintf("database.connections.%s.singular", c.connection)),
}
if driver != database.DriverSqlite {
if fullConfig.Dsn == "" {
fullConfig.Dsn = c.config.GetString(fmt.Sprintf("database.connections.%s.dsn", c.connection))
}
if fullConfig.Host == "" {
fullConfig.Host = c.config.GetString(fmt.Sprintf("database.connections.%s.host", c.connection))
}
Expand Down
5 changes: 5 additions & 0 deletions database/db/config_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func (s *ConfigTestSuite) TestWrites() {
}

func (s *ConfigTestSuite) TestFillDefault() {
dsn := "dsn"
host := "localhost"
port := 3306
database := "forge"
Expand Down Expand Up @@ -133,6 +134,7 @@ func (s *ConfigTestSuite) TestFillDefault() {
s.mockConfig.EXPECT().GetString(fmt.Sprintf("database.connections.%s.prefix", s.connection)).Return(prefix).Once()
s.mockConfig.EXPECT().GetBool(fmt.Sprintf("database.connections.%s.singular", s.connection)).Return(singular).Once()
s.mockConfig.EXPECT().GetString(fmt.Sprintf("database.connections.%s.driver", s.connection)).Return("mysql").Once()
s.mockConfig.EXPECT().GetString(fmt.Sprintf("database.connections.%s.dsn", s.connection)).Return(dsn).Once()
s.mockConfig.EXPECT().GetString(fmt.Sprintf("database.connections.%s.host", s.connection)).Return(host).Once()
s.mockConfig.EXPECT().GetInt(fmt.Sprintf("database.connections.%s.port", s.connection)).Return(port).Once()
s.mockConfig.EXPECT().GetString(fmt.Sprintf("database.connections.%s.database", s.connection)).Return(database).Once()
Expand All @@ -150,6 +152,7 @@ func (s *ConfigTestSuite) TestFillDefault() {
Charset: charset,
Loc: loc,
Config: contractsdatabase.Config{
Dsn: dsn,
Host: host,
Port: port,
Database: database,
Expand All @@ -163,6 +166,7 @@ func (s *ConfigTestSuite) TestFillDefault() {
name: "success when configs have item",
configs: []contractsdatabase.Config{
{
Dsn: dsn,
Host: host,
Port: port,
Database: database,
Expand All @@ -186,6 +190,7 @@ func (s *ConfigTestSuite) TestFillDefault() {
Charset: charset,
Loc: loc,
Config: contractsdatabase.Config{
Dsn: dsn,
Database: database,
Host: host,
Port: port,
Expand Down
4 changes: 4 additions & 0 deletions database/db/dsn.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (
)

func Dsn(config database.FullConfig) string {
if config.Dsn != "" {
return config.Dsn
}

if config.Host == "" && config.Driver != database.DriverSqlite {
return ""
}
Expand Down
9 changes: 9 additions & 0 deletions database/db/dsn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ func TestDsn(t *testing.T) {
Config: database.Config{},
},
},
{
name: "dsn is not empty",
config: database.FullConfig{
Config: database.Config{
Dsn: "dsn",
},
},
expectDsn: "dsn",
},
{
name: "mysql",
config: database.FullConfig{
Expand Down
6 changes: 6 additions & 0 deletions database/gorm/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ func (r *MockMysql) ReadWrite(readDatabaseConfig testing.DatabaseConfig) {
r.mockConfig.On("Get", fmt.Sprintf("database.connections.%s.write", r.connection)).Return([]contractsdatabase.Config{
{Host: "127.0.0.1", Database: r.database, Port: r.port, Username: r.user, Password: r.password},
})
r.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.dsn", r.connection)).Return("")
r.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.prefix", r.connection)).Return("")
r.mockConfig.On("GetBool", fmt.Sprintf("database.connections.%s.singular", r.connection)).Return(false)
r.basic()
Expand All @@ -302,6 +303,7 @@ func (r *MockMysql) single() {
r.mockConfig.On("Get", fmt.Sprintf("database.connections.%s.read", r.connection)).Return(nil)
r.mockConfig.On("Get", fmt.Sprintf("database.connections.%s.write", r.connection)).Return(nil)
r.mockConfig.On("GetBool", "app.debug").Return(true)
r.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.dsn", r.connection)).Return("")
r.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.host", r.connection)).Return("127.0.0.1")
r.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.username", r.connection)).Return(r.user)
r.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.password", r.connection)).Return(r.password)
Expand Down Expand Up @@ -347,6 +349,7 @@ func (r *MockPostgres) ReadWrite(readDatabaseConfig testing.DatabaseConfig) {
r.mockConfig.On("Get", fmt.Sprintf("database.connections.%s.write", r.connection)).Return([]contractsdatabase.Config{
{Host: "127.0.0.1", Database: r.database, Port: r.port, Username: r.user, Password: r.password},
})
r.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.dsn", r.connection)).Return("")
r.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.prefix", r.connection)).Return("")
r.mockConfig.On("GetBool", fmt.Sprintf("database.connections.%s.singular", r.connection)).Return(false)
r.basic()
Expand All @@ -373,6 +376,7 @@ func (r *MockPostgres) basic() {
func (r *MockPostgres) single() {
r.mockConfig.On("Get", fmt.Sprintf("database.connections.%s.read", r.connection)).Return(nil)
r.mockConfig.On("Get", fmt.Sprintf("database.connections.%s.write", r.connection)).Return(nil)
r.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.dsn", r.connection)).Return("")
r.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.host", r.connection)).Return("127.0.0.1")
r.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.username", r.connection)).Return(r.user)
r.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.password", r.connection)).Return(r.password)
Expand Down Expand Up @@ -475,6 +479,7 @@ func (r *MockSqlserver) ReadWrite(readDatabaseConfig testing.DatabaseConfig) {
r.mockConfig.On("Get", fmt.Sprintf("database.connections.%s.write", r.connection)).Return([]contractsdatabase.Config{
{Host: "127.0.0.1", Database: r.database, Port: r.port, Username: r.user, Password: r.password},
})
r.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.dsn", r.connection)).Return("")
r.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.prefix", r.connection)).Return("")
r.mockConfig.On("GetBool", fmt.Sprintf("database.connections.%s.singular", r.connection)).Return(false)
r.basic()
Expand All @@ -498,6 +503,7 @@ func (r *MockSqlserver) basic() {
func (r *MockSqlserver) single() {
r.mockConfig.On("Get", fmt.Sprintf("database.connections.%s.read", r.connection)).Return(nil)
r.mockConfig.On("Get", fmt.Sprintf("database.connections.%s.write", r.connection)).Return(nil)
r.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.dsn", r.connection)).Return("")
r.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.host", r.connection)).Return("127.0.0.1")
r.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.username", r.connection)).Return(r.user)
r.mockConfig.On("GetString", fmt.Sprintf("database.connections.%s.password", r.connection)).Return(r.password)
Expand Down
1 change: 1 addition & 0 deletions foundation/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ func (s *ApplicationTestSuite) TestMakeOrm() {
mockConfig.EXPECT().GetString("database.connections.postgres.driver").Return(contractsdatabase.DriverPostgres.String()).Twice()
mockConfig.EXPECT().GetString("database.connections.postgres.prefix").Return("").Twice()
mockConfig.EXPECT().GetBool("database.connections.postgres.singular").Return(true).Twice()
mockConfig.EXPECT().GetString("database.connections.postgres.dsn").Return("").Twice()
mockConfig.EXPECT().GetString("database.connections.postgres.host").Return("localhost").Twice()
mockConfig.EXPECT().GetString("database.connections.postgres.username").Return(config.Username).Twice()
mockConfig.EXPECT().GetString("database.connections.postgres.password").Return(config.Password).Twice()
Expand Down

0 comments on commit 541d09f

Please sign in to comment.