Skip to content

Commit

Permalink
v0.5.0: added address and database config settings for pg
Browse files Browse the repository at this point in the history
  • Loading branch information
chapsuk committed Aug 6, 2018
1 parent d777143 commit 9b598a6
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 7 deletions.
27 changes: 22 additions & 5 deletions config/viper.go → config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,30 @@ func fillDBConfig(cfg *driver.Config) {
if viper.IsSet("postgres.dsn") || viper.IsSet("postgres.host") {
cfg.Dialect = "postgres"
dsn := viper.GetString("postgres.dsn")
if dsn == "" && viper.IsSet("postgres.host") {
dsn = fmt.Sprintf("postgres://%s:%s@%s:%d/%s?%s",
if dsn == "" {
var addr string
if viper.IsSet("postgres.host") {
port := viper.GetInt("postgres.port")
if port == 0 {
port = 5432
}
addr = fmt.Sprintf("%s:%d", viper.GetString("postgres.host"), port)
}

if viper.GetString("postgres.address") != "" {
addr = viper.GetString("postgres.address")
}

db := viper.GetString("postgres.db")
if viper.IsSet("postgres.database") {
db = viper.GetString("postgres.database")
}

dsn = fmt.Sprintf("postgres://%s:%s@%s/%s?%s",
viper.GetString("postgres.user"),
viper.GetString("postgres.password"),
viper.GetString("postgres.host"),
viper.GetInt("postgres.port"),
viper.GetString("postgres.db"),
addr,
db,
viper.GetString("postgres.options"),
)
}
Expand Down
141 changes: 141 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package config

import (
"database/sql"
"strings"
"testing"

"github.com/chapsuk/miga/driver"
"github.com/go-pg/pg"
"github.com/lib/pq"
"github.com/spf13/viper"
)

type (
credentials struct {
Host string
Port string
Address string
User string
Password string
Database string
Options string
}

testCase struct {
creds credentials
shouldErr bool
invalidDSN bool
dsn string
}
)

func TestPostgresDSN(t *testing.T) {
for i, tcase := range pgCredentialsTable {
viper.Set("postgres.host", tcase.creds.Host)
viper.Set("postgres.port", tcase.creds.Port)
viper.Set("postgres.address", tcase.creds.Address)
viper.Set("postgres.user", tcase.creds.User)
viper.Set("postgres.database", tcase.creds.Database)
viper.Set("postgres.password", tcase.creds.Password)
viper.Set("postgres.options", tcase.creds.Options)

dcfg := &driver.Config{}
fillDBConfig(dcfg)

if dcfg.Dsn != tcase.dsn {
t.Fatalf("#%d expected dsn: %s actual: %s", i, tcase.dsn, dcfg.Dsn)
}

opts, errPg := pg.ParseURL(dcfg.Dsn)
if errPg == nil {
db := pg.Connect(opts)
_, err := db.Exec("select 1")
if err != nil {
if !tcase.invalidDSN && !strings.Contains(err.Error(), "connection refused") {
t.Fatalf("Unexpected connection error: %s", err)
}
}
}

uri, errPq := pq.ParseURL(dcfg.Dsn)
if errPq == nil {
db, err := sql.Open("postgres", uri)
if err != nil {
t.Logf("ERROR %s", err)
}

_, err = db.Query("select 1")
if err != nil {
if !tcase.invalidDSN && !strings.Contains(err.Error(), "connection refused") {
t.Fatalf("Unexpected connection error: %s", err)
}
}
}

errs := 0
if tcase.shouldErr {
if errPg == nil {
errs++
t.Logf("#%d expected `pg` dsn parse error, but err is nil", i)
}
if errPq == nil {
errs++
t.Logf("#%d expected `pq` dsn parse error, but err is nil", i)
}
if errs > 0 {
t.Fatalf("expected dsn parse error, but pg: %s pq: %s", errPg, errPq)
}
} else {
if errPg != nil {
errs++
t.Logf("#%d `pg` dsn parse error: %s", i, errPg)
}
if errPq != nil {
errs++
t.Logf("#%d `pq` dsn parse error: %s", i, errPq)
}
if errs > 0 {
t.Fatalf("expected no dsn parse error, but pg %s pq: %s", errPg, errPq)
}
}
}
}

var pgCredentialsTable = []testCase{
{
creds: credentials{
Host: "127.0.0.1",
Port: "11132",
Address: "",
User: "foo",
Password: "bar",
Database: "test",
Options: "",
},
dsn: "postgres://foo:bar@127.0.0.1:11132/test?",
},
{
creds: credentials{
Host: "128.0.0.1",
Port: "1331",
Address: "127.0.0.1:11132",
User: "foo",
Password: "bar",
Database: "test",
Options: "",
},
dsn: "postgres://foo:bar@127.0.0.1:11132/test?",
},
{
creds: credentials{
Address: "127.0.0.1:11132",
User: "foo",
Password: "bar/",
Database: "test",
Options: "",
},
invalidDSN: true,
dsn: "postgres://foo:bar/@127.0.0.1:11132/test?",
},
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

var (
Name = "miga"
Version = "develop"
Version = "v0.5.0"
)

func main() {
Expand Down
3 changes: 2 additions & 1 deletion miga.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ postgres:
password: password
host: 127.0.0.1
port: 5432
db: miga
database: miga
# db: miga # deprecated
options: sslmode=disable

# mysql:
Expand Down

0 comments on commit 9b598a6

Please sign in to comment.