mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-30 04:44:28 +00:00
feat(db): preserve agent sessions on asset deletion by setting onDelete to SetNull
This commit is contained in:
@@ -1902,6 +1902,72 @@ describe('AssetService', () => {
|
|||||||
// Verify record is gone
|
// Verify record is gone
|
||||||
expect(await prisma.asset.findUnique({ where: { id: complexFile.id } })).toBeNull()
|
expect(await prisma.asset.findUnique({ where: { id: complexFile.id } })).toBeNull()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should set AgentSession.assetId to null when asset is purged, preserving session and session entries', async () => {
|
||||||
|
const { project } = await setupBasicAssets()
|
||||||
|
|
||||||
|
const agentUser = await prisma.user.create({
|
||||||
|
data: {
|
||||||
|
name: 'Test Agent',
|
||||||
|
email: `test-agent-${Date.now()}@example.com`,
|
||||||
|
type: 'agent',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
const agent = await prisma.agent.create({
|
||||||
|
data: {
|
||||||
|
id: agentUser.id,
|
||||||
|
teamId: project.teamId,
|
||||||
|
type: 'chat',
|
||||||
|
config: { provider: 'test', model: 'test' },
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const asset = await prisma.asset.create({
|
||||||
|
data: {
|
||||||
|
name: 'Asset With Session',
|
||||||
|
type: AssetType.file,
|
||||||
|
status: AssetStatus.pending_purge,
|
||||||
|
isDeleted: true,
|
||||||
|
project: { connect: { id: project.id } },
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const session = await prisma.agentSession.create({
|
||||||
|
data: {
|
||||||
|
agentId: agent.id,
|
||||||
|
cwd: '/',
|
||||||
|
assetId: asset.id,
|
||||||
|
type: 'comment',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const entry = await prisma.agentSessionEntry.create({
|
||||||
|
data: {
|
||||||
|
id: `entry-${Date.now()}`,
|
||||||
|
sessionId: session.id,
|
||||||
|
assetId: asset.id,
|
||||||
|
type: 'message',
|
||||||
|
data: { content: 'test message' },
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
await (assetService as any).purgePendingAssets()
|
||||||
|
|
||||||
|
expect(await prisma.asset.findUnique({ where: { id: asset.id } })).toBeNull()
|
||||||
|
|
||||||
|
const updatedSession = await prisma.agentSession.findUnique({
|
||||||
|
where: { id: session.id },
|
||||||
|
})
|
||||||
|
expect(updatedSession).not.toBeNull()
|
||||||
|
expect(updatedSession?.assetId).toBeNull()
|
||||||
|
|
||||||
|
const updatedEntry = await prisma.agentSessionEntry.findUnique({
|
||||||
|
where: { id: entry.id },
|
||||||
|
})
|
||||||
|
expect(updatedEntry).not.toBeNull()
|
||||||
|
expect(updatedEntry?.sessionId).toBe(session.id)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
-- DropForeignKey
|
||||||
|
ALTER TABLE "agent_sessions" DROP CONSTRAINT "agent_sessions_asset_id_fkey";
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "agent_sessions" ADD CONSTRAINT "agent_sessions_asset_id_fkey" FOREIGN KEY ("asset_id") REFERENCES "assets"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
||||||
@@ -708,7 +708,7 @@ model AgentSession {
|
|||||||
cwd String
|
cwd String
|
||||||
leafId String? @map("leaf_id")
|
leafId String? @map("leaf_id")
|
||||||
assetId String? @map("asset_id")
|
assetId String? @map("asset_id")
|
||||||
asset Asset? @relation("AssetToAgentSession", fields: [assetId], references: [id], onDelete: Cascade)
|
asset Asset? @relation("AssetToAgentSession", fields: [assetId], references: [id], onDelete: SetNull)
|
||||||
userCommentId String? @map("user_comment_id")
|
userCommentId String? @map("user_comment_id")
|
||||||
type AgentSessionType @default(comment)
|
type AgentSessionType @default(comment)
|
||||||
name String?
|
name String?
|
||||||
|
|||||||
+1
-1
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user