ver_00003.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package db
  2. import (
  3. "context"
  4. "f1-game/internal/component/db"
  5. "f1-game/internal/data"
  6. nameDB "f1-game/internal/name/db"
  7. "fmt"
  8. clog "github.com/cherry-game/cherry/logger"
  9. "go.mongodb.org/mongo-driver/v2/bson"
  10. "go.mongodb.org/mongo-driver/v2/mongo/options"
  11. )
  12. // ver00003 修复探险初始化关卡ID
  13. type ver00003 struct {
  14. }
  15. // Version 手动自增,必需唯一
  16. func (ver00003) Version() int32 {
  17. return 3
  18. }
  19. func (ver00003) Enable() bool {
  20. return false
  21. }
  22. func (p ver00003) Execute() bool {
  23. var (
  24. collection = dbComponent.DB().Collection(nameDB.Table_Chapter)
  25. ctx = context.TODO()
  26. pageNum int32 = 1
  27. pageSize int32 = 100000 // 10W
  28. projection = bson.M{}
  29. count int64 = 0
  30. )
  31. for {
  32. list, _, err := db.Pagination[*ChapterTable](collection, ctx, nil, projection, nil, pageNum, pageSize, false)
  33. if err != nil {
  34. clog.Errorf("[repairChapterTable] err = %v", err)
  35. break
  36. }
  37. if len(list) < 1 {
  38. break
  39. }
  40. for _, chapterTable := range list {
  41. var (
  42. chapterID = chapterTable.Chapter.ChapterID
  43. levelID = chapterTable.Chapter.LevelID
  44. )
  45. if chapterID <= 0 || levelID <= 0 {
  46. continue
  47. }
  48. _, found := data.ChapterLevel.GetByID(chapterTable.Chapter.LevelID)
  49. if found {
  50. continue
  51. }
  52. chapterRow, found := data.Chapter.GetByID(chapterID)
  53. if !found {
  54. return false
  55. }
  56. chapterTable.Chapter.LevelID = chapterRow.StartID
  57. tableBytes, _ := bson.Marshal(chapterTable)
  58. update := bson.D{{Key: "$set", Value: bson.Raw(tableBytes)}}
  59. filter := bson.M{
  60. "_id": chapterTable.PrimaryValue(), // 利用自带的_id主键进行条件过滤
  61. }
  62. _, err = dbComponent.DB().
  63. Collection(chapterTable.TableName()).
  64. UpdateOne(ctx, filter, update, options.UpdateOne().SetUpsert(true))
  65. if err != nil {
  66. clog.Fatalf("[migrator - %d] update chapter table error: %v", p.Version(), err)
  67. }
  68. }
  69. pageNum++
  70. count += int64(len(list))
  71. }
  72. clog.Infof("%s is ok.", p.Desc())
  73. return true
  74. }
  75. func (p ver00003) Desc() string {
  76. return fmt.Sprintf("[migrator - %d] repair chapter init level", p.Version())
  77. }