Skip to content

Commit

Permalink
Merge pull request #21 from tonytanger/master
Browse files Browse the repository at this point in the history
Validating the migration ID exist in the list of migrations
  • Loading branch information
andreynering authored Dec 2, 2018
2 parents 4360c65 + 88cddeb commit 0ea84f7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
26 changes: 25 additions & 1 deletion gormigrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ var (
// ErrNoRunnedMigration is returned when any runned migration was found while
// running RollbackLast
ErrNoRunnedMigration = errors.New("gormigrate: Could not find last runned migration")

// ErrMigrationIDDoesNotExist is returned when migrating or rolling back to a migration ID that
// does not exist in the list of migrations
ErrMigrationIDDoesNotExist = errors.New("gormigrate: Tried to migrate to an ID that doesn't exist")
)

// New returns a new Gormigrate.
Expand Down Expand Up @@ -109,7 +113,10 @@ func (g *Gormigrate) InitSchema(initSchema InitSchemaFunc) {

// Migrate executes all migrations that did not run yet.
func (g *Gormigrate) Migrate() error {
return g.migrate("")
if len(g.migrations) == 0 {
return ErrNoMigrationDefined
}
return g.migrate(g.migrations[len(g.migrations)-1].ID)
}

// MigrateTo executes all migrations that did not run yet up to the migration that matches `migrationID`.
Expand All @@ -126,6 +133,10 @@ func (g *Gormigrate) migrate(migrationID string) error {
return err
}

if err := g.checkIDExist(migrationID); err != nil {
return err
}

if err := g.createMigrationTableIfNotExists(); err != nil {
return err
}
Expand Down Expand Up @@ -164,6 +175,15 @@ func (g *Gormigrate) checkDuplicatedID() error {
return nil
}

func (g *Gormigrate) checkIDExist(migrationID string) error {
for _, migrate := range g.migrations {
if migrate.ID == migrationID {
return nil
}
}
return ErrMigrationIDDoesNotExist
}

// RollbackLast undo the last migration
func (g *Gormigrate) RollbackLast() error {
if len(g.migrations) == 0 {
Expand All @@ -188,6 +208,10 @@ func (g *Gormigrate) RollbackTo(migrationID string) error {
return ErrNoMigrationDefined
}

if err := g.checkIDExist(migrationID); err != nil {
return err
}

g.begin()

for i := len(g.migrations) - 1; i >= 0; i-- {
Expand Down
10 changes: 10 additions & 0 deletions gormigrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ func TestInitSchema(t *testing.T) {
})
}

func TestMigrationIDDoesNotExist(t *testing.T) {
forEachDatabase(t, func(db *gorm.DB) {
m := New(db, DefaultOptions, migrations)
assert.Equal(t, ErrMigrationIDDoesNotExist, m.MigrateTo("1234"))
assert.Equal(t, ErrMigrationIDDoesNotExist, m.RollbackTo("1234"))
assert.Equal(t, ErrMigrationIDDoesNotExist, m.MigrateTo(""))
assert.Equal(t, ErrMigrationIDDoesNotExist, m.RollbackTo(""))
})
}

func TestMissingID(t *testing.T) {
forEachDatabase(t, func(db *gorm.DB) {
migrationsMissingID := []*Migration{
Expand Down

0 comments on commit 0ea84f7

Please sign in to comment.