fix(server): chunk session in migration (#13107)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Refactor**
* Improved the process for updating session records to handle them in
smaller batches, enhancing reliability and performance during data
updates. No changes to user-facing features.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
DarkSky
2025-07-09 15:32:02 +08:00
committed by GitHub
parent a50270fc03
commit 8236ecf486

View File

@@ -1,4 +1,5 @@
import { PrismaClient } from '@prisma/client';
import { chunk } from 'lodash-es';
type SessionTime = {
sessionId: string;
@@ -17,16 +18,19 @@ export class CorrectSessionUpdateTime1751966744168 {
},
});
await Promise.all(
sessionTime
.filter((s): s is SessionTime => !!s._max.createdAt)
.map(s =>
db.aiSession.update({
where: { id: s.sessionId },
data: { updatedAt: s._max.createdAt },
})
)
);
for (const s of chunk(sessionTime, 100)) {
const sessions = s.filter((s): s is SessionTime => !!s._max.createdAt);
await db.$transaction(async tx => {
await Promise.all(
sessions.map(s =>
tx.aiSession.update({
where: { id: s.sessionId },
data: { updatedAt: s._max.createdAt },
})
)
);
});
}
}
// revert the migration