| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- package db
- import (
- "context"
- "f1-game/internal/component/db"
- "f1-game/internal/data"
- nameDB "f1-game/internal/name/db"
- "fmt"
- clog "github.com/cherry-game/cherry/logger"
- "go.mongodb.org/mongo-driver/v2/bson"
- "go.mongodb.org/mongo-driver/v2/mongo/options"
- )
- // ver00003 修复探险初始化关卡ID
- type ver00003 struct {
- }
- // Version 手动自增,必需唯一
- func (ver00003) Version() int32 {
- return 3
- }
- func (ver00003) Enable() bool {
- return false
- }
- func (p ver00003) Execute() bool {
- var (
- collection = dbComponent.DB().Collection(nameDB.Table_Chapter)
- ctx = context.TODO()
- pageNum int32 = 1
- pageSize int32 = 100000 // 10W
- projection = bson.M{}
- count int64 = 0
- )
- for {
- list, _, err := db.Pagination[*ChapterTable](collection, ctx, nil, projection, nil, pageNum, pageSize, false)
- if err != nil {
- clog.Errorf("[repairChapterTable] err = %v", err)
- break
- }
- if len(list) < 1 {
- break
- }
- for _, chapterTable := range list {
- var (
- chapterID = chapterTable.Chapter.ChapterID
- levelID = chapterTable.Chapter.LevelID
- )
- if chapterID <= 0 || levelID <= 0 {
- continue
- }
- _, found := data.ChapterLevel.GetByID(chapterTable.Chapter.LevelID)
- if found {
- continue
- }
- chapterRow, found := data.Chapter.GetByID(chapterID)
- if !found {
- return false
- }
- chapterTable.Chapter.LevelID = chapterRow.StartID
- tableBytes, _ := bson.Marshal(chapterTable)
- update := bson.D{{Key: "$set", Value: bson.Raw(tableBytes)}}
- filter := bson.M{
- "_id": chapterTable.PrimaryValue(), // 利用自带的_id主键进行条件过滤
- }
- _, err = dbComponent.DB().
- Collection(chapterTable.TableName()).
- UpdateOne(ctx, filter, update, options.UpdateOne().SetUpsert(true))
- if err != nil {
- clog.Fatalf("[migrator - %d] update chapter table error: %v", p.Version(), err)
- }
- }
- pageNum++
- count += int64(len(list))
- }
- clog.Infof("%s is ok.", p.Desc())
- return true
- }
- func (p ver00003) Desc() string {
- return fmt.Sprintf("[migrator - %d] repair chapter init level", p.Version())
- }
|