-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Config] Standing up the PostgreSQL config (#28)
- Loading branch information
Showing
8 changed files
with
190 additions
and
36 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,58 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"github.com/artie-labs/reader/constants" | ||
"github.com/artie-labs/transfer/lib/stringutil" | ||
) | ||
|
||
type PostgreSQL struct { | ||
Host string `yaml:"host"` | ||
Port string `yaml:"port"` | ||
Username string `yaml:"userName"` | ||
Password string `yaml:"password"` | ||
Database string `yaml:"database"` | ||
Tables []*PostgreSQLTable `yaml:"tables"` | ||
} | ||
|
||
type PostgreSQLTable struct { | ||
Name string `yaml:"name"` | ||
Schema string `yaml:"schema"` | ||
Limit uint `yaml:"limit"` | ||
OptionalPrimaryKeyValStart string `yaml:"optionalPrimaryKeyValStart"` | ||
OptionalPrimaryKeyValEnd string `yaml:"optionalPrimaryKeyValEnd"` | ||
} | ||
|
||
func (p *PostgreSQLTable) GetLimit() uint { | ||
if p.Limit == 0 { | ||
return constants.DefaultLimit | ||
} | ||
|
||
return p.Limit | ||
} | ||
|
||
func (p *PostgreSQL) Validate() error { | ||
if p == nil { | ||
return fmt.Errorf("postgres config is nil") | ||
} | ||
|
||
if stringutil.Empty(p.Host, p.Port, p.Username, p.Password, p.Database) { | ||
return fmt.Errorf("one of the postgresql settings is empty: host, port, username, password, database") | ||
} | ||
|
||
if len(p.Tables) == 0 { | ||
return fmt.Errorf("no tables passed in") | ||
} | ||
|
||
for _, table := range p.Tables { | ||
if table.Name == "" { | ||
return fmt.Errorf("table name must be passed in") | ||
} | ||
|
||
if table.Schema == "" { | ||
return fmt.Errorf("schema must be passed in") | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains 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,82 @@ | ||
package config | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestPostgresValidate(t *testing.T) { | ||
{ | ||
// Config is empty | ||
var p *PostgreSQL | ||
assert.ErrorContains(t, p.Validate(), "postgres config is nil") | ||
} | ||
{ | ||
// Host, port, username, password, database are empty | ||
p := &PostgreSQL{} | ||
assert.ErrorContains(t, p.Validate(), "one of the postgresql settings is empty: host, port, username, password, database") | ||
} | ||
{ | ||
// Tables are empty | ||
p := &PostgreSQL{ | ||
Host: "host", | ||
Port: "port", | ||
Username: "username", | ||
Password: "password", | ||
Database: "database", | ||
} | ||
|
||
assert.ErrorContains(t, p.Validate(), "no tables passed in") | ||
} | ||
{ | ||
// No table name | ||
p := &PostgreSQL{ | ||
Host: "host", | ||
Port: "port", | ||
Username: "username", | ||
Password: "password", | ||
Database: "database", | ||
Tables: []*PostgreSQLTable{ | ||
{ | ||
Schema: "schema", | ||
}, | ||
}, | ||
} | ||
|
||
assert.ErrorContains(t, p.Validate(), "table name must be passed in") | ||
} | ||
{ | ||
// No schema name | ||
p := &PostgreSQL{ | ||
Host: "host", | ||
Port: "port", | ||
Username: "username", | ||
Password: "password", | ||
Database: "database", | ||
Tables: []*PostgreSQLTable{ | ||
{ | ||
Name: "name", | ||
}, | ||
}, | ||
} | ||
|
||
assert.ErrorContains(t, p.Validate(), "schema must be passed in") | ||
} | ||
{ | ||
// Valid | ||
p := &PostgreSQL{ | ||
Host: "host", | ||
Port: "port", | ||
Username: "username", | ||
Password: "password", | ||
Database: "database", | ||
Tables: []*PostgreSQLTable{ | ||
{ | ||
Name: "name", | ||
Schema: "schema", | ||
}, | ||
}, | ||
} | ||
assert.NoError(t, p.Validate()) | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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