Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into main
Browse files Browse the repository at this point in the history
# Conflicts:
#	README.md
  • Loading branch information
lawzava committed Jun 26, 2021
2 parents 7b5a69e + 9f61522 commit f167002
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
28 changes: 19 additions & 9 deletions migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (

var errNoMigrationVersion = errors.New("migration version not found")

// InfoLogger defines info level logger, passes go-sprintf-friendly format & arguments.
type InfoLogger func(format string, args ...interface{})

// Options define applied migrations options and behavior.
type Options struct {
// VersionNumberToApply defines target version for migration actions.
Expand All @@ -24,6 +27,9 @@ type Options struct {

// RefreshSchema drops and recreates public schema.
RefreshSchema bool

// LogInfo handles info logging
LogInfo InfoLogger
}

type migrationTask struct {
Expand Down Expand Up @@ -53,6 +59,12 @@ func New(db *pg.DB, opt Options, migrations ...*Migration) (Migrate, error) {
return nil, err
}

if opt.LogInfo == nil {
opt.LogInfo = func(format string, args ...interface{}) {
log.Info().Msgf(format, args...)
}
}

return migrate{
task: &migrationTask{
migrations: mapMigrations(migrations),
Expand Down Expand Up @@ -85,7 +97,7 @@ func (m migrationTask) migrate() error {
}

if m.opt.PrintInfoAndExit {
log.Info().Msgf("currently applied version: %d", lastAppliedMigrationNumber)
m.opt.LogInfo("currently applied version: %d", lastAppliedMigrationNumber)

return nil
}
Expand Down Expand Up @@ -118,16 +130,14 @@ func (m migrationTask) handleForceVersionWithoutMigrations() error {
}

func (m migrationTask) refreshDatabase() error {
log.Info().Msg("refreshing database")
m.opt.LogInfo("refreshing database")

err := m.repo.DropDatabase()
if err != nil {
log.Error().Err(err).Msg("failed to DropDatabase (running with 'refresh' flag)")

return fmt.Errorf("dropping database: %w", err)
return fmt.Errorf("failed to DropDatabase (running with 'refresh' flag): %w", err)
}

log.Info().Msg("ensuring migrations table is present")
m.opt.LogInfo("ensuring migrations table is present")

err = m.repo.EnsureMigrationTable()
if err != nil {
Expand All @@ -139,7 +149,7 @@ func (m migrationTask) refreshDatabase() error {

func (m *migrationTask) applyMigrations(lastAppliedMigrationNumber uint) error {
if len(m.migrations) == 0 {
log.Info().Msg("no migrations to apply.")
m.opt.LogInfo("no migrations to apply.")

return nil
}
Expand Down Expand Up @@ -167,7 +177,7 @@ func (m *migrationTask) applyBackwardMigrations(lastAppliedMigrationNumber uint)
break
}

log.Info().Msgf("applying backwards migration %d (%s)", migration.Number, migration.Name)
m.opt.LogInfo("applying backwards migration %d (%s)", migration.Number, migration.Name)

if err := m.repo.ApplyMigration(migration.Backwards); err != nil {
return fmt.Errorf("failed to apply the migration (BackwardMigration): %w", err)
Expand All @@ -193,7 +203,7 @@ func (m *migrationTask) applyForwardMigrations(lastAppliedMigrationNumber uint)
break
}

log.Info().Msgf("applying forward migration %d (%s)", migration.Number, migration.Name)
m.opt.LogInfo("applying forward migration %d (%s)", migration.Number, migration.Name)

if err := m.repo.ApplyMigration(migration.Forwards); err != nil {
return fmt.Errorf("failed to apply the migration (ForwardMigration): %w", err)
Expand Down
5 changes: 5 additions & 0 deletions migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ func performMigrateTaskWithMigrations(t *testing.T, repo repository, options Opt
func performMigrateTask(t *testing.T, repo repository, options Options, migrations []*Migration) error {
t.Helper()

options.LogInfo = func(format string, args ...interface{}) {
// nolint:forbidigo // allow in tests
fmt.Printf(format+"\n", args...)
}

task := migrationTask{
migrations: mapMigrations(migrations),
repo: repo,
Expand Down

0 comments on commit f167002

Please sign in to comment.