From baddf9745e3084eddbedb0e843b3dd74df072d18 Mon Sep 17 00:00:00 2001 From: Fanir Date: Tue, 22 Feb 2022 20:06:34 +0100 Subject: [PATCH] fixed migrator and added logging --- cmd/feedizer/internal/database/database.go | 6 +++++- cmd/feedizer/internal/database/migrator/logger.go | 14 ++++++++++++++ .../internal/database/migrator/migrator.go | 10 ++++------ 3 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 cmd/feedizer/internal/database/migrator/logger.go diff --git a/cmd/feedizer/internal/database/database.go b/cmd/feedizer/internal/database/database.go index c580c8f..7720075 100644 --- a/cmd/feedizer/internal/database/database.go +++ b/cmd/feedizer/internal/database/database.go @@ -2,6 +2,7 @@ package database import ( "context" + "errors" "fmt" "log" "time" @@ -14,7 +15,10 @@ var LogName string // ParseConfig parses the database config string and sets LogName. func ParseConfig(config string) (*pgxpool.Config, error) { dbConfig, err := pgxpool.ParseConfig(config) - LogName = fmt.Sprintf("%s@%s:%s/%s", dbConfig.ConnConfig.User, dbConfig.ConnConfig.Host, dbConfig.ConnConfig.Port, dbConfig.ConnConfig.Database) + LogName = fmt.Sprintf("%s@%s:%d/%s", dbConfig.ConnConfig.User, dbConfig.ConnConfig.Host, dbConfig.ConnConfig.Port, dbConfig.ConnConfig.Database) + if dbConfig.ConnConfig.Database == "" { + return nil, errors.New("database name not set, please check your connection string") + } return dbConfig, err } diff --git a/cmd/feedizer/internal/database/migrator/logger.go b/cmd/feedizer/internal/database/migrator/logger.go new file mode 100644 index 0000000..eebc030 --- /dev/null +++ b/cmd/feedizer/internal/database/migrator/logger.go @@ -0,0 +1,14 @@ +package migrator + +import "log" + +type Logger struct { +} + +// Printf is like fmt.Printf +func (l *Logger) Printf(format string, v ...interface{}) { + log.Printf("migrator: "+format, v...) +} + +// Verbose should return true when verbose logging output is wanted +func (l *Logger) Verbose() bool { return true } diff --git a/cmd/feedizer/internal/database/migrator/migrator.go b/cmd/feedizer/internal/database/migrator/migrator.go index 5685baf..ab83c56 100644 --- a/cmd/feedizer/internal/database/migrator/migrator.go +++ b/cmd/feedizer/internal/database/migrator/migrator.go @@ -4,9 +4,9 @@ import ( "github.com/golang-migrate/migrate/v4" migratePGX "github.com/golang-migrate/migrate/v4/database/pgx" "github.com/golang-migrate/migrate/v4/source/iofs" - "github.com/jackc/pgx/v4" "github.com/jackc/pgx/v4/stdlib" + "git.zom.bi/fanir/feedizer/cmd/feedizer/internal/database" "git.zom.bi/fanir/feedizer/models/migrations" ) @@ -23,14 +23,11 @@ func New(dbConfig string) *Migrator { return &Migrator{err: err} } - connConfig, err := pgx.ParseConfig(dbConfig) + poolConfig, err := database.ParseConfig(dbConfig) if err != nil { return &Migrator{err: err} } - connString := stdlib.RegisterConnConfig(connConfig) - - var p migratePGX.Postgres - driver, err := p.Open(connString) + driver, err := migratePGX.WithInstance(stdlib.OpenDB(*poolConfig.ConnConfig), &migratePGX.Config{}) if err != nil { return &Migrator{err: err} } @@ -39,6 +36,7 @@ func New(dbConfig string) *Migrator { if err != nil { return &Migrator{err: err} } + m.Log = &Logger{} return &Migrator{m: m} }