Skip to content
This repository has been archived by the owner on Jun 28, 2018. It is now read-only.

Commit

Permalink
allow to force NilVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
mattes committed Feb 20, 2017
1 parent 55f5425 commit eb02bc7
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func dropCmd(m *migrate.Migrate) {
}
}

func forceCmd(m *migrate.Migrate, v uint) {
func forceCmd(m *migrate.Migrate, v int) {
if err := m.Force(v); err != nil {
log.fatalErr(err)
}
Expand Down
8 changes: 6 additions & 2 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,16 @@ Commands:
log.fatal("error: please specify version argument V")
}

v, err := strconv.ParseUint(flag.Arg(1), 10, 64)
v, err := strconv.ParseInt(flag.Arg(1), 10, 64)
if err != nil {
log.fatal("error: can't read version argument V")
}

forceCmd(migrater, uint(v))
if v < -1 {
log.fatal("error: argument V must be >= -1")
}

forceCmd(migrater, int(v))

if log.verbose {
log.Println("Finished after", time.Now().Sub(startTime))
Expand Down
1 change: 1 addition & 0 deletions database/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type Driver interface {

// SetVersion saves version and dirty state.
// Migrate will call this function before and after each call to Run.
// version must be >= -1. -1 means NilVersion.
SetVersion(version int, dirty bool) error

// Version returns the currently active version and if the database is dirty.
Expand Down
8 changes: 6 additions & 2 deletions migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,16 @@ func (m *Migrate) Run(migration ...*Migration) error {
// Force sets a migration version.
// It does not check any currently active version in database.
// It does not check if the database is dirty.
func (m *Migrate) Force(version uint) error {
func (m *Migrate) Force(version int) error {
if version < -1 {
panic("version must be >= -1")
}

if err := m.lock(); err != nil {
return err
}

if err := m.databaseDrv.SetVersion(int(version), false); err != nil {
if err := m.databaseDrv.SetVersion(version, false); err != nil {
return m.unlockErr(err)
}

Expand Down

0 comments on commit eb02bc7

Please sign in to comment.