-
Notifications
You must be signed in to change notification settings - Fork 257
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
Postgres: support a connection string DSN #435
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ import ( | |
"github.com/urfave/cli/v2" | ||
|
||
"github.com/amacneil/dbmate/v2/pkg/dbmate" | ||
"github.com/amacneil/dbmate/v2/pkg/dbutil" | ||
_ "github.com/amacneil/dbmate/v2/pkg/driver/clickhouse" | ||
_ "github.com/amacneil/dbmate/v2/pkg/driver/mysql" | ||
_ "github.com/amacneil/dbmate/v2/pkg/driver/postgres" | ||
|
@@ -225,11 +226,11 @@ func loadDotEnv() { | |
// action wraps a cli.ActionFunc with dbmate initialization logic | ||
func action(f func(*dbmate.DB, *cli.Context) error) cli.ActionFunc { | ||
return func(c *cli.Context) error { | ||
u, err := getDatabaseURL(c) | ||
u, dsn, err := getDatabaseConfig(c) | ||
if err != nil { | ||
return err | ||
} | ||
db := dbmate.New(u) | ||
db := dbmate.NewWithDSN(u, dsn) | ||
db.AutoDumpSchema = !c.Bool("no-dump-schema") | ||
db.MigrationsDir = c.String("migrations-dir") | ||
db.MigrationsTableName = c.String("migrations-table") | ||
|
@@ -245,7 +246,7 @@ func action(f func(*dbmate.DB, *cli.Context) error) cli.ActionFunc { | |
} | ||
|
||
// getDatabaseURL returns the current database url from cli flag or environment variable | ||
func getDatabaseURL(c *cli.Context) (u *url.URL, err error) { | ||
func getDatabaseConfig(c *cli.Context) (*url.URL, *dbutil.DSN, error) { | ||
// check --url flag first | ||
value := c.String("url") | ||
if value == "" { | ||
|
@@ -254,7 +255,16 @@ func getDatabaseURL(c *cli.Context) (u *url.URL, err error) { | |
value = os.Getenv(env) | ||
} | ||
|
||
return url.Parse(value) | ||
if value == "" { | ||
if dsn := os.Getenv("DATABASE_DSN"); dsn != "" { | ||
driver := os.Getenv("DBMATE_DRIVER") | ||
Comment on lines
+259
to
+260
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not a fan of hard-coding the environment variable names here. I would prefer new CLI args be introduced, with this as a suggested implementation:
|
||
dsn, err := dbmate.NewDSN(driver, dsn) | ||
return nil, &dsn, err | ||
} | ||
} | ||
|
||
u, err := url.Parse(value) | ||
return u, nil, err | ||
} | ||
|
||
// redactLogString attempts to redact passwords from errors | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ import ( | |
"path/filepath" | ||
"regexp" | ||
"sort" | ||
"strings" | ||
"time" | ||
|
||
"github.com/amacneil/dbmate/v2/pkg/dbutil" | ||
|
@@ -30,6 +31,14 @@ var ( | |
ErrCreateDirectory = errors.New("unable to create directory") | ||
) | ||
|
||
func NewDSN(driver string, input string) (dbutil.DSN, error) { | ||
dsn, err := dbutil.NewDSN(driver, input) | ||
if errors.Is(err, dbutil.ErrDriverUnset) { | ||
return dsn, fmt.Errorf("%s: expected DBMATE_DRIVER to be set or driver= in DSN: %w", err.Error(), ErrUnsupportedDriver) | ||
} | ||
return dsn, err | ||
} | ||
|
||
// migrationFileRegexp pattern for valid migration files | ||
var migrationFileRegexp = regexp.MustCompile(`^(\d+).*\.sql$`) | ||
|
||
|
@@ -39,6 +48,8 @@ type DB struct { | |
AutoDumpSchema bool | ||
// DatabaseURL is the database connection string | ||
DatabaseURL *url.URL | ||
// DatabaseDSN is the database connection dsn | ||
DatabaseDSN *dbutil.DSN | ||
// FS specifies the filesystem, or nil for OS filesystem | ||
FS fs.FS | ||
// Log is the interface to write stdout | ||
|
@@ -67,9 +78,15 @@ type StatusResult struct { | |
|
||
// New initializes a new dbmate database | ||
func New(databaseURL *url.URL) *DB { | ||
return NewWithDSN(databaseURL, nil) | ||
} | ||
|
||
// NewWithDSN initializes a new dbmate database with either a url or a dsn | ||
func NewWithDSN(databaseURL *url.URL, dsn *dbutil.DSN) *DB { | ||
return &DB{ | ||
AutoDumpSchema: true, | ||
DatabaseURL: databaseURL, | ||
DatabaseDSN: dsn, | ||
FS: nil, | ||
Log: os.Stdout, | ||
MigrationsDir: "./db/migrations", | ||
|
@@ -84,17 +101,25 @@ func New(databaseURL *url.URL) *DB { | |
|
||
// Driver initializes the appropriate database driver | ||
func (db *DB) Driver() (Driver, error) { | ||
var scheme string | ||
if db.DatabaseURL == nil || db.DatabaseURL.Scheme == "" { | ||
return nil, ErrInvalidURL | ||
if db.DatabaseDSN != nil { | ||
scheme = strings.ToLower(db.DatabaseDSN.Driver()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dislike converting the case at usage time, and would rather that be done in one single place, in |
||
} else { | ||
return nil, ErrInvalidURL | ||
} | ||
} else { | ||
scheme = db.DatabaseURL.Scheme | ||
} | ||
|
||
driverFunc := drivers[db.DatabaseURL.Scheme] | ||
driverFunc := drivers[scheme] | ||
if driverFunc == nil { | ||
return nil, fmt.Errorf("%w: %s", ErrUnsupportedDriver, db.DatabaseURL.Scheme) | ||
return nil, fmt.Errorf("%w: %s", ErrUnsupportedDriver, scheme) | ||
} | ||
|
||
config := DriverConfig{ | ||
DatabaseURL: db.DatabaseURL, | ||
DatabaseDSN: db.DatabaseDSN, | ||
Log: db.Log, | ||
MigrationsTableName: db.MigrationsTableName, | ||
} | ||
|
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.
fix:
DATABBASE_URL
->DATABASE_URL