@@ -664,39 +664,56 @@ async function migrateGalgames() {
664664async function migrateMessages ( ) {
665665 console . log ( '\n🚀 Starting Message migration...' )
666666
667- // 1. 预加载所有有效的用户ID到一个Set中,用于快速查找
668- console . log ( ' Fetching all valid user IDs from target database...' )
669- const users = await prisma . user . findMany ( {
670- select : {
671- id : true // 只需要id字段
672- }
673- } )
667+ // 1. 预加载所有有效的用户ID、Topic ID 和 Galgame ID 到 Set 中,用于快速查找
668+ console . log ( ' Fetching all valid IDs from target database...' )
669+
670+ // 获取用户ID
671+ const users = await prisma . user . findMany ( { select : { id : true } } )
674672 const validUserIds = new Set ( users . map ( ( u ) => u . id ) )
675673 console . log ( ` ... Found ${ validUserIds . size } valid users.` )
676674
675+ // 获取 Topic ID
676+ const topics = await prisma . topic . findMany ( { select : { id : true } } )
677+ const validTopicIds = new Set ( topics . map ( ( t ) => t . id ) )
678+ console . log ( ` ... Found ${ validTopicIds . size } valid topics.` )
679+
680+ // 获取 Galgame ID
681+ const galgames = await prisma . galgame . findMany ( { select : { id : true } } )
682+ const validGalgameIds = new Set ( galgames . map ( ( g ) => g . id ) )
683+ console . log ( ` ... Found ${ validGalgameIds . size } valid galgames.` )
684+
677685 const total = await MessageModel . countDocuments ( )
678686 const cursor = MessageModel . find ( ) . lean ( ) . cursor ( )
679687
680688 let migratedCount = 0
681- let skippedCount = 0 // 新增一个计数器,用于记录跳过的消息
689+ let skippedCount = 0
682690 let batch : Prisma . messageCreateManyArgs [ 'data' ] = [ ]
683691
684692 for await ( const doc of cursor ) {
685- // 2. 在内存中校验 sender_id 和 receiver_id 是否有效
693+ // 2. 校验 sender_id 和 receiver_id 是否有效
686694 if (
695+ ! doc . sender_uid ||
696+ ! doc . receiver_uid ||
687697 ! validUserIds . has ( doc . sender_uid ) ||
688698 ! validUserIds . has ( doc . receiver_uid )
689699 ) {
690700 skippedCount ++
691- continue // 跳过这条消息
701+ continue // 如果发送者或接收者ID为空或无效,则跳过
692702 }
693703
694- // 如果 sender_uid 或 receiver_uid 为空也跳过 (可选的额外健壮性检查)
695- if ( ! doc . sender_uid || ! doc . receiver_uid ) {
704+ // 3. 校验 tid 和 gid 是否有效
705+ // 只有当 tid 或 gid 存在时,才进行校验。如果它们都为 null/undefined,则消息本身与 topic/galgame 无关,应该被迁移。
706+ // 如果 tid 存在,但它不在有效的 Topic ID 集合中,则跳过。
707+ if ( doc . tid && ! validTopicIds . has ( doc . tid ) ) {
696708 skippedCount ++
697709 continue
698710 }
699-
711+ // 如果 gid 存在,但它不在有效的 Galgame ID 集合中,则跳过。
712+ if ( doc . gid && ! validGalgameIds . has ( doc . gid ) ) {
713+ skippedCount ++
714+ continue
715+ }
716+ // 只有通过所有校验的消息才会进入这里
700717 const link = doc . gid ? `/galgame/${ doc . gid } ` : `/topic/${ doc . tid } `
701718
702719 batch . push ( {
@@ -731,7 +748,6 @@ async function migrateMessages() {
731748 `✅ Message migration complete. Total migrated: ${ migratedCount } . Total skipped: ${ skippedCount } .`
732749 )
733750}
734-
735751async function migrateSystemMessages ( ) {
736752 console . log ( '\n🚀 Starting System Message (MessageAdmin) migration...' )
737753 const total = await MessageAdminModel . countDocuments ( )
0 commit comments