Skip to content

Commit

Permalink
Add --number flag to dbmate down
Browse files Browse the repository at this point in the history
  • Loading branch information
mg98 committed Sep 5, 2022
1 parent 16b8492 commit 34a3545
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
9 changes: 8 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,17 @@ func NewApp() *cli.App {
EnvVars: []string{"DBMATE_VERBOSE"},
Usage: "print the result of each statement execution",
},
&cli.IntFlag{
Name: "number",
Aliases: []string{"n"},
Usage: "number of migrations to rollback (-1 means all)",
Value: 1,
},
},
Action: action(func(db *dbmate.DB, c *cli.Context) error {
db.Verbose = c.Bool("verbose")
return db.Rollback()
number := c.Int("number")
return db.Rollback(number)
}),
},
{
Expand Down
67 changes: 32 additions & 35 deletions pkg/dbmate/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ func migrationVersion(filename string) string {
}

// Rollback rolls back the most recent migration
func (db *DB) Rollback() error {
func (db *DB) Rollback(number int) error {
drv, err := db.GetDriver()
if err != nil {
return err
Expand All @@ -483,55 +483,52 @@ func (db *DB) Rollback() error {
}
defer dbutil.MustClose(sqlDB)

applied, err := drv.SelectMigrations(sqlDB, 1)
applied, err := drv.SelectMigrations(sqlDB, number)
if err != nil {
return err
}

// grab most recent applied migration (applied has len=1)
latest := ""
for ver := range applied {
latest = ver
}
if latest == "" {
if len(applied) == 0 {
return ErrNoRollback
}

filename, err := findMigrationFile(db.MigrationsDir, latest)
if err != nil {
return err
}

fmt.Fprintf(db.Log, "Rolling back: %s\n", filename)
for ver := range applied {
filename, err := findMigrationFile(db.MigrationsDir, ver)
if err != nil {
return err
}

_, down, err := parseMigration(filepath.Join(db.MigrationsDir, filename))
if err != nil {
return err
}
fmt.Fprintf(db.Log, "Rolling back: %s\n", filename)

execMigration := func(tx dbutil.Transaction) error {
// rollback migration
result, err := tx.Exec(down.Contents)
_, down, err := parseMigration(filepath.Join(db.MigrationsDir, filename))
if err != nil {
return err
} else if db.Verbose {
db.printVerbose(result)
}

// remove migration record
return drv.DeleteMigration(tx, latest)
}
execMigration := func(tx dbutil.Transaction) error {
// rollback migration
result, err := tx.Exec(down.Contents)
if err != nil {
return err
} else if db.Verbose {
db.printVerbose(result)
}

if down.Options.Transaction() {
// begin transaction
err = doTransaction(sqlDB, execMigration)
} else {
// run outside of transaction
err = execMigration(sqlDB)
}
// remove migration record
return drv.DeleteMigration(tx, ver)
}

if err != nil {
return err
if down.Options.Transaction() {
// begin transaction
err = doTransaction(sqlDB, execMigration)
} else {
// run outside of transaction
err = execMigration(sqlDB)
}

if err != nil {
return err
}
}

// automatically update schema file, silence errors
Expand Down

0 comments on commit 34a3545

Please sign in to comment.