Skip to content

Commit

Permalink
Standardize logging in migrator package
Browse files Browse the repository at this point in the history
  • Loading branch information
anbsky committed May 22, 2023
1 parent ae3de30 commit 9cbd551
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
13 changes: 6 additions & 7 deletions pkg/migrator/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"database/sql"
"embed"
"fmt"

"github.com/OdyseeTeam/odysee-api/config"
)

type DSNConfig interface {
GetFullDSN() string
DBName() string
FullDSN() string
}

type DBConfig struct {
Expand Down Expand Up @@ -48,9 +47,9 @@ func (c *DBConfig) GetFullDSN() string {
return fmt.Sprintf("%s/%s?%s", c.dsn, c.dbName, c.connOpts)
}

func ConnectDB(config DSNConfig, migrationsFS ...embed.FS) (*sql.DB, error) {
func ConnectDB(cfg DSNConfig, migrationsFS ...embed.FS) (*sql.DB, error) {
var err error
db, err := sql.Open("postgres", config.GetFullDSN())
db, err := sql.Open("postgres", cfg.GetFullDSN())
if err != nil {
return nil, err
}
Expand All @@ -64,6 +63,6 @@ func ConnectDB(config DSNConfig, migrationsFS ...embed.FS) (*sql.DB, error) {
return db, nil
}

func DBConfigFromApp(cfg config.DBConfig) *DBConfig {
return DefaultDBConfig().DSN(cfg.Connection).Name(cfg.DBName)
func DBConfigFromApp(cfg DSNConfig) *DBConfig {
return DefaultDBConfig().DSN(cfg.GetFullDSN()).Name(cfg.DBName)
}
16 changes: 8 additions & 8 deletions pkg/migrator/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"fmt"
"strings"

"github.com/OdyseeTeam/odysee-api/internal/monitor"
"github.com/sirupsen/logrus"
"github.com/OdyseeTeam/odysee-api/pkg/logging"
"github.com/OdyseeTeam/odysee-api/pkg/logging/zapadapter"

"github.com/lib/pq"
migrate "github.com/rubenv/sql-migrate"
Expand All @@ -19,7 +19,7 @@ type Migrator struct {
db *sql.DB
ms migrate.MigrationSet
source *migrate.EmbedFileSystemMigrationSource
log monitor.ModuleLogger
logger logging.KVLogger
}

func New(db *sql.DB, fs embed.FS) Migrator {
Expand All @@ -31,7 +31,7 @@ func New(db *sql.DB, fs embed.FS) Migrator {
FileSystem: fs,
Root: "migrations",
},
log: monitor.NewModuleLogger("migrator"),
logger: zapadapter.NewKV(nil),
}
}

Expand All @@ -41,7 +41,7 @@ func (m Migrator) MigrateUp(max int) (int, error) {
if err != nil {
return 0, err
}
m.log.WithFields(logrus.Fields{"count": n}).Info("migrations applied")
m.logger.Info("migrations applied", "count", n)
return n, nil
}

Expand All @@ -51,7 +51,7 @@ func (m Migrator) MigrateDown(max int) (int, error) {
if err != nil {
return 0, err
}
m.log.WithFields(logrus.Fields{"count": n}).Info("migrations unapplied")
m.logger.Info("migrations unapplied", "count", n)
return n, nil
}

Expand All @@ -69,7 +69,7 @@ func (m Migrator) CreateDB(dbName string) error {
if err != nil {
return err
}
m.log.WithFields(logrus.Fields{"db": dbName}).Info("database created")
m.logger.Info("migrations applied", "db", dbName)
return nil
}

Expand All @@ -79,6 +79,6 @@ func (m Migrator) DropDB(dbName string) error {
if err != nil {
return err
}
m.log.WithFields(logrus.Fields{"db": dbName}).Info("database dropped")
m.logger.Info("database dropped", "db", dbName)
return nil
}

0 comments on commit 9cbd551

Please sign in to comment.