Skip to content

Commit

Permalink
fix migration
Browse files Browse the repository at this point in the history
  • Loading branch information
thanhhaudev committed Oct 23, 2024
1 parent 8b95f20 commit a3ed191
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/cmd/migrate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions src/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
},
},
{
Expand Down

0 comments on commit a3ed191

Please sign in to comment.