1653638869132_migrate.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package version
  2. import (
  3. "go-admin/cmd/migrate/migration/models"
  4. common "go-admin/common/models"
  5. "gorm.io/gorm"
  6. "runtime"
  7. "strconv"
  8. "go-admin/cmd/migrate/migration"
  9. )
  10. func init() {
  11. _, fileName, _, _ := runtime.Caller(0)
  12. migration.Migrate.SetVersion(migration.GetFilename(fileName), _1653638869132Test)
  13. }
  14. func _1653638869132Test(db *gorm.DB, version string) error {
  15. return db.Transaction(func(tx *gorm.DB) error {
  16. var list []models.SysMenu
  17. err := tx.Model(&models.SysMenu{}).Order("parent_id,menu_id").Find(&list).Error
  18. if err != nil {
  19. return err
  20. }
  21. for _, v := range list {
  22. if v.ParentId == 0 {
  23. v.Paths = "/0/" + strconv.Itoa(v.MenuId)
  24. } else {
  25. var e models.SysMenu
  26. err = tx.Model(&models.SysMenu{}).Where("menu_id=?", v.ParentId).First(&e).Error
  27. if err != nil {
  28. if err == gorm.ErrRecordNotFound {
  29. continue
  30. }
  31. return err
  32. }
  33. v.Paths = e.Paths + "/" + strconv.Itoa(v.MenuId)
  34. }
  35. err = tx.Model(&v).Update("paths", v.Paths).Error
  36. if err != nil {
  37. return err
  38. }
  39. }
  40. return tx.Create(&common.Migration{
  41. Version: version,
  42. }).Error
  43. })
  44. }