Skip to content

Commit

Permalink
feat: enable Provider verbose logging for statements (#851)
Browse files Browse the repository at this point in the history
  • Loading branch information
vfoucault authored Nov 20, 2024
1 parent 512a496 commit f3d1dc4
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions provider_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (p *Provider) runIndividually(
}
if useTx {
return beginTx(ctx, conn, func(tx *sql.Tx) error {
if err := runMigration(ctx, tx, m, direction); err != nil {
if err := p.runMigration(ctx, tx, m, direction); err != nil {
return err
}
return p.maybeInsertOrDelete(ctx, tx, m.Version, direction)
Expand All @@ -189,12 +189,12 @@ func (p *Provider) runIndividually(
//
// For now, we guard against this scenario by checking the max open connections and
// returning an error in the prepareMigration function.
if err := runMigration(ctx, p.db, m, direction); err != nil {
if err := p.runMigration(ctx, p.db, m, direction); err != nil {
return err
}
return p.maybeInsertOrDelete(ctx, p.db, m.Version, direction)
case TypeSQL:
if err := runMigration(ctx, conn, m, direction); err != nil {
if err := p.runMigration(ctx, conn, m, direction); err != nil {
return err
}
return p.maybeInsertOrDelete(ctx, conn, m.Version, direction)
Expand Down Expand Up @@ -382,19 +382,19 @@ func isEmpty(m *Migration, direction bool) bool {

// runMigration is a helper function that runs the migration in the given direction. It must only be
// called after the migration has been parsed and initialized.
func runMigration(ctx context.Context, db database.DBTxConn, m *Migration, direction bool) error {
func (p *Provider) runMigration(ctx context.Context, db database.DBTxConn, m *Migration, direction bool) error {
switch m.Type {
case TypeGo:
return runGo(ctx, db, m, direction)
return p.runGo(ctx, db, m, direction)
case TypeSQL:
return runSQL(ctx, db, m, direction)
return p.runSQL(ctx, db, m, direction)
}
return fmt.Errorf("invalid migration type: %q", m.Type)
}

// runGo is a helper function that runs the given Go functions in the given direction. It must only
// be called after the migration has been initialized.
func runGo(ctx context.Context, db database.DBTxConn, m *Migration, direction bool) (retErr error) {
func (p *Provider) runGo(ctx context.Context, db database.DBTxConn, m *Migration, direction bool) (retErr error) {
defer func() {
if r := recover(); r != nil {
retErr = fmt.Errorf("panic: %v\n%s", r, debug.Stack())
Expand Down Expand Up @@ -426,7 +426,8 @@ func runGo(ctx context.Context, db database.DBTxConn, m *Migration, direction bo

// runSQL is a helper function that runs the given SQL statements in the given direction. It must
// only be called after the migration has been parsed.
func runSQL(ctx context.Context, db database.DBTxConn, m *Migration, direction bool) error {
func (p *Provider) runSQL(ctx context.Context, db database.DBTxConn, m *Migration, direction bool) error {

if !m.sql.Parsed {
return fmt.Errorf("sql migrations must be parsed")
}
Expand All @@ -437,6 +438,9 @@ func runSQL(ctx context.Context, db database.DBTxConn, m *Migration, direction b
statements = m.sql.Down
}
for _, stmt := range statements {
if p.cfg.verbose {
p.cfg.logger.Printf("Excuting statement: %s", stmt)
}
if _, err := db.ExecContext(ctx, stmt); err != nil {
return err
}
Expand Down

0 comments on commit f3d1dc4

Please sign in to comment.