mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-17 14:27:02 +08:00
fix(server): session updated at (#13099)
fix AI-325 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Chores** * Improved database handling for session update times to ensure more accurate tracking of session activity. * Enhanced migration process to better manage and update session metadata. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE "ai_sessions_metadata" ADD COLUMN "updated_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP;
|
||||
ALTER TABLE ai_sessions_metadata ADD COLUMN updated_at TIMESTAMPTZ(3);
|
||||
|
||||
UPDATE ai_sessions_metadata SET updated_at = created_at;
|
||||
|
||||
ALTER TABLE ai_sessions_metadata ALTER COLUMN updated_at SET NOT NULL, ALTER COLUMN updated_at SET DEFAULT CURRENT_TIMESTAMP;
|
||||
|
||||
-- DropIndex
|
||||
DROP INDEX IF EXISTS "ai_session_unique_doc_session_idx";
|
||||
@@ -0,0 +1,34 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
type SessionTime = {
|
||||
sessionId: string;
|
||||
_max: {
|
||||
createdAt: Date;
|
||||
};
|
||||
};
|
||||
|
||||
export class CorrectSessionUpdateTime1751966744168 {
|
||||
// do the migration
|
||||
static async up(db: PrismaClient) {
|
||||
const sessionTime = await db.aiSessionMessage.groupBy({
|
||||
by: ['sessionId'],
|
||||
_max: {
|
||||
createdAt: true,
|
||||
},
|
||||
});
|
||||
|
||||
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 },
|
||||
})
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// revert the migration
|
||||
static async down(_db: PrismaClient) {}
|
||||
}
|
||||
@@ -6,3 +6,4 @@ export * from './1732861452428-migrate-invite-status';
|
||||
export * from './1733125339942-universal-subscription';
|
||||
export * from './1738590347632-feature-redundant';
|
||||
export * from './1745211351719-create-indexer-tables';
|
||||
export * from './1751966744168-correct-session-update-time';
|
||||
|
||||
Reference in New Issue
Block a user