diff --git a/src/cmd/migrate/main.go b/src/cmd/migrate/main.go index f9178b3..db75878 100644 --- a/src/cmd/migrate/main.go +++ b/src/cmd/migrate/main.go @@ -42,9 +42,13 @@ func main() { log.Println("Rollback ran successfully.") } case "refresh": - if err := m.RollbackTo("00001_create_books_table"); err != nil { + if err := m.RollbackTo("00001_create_authors_table"); err != nil { log.Fatalf("could not rollback migration: %v", err) } else { + if err := m.RollbackLast(); err != nil { // Rollback the last migration, because this library doesn't support rollback all migrations + log.Fatalf("could not rollback migration: %v", err) + } + log.Println("Rollback ran successfully.") if err := m.Migrate(); err != nil { diff --git a/src/migration/migration.go b/src/migration/migration.go index 89c85a7..9fbb5bc 100644 --- a/src/migration/migration.go +++ b/src/migration/migration.go @@ -9,21 +9,21 @@ import ( func Migrations(db *gorm.DB) *gormigrate.Gormigrate { return gormigrate.New(db, gormigrate.DefaultOptions, []*gormigrate.Migration{ { - ID: "00001_create_books_table", + ID: "00001_create_authors_table", Migrate: func(tx *gorm.DB) error { - return tx.AutoMigrate(&model.Book{}) + return tx.AutoMigrate(&model.Author{}) }, Rollback: func(tx *gorm.DB) error { - return tx.Migrator().DropTable("books") + return tx.Migrator().DropTable("authors") }, }, { - ID: "00002_create_authors_table", + ID: "00002_create_books_table", Migrate: func(tx *gorm.DB) error { - return tx.AutoMigrate(&model.Author{}) + return tx.AutoMigrate(&model.Book{}) }, Rollback: func(tx *gorm.DB) error { - return tx.Migrator().DropTable("authors") + return tx.Migrator().DropTable("books") }, }, {