-
Notifications
You must be signed in to change notification settings - Fork 1
feat: [#540] Move the Postgres driver to a single package #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package postgres | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| contractsconfig "github.com/goravel/framework/contracts/config" | ||
| "github.com/goravel/framework/contracts/database" | ||
|
|
||
| "github.com/goravel/postgres/contracts" | ||
| ) | ||
|
|
||
| type ConfigBuilder struct { | ||
| config contractsconfig.Config | ||
| connection string | ||
| } | ||
|
|
||
| func NewConfigBuilder(config contractsconfig.Config, connection string) *ConfigBuilder { | ||
| return &ConfigBuilder{ | ||
| config: config, | ||
| connection: connection, | ||
| } | ||
| } | ||
|
|
||
| func (c *ConfigBuilder) Config() contractsconfig.Config { | ||
| return c.config | ||
| } | ||
|
|
||
| func (c *ConfigBuilder) Connection() string { | ||
| return c.connection | ||
| } | ||
|
|
||
| func (c *ConfigBuilder) Reads() []contracts.FullConfig { | ||
| configs := c.config.Get(fmt.Sprintf("database.connections.%s.read", c.connection)) | ||
| if readConfigs, ok := configs.([]contracts.Config); ok { | ||
| return c.fillDefault(readConfigs) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (c *ConfigBuilder) Writes() []contracts.FullConfig { | ||
| configs := c.config.Get(fmt.Sprintf("database.connections.%s.write", c.connection)) | ||
| if writeConfigs, ok := configs.([]contracts.Config); ok { | ||
| return c.fillDefault(writeConfigs) | ||
| } | ||
|
|
||
| // Use default db configuration when write is empty | ||
| return c.fillDefault([]contracts.Config{{}}) | ||
| } | ||
|
|
||
| func (c *ConfigBuilder) fillDefault(configs []contracts.Config) []contracts.FullConfig { | ||
| if len(configs) == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| var fullConfigs []contracts.FullConfig | ||
| driver := c.config.GetString(fmt.Sprintf("database.connections.%s.driver", c.connection)) | ||
|
|
||
| for _, config := range configs { | ||
| fullConfig := contracts.FullConfig{ | ||
| Config: config, | ||
| Connection: c.connection, | ||
| Driver: driver, | ||
| Prefix: c.config.GetString(fmt.Sprintf("database.connections.%s.prefix", c.connection)), | ||
| Singular: c.config.GetBool(fmt.Sprintf("database.connections.%s.singular", c.connection)), | ||
| NoLowerCase: c.config.GetBool(fmt.Sprintf("database.connections.%s.no_lower_case", c.connection)), | ||
| Sslmode: c.config.GetString(fmt.Sprintf("database.connections.%s.sslmode", c.connection)), | ||
| Timezone: c.config.GetString(fmt.Sprintf("database.connections.%s.timezone", c.connection)), | ||
| } | ||
| if nameReplacer := c.config.Get(fmt.Sprintf("database.connections.%s.name_replacer", c.connection)); nameReplacer != nil { | ||
| if replacer, ok := nameReplacer.(database.Replacer); ok { | ||
| fullConfig.NameReplacer = replacer | ||
| } | ||
| } | ||
| 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)) | ||
| } | ||
| if fullConfig.Port == 0 { | ||
| fullConfig.Port = c.config.GetInt(fmt.Sprintf("database.connections.%s.port", c.connection)) | ||
| } | ||
| if fullConfig.Username == "" { | ||
| fullConfig.Username = c.config.GetString(fmt.Sprintf("database.connections.%s.username", c.connection)) | ||
| } | ||
| if fullConfig.Password == "" { | ||
| fullConfig.Password = c.config.GetString(fmt.Sprintf("database.connections.%s.password", c.connection)) | ||
| } | ||
| if fullConfig.Schema == "" { | ||
| fullConfig.Schema = c.config.GetString(fmt.Sprintf("database.connections.%s.schema", c.connection), "public") | ||
| } | ||
| if config.Database == "" { | ||
| fullConfig.Database = c.config.GetString(fmt.Sprintf("database.connections.%s.database", c.connection)) | ||
| } | ||
| fullConfigs = append(fullConfigs, fullConfig) | ||
| } | ||
|
|
||
| return fullConfigs | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| package postgres | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/suite" | ||
|
|
||
| mocksconfig "github.com/goravel/framework/mocks/config" | ||
| "github.com/goravel/postgres/contracts" | ||
| ) | ||
|
|
||
| type ConfigTestSuite struct { | ||
| suite.Suite | ||
| config *ConfigBuilder | ||
| connection string | ||
| mockConfig *mocksconfig.Config | ||
| } | ||
|
|
||
| func TestConfigTestSuite(t *testing.T) { | ||
| suite.Run(t, &ConfigTestSuite{ | ||
| connection: "mysql", | ||
| }) | ||
| } | ||
|
|
||
| func (s *ConfigTestSuite) SetupTest() { | ||
| s.mockConfig = mocksconfig.NewConfig(s.T()) | ||
| s.config = NewConfigBuilder(s.mockConfig, s.connection) | ||
| } | ||
|
|
||
| func (s *ConfigTestSuite) TestReads() { | ||
| database := "forge" | ||
| prefix := "goravel_" | ||
| singular := false | ||
|
|
||
| // Test when configs is empty | ||
| s.mockConfig.EXPECT().Get("database.connections.mysql.read").Return(nil).Once() | ||
| s.Nil(s.config.Reads()) | ||
|
|
||
| // Test when configs is not empty | ||
| s.mockConfig.EXPECT().Get("database.connections.mysql.read").Return([]contracts.Config{ | ||
| { | ||
| Database: database, | ||
| }, | ||
| }).Once() | ||
| 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().GetBool(fmt.Sprintf("database.connections.%s.no_lower_case", s.connection)).Return(false).Once() | ||
| s.mockConfig.EXPECT().Get(fmt.Sprintf("database.connections.%s.name_replacer", s.connection)).Return(nil).Once() | ||
| s.mockConfig.EXPECT().GetString(fmt.Sprintf("database.connections.%s.driver", s.connection)).Return("postgres").Once() | ||
|
|
||
| s.Equal([]contracts.FullConfig{ | ||
| { | ||
| Connection: s.connection, | ||
| Driver: "postgres", | ||
| Prefix: prefix, | ||
| Config: contracts.Config{ | ||
| Database: database, | ||
| }, | ||
| }, | ||
| }, s.config.Reads()) | ||
| } | ||
|
|
||
| func (s *ConfigTestSuite) TestWrites() { | ||
| database := "forge" | ||
| prefix := "goravel_" | ||
| singular := false | ||
|
|
||
| // Test when configBuilder is empty | ||
| s.mockConfig.EXPECT().Get("database.connections.mysql.write").Return(nil).Once() | ||
| s.mockConfig.EXPECT().GetString(fmt.Sprintf("database.connections.%s.driver", s.connection)).Return("postgres").Once() | ||
| s.mockConfig.EXPECT().GetString(fmt.Sprintf("database.connections.%s.database", s.connection)).Return(database).Once() | ||
| 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().GetBool(fmt.Sprintf("database.connections.%s.no_lower_case", s.connection)).Return(false).Once() | ||
| s.mockConfig.EXPECT().Get(fmt.Sprintf("database.connections.%s.name_replacer", s.connection)).Return(nil).Once() | ||
|
|
||
| s.Equal([]contracts.FullConfig{ | ||
| { | ||
| Connection: s.connection, | ||
| Driver: "postgres", | ||
| Prefix: prefix, | ||
| Config: contracts.Config{ | ||
| Database: database, | ||
| }, | ||
| }, | ||
| }, s.config.Writes()) | ||
|
|
||
| // Test when configBuilder is not empty | ||
| s.mockConfig.EXPECT().Get("database.connections.mysql.write").Return([]contracts.Config{ | ||
| { | ||
| Database: database, | ||
| }, | ||
| }).Once() | ||
| s.mockConfig.EXPECT().GetString(fmt.Sprintf("database.connections.%s.driver", s.connection)).Return("postgres").Once() | ||
| 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().GetBool(fmt.Sprintf("database.connections.%s.no_lower_case", s.connection)).Return(false).Once() | ||
| s.mockConfig.EXPECT().Get(fmt.Sprintf("database.connections.%s.name_replacer", s.connection)).Return(nil).Once() | ||
|
|
||
| s.Equal([]contracts.FullConfig{ | ||
| { | ||
| Connection: s.connection, | ||
| Driver: "postgres", | ||
| Prefix: prefix, | ||
| Config: contracts.Config{ | ||
| Database: database, | ||
| }, | ||
| }, | ||
| }, s.config.Writes()) | ||
| } | ||
|
|
||
| func (s *ConfigTestSuite) TestFillDefault() { | ||
| dsn := "dsn" | ||
| host := "localhost" | ||
| port := 3306 | ||
| database := "forge" | ||
| username := "root" | ||
| password := "123123" | ||
| prefix := "goravel_" | ||
| singular := false | ||
| charset := "utf8mb4" | ||
| loc := "Local" | ||
| nameReplacer := strings.NewReplacer("a", "b") | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| configs []contracts.Config | ||
| setup func() | ||
| expectConfigs []contracts.FullConfig | ||
| }{ | ||
| { | ||
| name: "success when configs is empty", | ||
| setup: func() {}, | ||
| configs: []contracts.Config{}, | ||
| }, | ||
| { | ||
| name: "success when configs have item but key is empty", | ||
| configs: []contracts.Config{{}}, | ||
| setup: func() { | ||
| 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().GetBool(fmt.Sprintf("database.connections.%s.no_lower_case", s.connection)).Return(true).Once() | ||
| s.mockConfig.EXPECT().Get(fmt.Sprintf("database.connections.%s.name_replacer", s.connection)).Return(nameReplacer).Once() | ||
| s.mockConfig.EXPECT().GetString(fmt.Sprintf("database.connections.%s.driver", s.connection)).Return("postgres").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() | ||
| s.mockConfig.EXPECT().GetString(fmt.Sprintf("database.connections.%s.username", s.connection)).Return(username).Once() | ||
| s.mockConfig.EXPECT().GetString(fmt.Sprintf("database.connections.%s.password", s.connection)).Return(password).Once() | ||
| s.mockConfig.EXPECT().GetString(fmt.Sprintf("database.connections.%s.charset", s.connection)).Return(charset).Once() | ||
| s.mockConfig.EXPECT().GetString(fmt.Sprintf("database.connections.%s.loc", s.connection)).Return(loc).Once() | ||
| }, | ||
| expectConfigs: []contracts.FullConfig{ | ||
| { | ||
| Connection: s.connection, | ||
| Driver: "postgres", | ||
| Prefix: prefix, | ||
| Singular: singular, | ||
| Charset: charset, | ||
| Loc: loc, | ||
| NoLowerCase: true, | ||
| NameReplacer: nameReplacer, | ||
| Config: contracts.Config{ | ||
| Dsn: dsn, | ||
| Host: host, | ||
| Port: port, | ||
| Database: database, | ||
| Username: username, | ||
| Password: password, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "success when configs have item", | ||
| configs: []contracts.Config{ | ||
| { | ||
| Dsn: dsn, | ||
| Host: host, | ||
| Port: port, | ||
| Database: database, | ||
| Username: username, | ||
| Password: password, | ||
| }, | ||
| }, | ||
| setup: func() { | ||
| s.mockConfig.EXPECT().GetString(fmt.Sprintf("database.connections.%s.driver", s.connection)).Return("postgres").Once() | ||
| 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().GetBool(fmt.Sprintf("database.connections.%s.no_lower_case", s.connection)).Return(true).Once() | ||
| s.mockConfig.EXPECT().Get(fmt.Sprintf("database.connections.%s.name_replacer", s.connection)).Return(nameReplacer).Once() | ||
| s.mockConfig.EXPECT().GetString(fmt.Sprintf("database.connections.%s.charset", s.connection)).Return(charset).Once() | ||
| s.mockConfig.EXPECT().GetString(fmt.Sprintf("database.connections.%s.loc", s.connection)).Return(loc).Once() | ||
| }, | ||
| expectConfigs: []contracts.FullConfig{ | ||
| { | ||
| Connection: s.connection, | ||
| Driver: "postgres", | ||
| Prefix: prefix, | ||
| Singular: singular, | ||
| Charset: charset, | ||
| Loc: loc, | ||
| NoLowerCase: true, | ||
| NameReplacer: nameReplacer, | ||
| Config: contracts.Config{ | ||
| Dsn: dsn, | ||
| Database: database, | ||
| Host: host, | ||
| Port: port, | ||
| Username: username, | ||
| Password: password, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
| s.Run(test.name, func() { | ||
| test.setup() | ||
| configs := s.config.fillDefault(test.configs) | ||
|
|
||
| s.Equal(test.expectConfigs, configs) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package contracts | ||
|
|
||
| import ( | ||
| contractsconfig "github.com/goravel/framework/contracts/config" | ||
| ) | ||
|
|
||
| type ConfigBuilder interface { | ||
| Config() contractsconfig.Config | ||
| Reads() []FullConfig | ||
| Writes() []FullConfig | ||
| } | ||
|
|
||
| // Replacer replacer interface like strings.Replacer | ||
| type Replacer interface { | ||
| Replace(name string) string | ||
| } | ||
|
|
||
| // Config Used in config/database.go | ||
| type Config struct { | ||
| Dsn string | ||
| Host string | ||
| Port int | ||
| Database string | ||
| Username string | ||
| Password string | ||
| Schema string | ||
| } | ||
|
|
||
| // FullConfig Fill the default value for Config | ||
| type FullConfig struct { | ||
| Config | ||
| Driver string | ||
| Connection string | ||
| Prefix string | ||
| Singular bool | ||
| Charset string // Mysql, Sqlserver | ||
| Loc string // Mysql | ||
| Sslmode string // Postgres | ||
| Timezone string // Postgres | ||
| NoLowerCase bool | ||
| NameReplacer Replacer | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add error handling for type assertions.
The type assertion could panic if the configuration value is of an unexpected type.
func (c *ConfigBuilder) Reads() []contracts.FullConfig { configs := c.config.Get(fmt.Sprintf("database.connections.%s.read", c.connection)) - if readConfigs, ok := configs.([]contracts.Config); ok { + readConfigs, ok := configs.([]contracts.Config) + if configs != nil && !ok { + // Log or handle the type mismatch + return nil + } + if ok { return c.fillDefault(readConfigs) } return nil }📝 Committable suggestion