Write log lines to DB.Log output (#195)

This makes it possible to redirect the logs somewhere else, useful if you embed dbmate into your application.
This commit is contained in:
Bouke van der Bijl 2021-02-18 23:10:57 +01:00 committed by GitHub
parent 454f93a000
commit 2bac2c7590
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 26 deletions

View file

@ -4,6 +4,7 @@ import (
"bytes"
"database/sql"
"fmt"
"io"
"net/url"
"strings"
@ -22,6 +23,7 @@ func init() {
type Driver struct {
migrationsTableName string
databaseURL *url.URL
log io.Writer
}
// NewDriver initializes the driver
@ -29,6 +31,7 @@ func NewDriver(config dbmate.DriverConfig) dbmate.Driver {
return &Driver{
migrationsTableName: config.MigrationsTableName,
databaseURL: config.DatabaseURL,
log: config.Log,
}
}
@ -112,7 +115,7 @@ func (drv *Driver) openPostgresDB() (*sql.DB, error) {
// CreateDatabase creates the specified database
func (drv *Driver) CreateDatabase() error {
name := dbutil.DatabaseName(drv.databaseURL)
fmt.Printf("Creating: %s\n", name)
fmt.Fprintf(drv.log, "Creating: %s\n", name)
db, err := drv.openPostgresDB()
if err != nil {
@ -129,7 +132,7 @@ func (drv *Driver) CreateDatabase() error {
// DropDatabase drops the specified database (if it exists)
func (drv *Driver) DropDatabase() error {
name := dbutil.DatabaseName(drv.databaseURL)
fmt.Printf("Dropping: %s\n", name)
fmt.Fprintf(drv.log, "Dropping: %s\n", name)
db, err := drv.openPostgresDB()
if err != nil {
@ -233,7 +236,7 @@ func (drv *Driver) CreateMigrationsTable(db *sql.DB) error {
// in theory we could attempt to create the schema every time, but we avoid that
// in case the user doesn't have permissions to create schemas
fmt.Printf("Creating schema: %s\n", schema)
fmt.Fprintf(drv.log, "Creating schema: %s\n", schema)
_, err = db.Exec(fmt.Sprintf("create schema if not exists %s", schema))
if err != nil {
return err