Skip to content

Commit

Permalink
fix(config): add a specific sql conn string for seeder
Browse files Browse the repository at this point in the history
  • Loading branch information
danvergara committed May 8, 2021
1 parent ec9dfdf commit 56284c8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
15 changes: 12 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,21 @@ jobs:
restore-keys: |
${{ runner.os }}-go-
- name: Migrate db
run: go run cmd/dbmigrate/main.go
env:
DB_USER: dblab_user
DB_PASSWORD: dblab_pass
DB_HOST: localhost
DB_PORT: 3306
DB_NAME: dblab_db
DB_DRIVER: mysql

# Runs go test ./... against mysql container
- name: Test MySQL
run: |
go run cmd/dbmigrate/main.go
# go run cmd/seeder/main.go seed
# go test -v ./...
go run cmd/seeder/main.go seed
go test -v ./...
env:
DB_USER: dblab_user
DB_PASSWORD: dblab_pass
Expand Down
2 changes: 1 addition & 1 deletion cmd/seeder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func handleArgs() {
if len(args) >= 1 {
switch args[0] {
case "seed":
connString := cfg.GetDBConnStr()
connString := cfg.GetSQLXDBConnStr()
db, err := sqlx.Open(cfg.Driver(), connString)
if err != nil {
log.Fatalf("Error opening DB: %v", err)
Expand Down
17 changes: 17 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ func (c *Config) GetDBConnStr() string {
return c.getDBConnStr(c.dbHost, c.dbName)
}

// GetSQLXDBConnStr returns the connection string.
func (c *Config) GetSQLXDBConnStr() string {
return c.getSQLXConnStr(c.dbHost, c.dbName)
}

// GetTestDBConnStr returns the test connection string.
func (c *Config) GetTestDBConnStr() string {
return c.getDBConnStr(c.testDBHost, c.testDBName)
Expand All @@ -62,6 +67,18 @@ func (c *Config) getDBConnStr(dbhost, dbname string) string {
}
}

// getSQLXConnStr returns the connection string based on the provied host and db name.
func (c *Config) getSQLXConnStr(dbhost, dbname string) string {
switch c.dbDriver {
case "postgres":
return fmt.Sprintf("%s://%s:%s@%s:%s/%s?sslmode=disable", c.dbDriver, c.dbUser, c.dbPswd, dbhost, c.dbPort, dbname)
case "mysql":
return fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", c.dbUser, c.dbPswd, dbhost, c.dbPort, dbname)
default:
return ""
}
}

// GetMigration return up or down string to instruct the program if it should migrate database up or down.
func (c *Config) GetMigration() string {
return c.migrate
Expand Down

0 comments on commit 56284c8

Please sign in to comment.