Skip to content

Commit

Permalink
fix: Connect using DB_DSN env var
Browse files Browse the repository at this point in the history
The DSN formation scheme is too rigid. There are so many possible
connection parameters, that supporting them all is just infeasible. It's
better to use the same piece of data for connecting to the DB in the
first place, that is the data source name. The standard library already
works this way and it should be a little easier to use godfish now that
there are fewer environment variables to manage.
  • Loading branch information
rafaelespinoza committed Apr 19, 2021
1 parent 6ba4d3e commit 1dc54d9
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 22 deletions.
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,9 @@ set, then it checks the configuration file.

#### connecting to the db

Database connection parameters are always read from environment variables. The
ones to set are:

Database connection parameters are always read from environment variables. Set:
```
DB_HOST=
DB_NAME=
DB_PASSWORD=
DB_PORT=
DB_USER=
DB_DSN=
```

#### configure file paths
Expand Down
4 changes: 4 additions & 0 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ func NewDriver(dsn DSN, migConf *MigrationsConf) (driver Driver, err error) {
// output will be passed to the standard library's sql.Open method.
type DSN interface {
// Boot takes inputs from the host environment so it can create a Driver.
//
// Deprecated: Set the DB_DSN environment variable instead of using this.
Boot(ConnectionParams) error
// NewDriver calls the constructor of the corresponding Driver.
NewDriver(*MigrationsConf) (Driver, error)
Expand All @@ -59,6 +61,8 @@ type DSN interface {
}

// ConnectionParams is what to use when initializing a DSN.
//
// Deprecated: Set the DB_DSN environment variable instead of using this.
type ConnectionParams struct {
Encoding string // Encoding is the client encoding for the connection.
Host string // Host is the name of the host to connect to.
Expand Down
6 changes: 2 additions & 4 deletions drivers/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mysql
import (
"database/sql"
"fmt"
"os"
"os/exec"
"regexp"
"strings"
Expand Down Expand Up @@ -32,10 +33,7 @@ func (p *DSN) NewDriver(migConf *godfish.MigrationsConf) (godfish.Driver, error)

// String generates a data source name (or connection URL) based on the fields.
func (p *DSN) String() string {
return fmt.Sprintf(
"%s:%s@tcp(%s:%s)/%s",
p.User, p.Pass, p.Host, p.Port, p.Name,
)
return os.Getenv("DB_DSN")
}

// driver implements the godfish.Driver interface for mysql databases.
Expand Down
6 changes: 2 additions & 4 deletions drivers/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"database/sql"
"fmt"
"os"
"os/exec"

"github.com/lib/pq"
Expand Down Expand Up @@ -31,10 +32,7 @@ func (p *DSN) NewDriver(migConf *godfish.MigrationsConf) (godfish.Driver, error)

// String generates a data source name (or connection URL) based on the fields.
func (p *DSN) String() string {
return fmt.Sprintf(
"postgresql://%s:%s/%s?client_encoding=%s&sslmode=require",
p.Host, p.Port, p.Name, p.Encoding,
)
return os.Getenv("DB_DSN")
}

// driver implements the Driver interface for postgres databases.
Expand Down
8 changes: 2 additions & 6 deletions internal/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,8 @@ Description:
Configuration options are set with flags or with a configuration file. Options
specified via flags will take precedence over the config file.
Specify database connection params with environment variables:
DB_HOST=
DB_NAME=
DB_PASSWORD=
DB_PORT=
DB_USER=
Specify database connection params with environment variable:
DB_DSN=
The following flags should go before the command.`,
bin)
Expand Down

0 comments on commit 1dc54d9

Please sign in to comment.