diff --git a/prisma/migrations/20260523154249_remove_asset_cascade/migration.sql b/prisma/migrations/20260523154249_remove_asset_cascade/migration.sql new file mode 100644 index 00000000..f8281357 --- /dev/null +++ b/prisma/migrations/20260523154249_remove_asset_cascade/migration.sql @@ -0,0 +1,17 @@ +-- DropForeignKey +ALTER TABLE "assets" DROP CONSTRAINT "assets_parent_id_fkey"; + +-- DropForeignKey +ALTER TABLE "assets" DROP CONSTRAINT "assets_project_id_fkey"; + +-- DropForeignKey +ALTER TABLE "assets" DROP CONSTRAINT "assets_target_id_fkey"; + +-- AddForeignKey +ALTER TABLE "assets" ADD CONSTRAINT "assets_parent_id_fkey" FOREIGN KEY ("parent_id") REFERENCES "assets"("id") ON DELETE SET NULL ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "assets" ADD CONSTRAINT "assets_target_id_fkey" FOREIGN KEY ("target_id") REFERENCES "assets"("id") ON DELETE SET NULL ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "assets" ADD CONSTRAINT "assets_project_id_fkey" FOREIGN KEY ("project_id") REFERENCES "projects"("id") ON DELETE SET NULL ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 1481ff68..7ebd7769 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -363,11 +363,11 @@ model Asset { updatedAt DateTime @updatedAt @map("updated_at") parentId String? @map("parent_id") - parent Asset? @relation("AssetChildren", fields: [parentId], references: [id], onDelete: Cascade) + parent Asset? @relation("AssetChildren", fields: [parentId], references: [id]) children Asset[] @relation("AssetChildren") targetId String? @map("target_id") - target Asset? @relation("SymlinkTarget", fields: [targetId], references: [id], onDelete: Cascade) + target Asset? @relation("SymlinkTarget", fields: [targetId], references: [id]) symlinks Asset[] @relation("SymlinkTarget") metadataValues AssetMetadataValue[] @relation("AssetToMetadataValues") @@ -379,7 +379,7 @@ model Asset { task Task? @relation("TaskToAsset", fields: [taskId], references: [id]) projectId String? @map("project_id") - project Project? @relation("ProjectToAsset", fields: [projectId], references: [id], onDelete: Cascade) + project Project? @relation("ProjectToAsset", fields: [projectId], references: [id]) comments AssetComment[] @relation("AssetToComment") commentAttachments AssetCommentAttachment[] @relation("AssetToCommentAttachment") diff --git a/src/services/asset/asset.test.ts b/src/services/asset/asset.test.ts index 33df8be2..892ce534 100644 --- a/src/services/asset/asset.test.ts +++ b/src/services/asset/asset.test.ts @@ -9,6 +9,7 @@ vi.mock('@/services/s3/s3', () => ({ s3Service: { presign: vi.fn().mockResolvedValue('http://mock-s3-url'), putObject: vi.fn().mockResolvedValue(undefined), + deleteObject: vi.fn().mockResolvedValue(undefined), }, })) @@ -1173,4 +1174,59 @@ describe('AssetService', () => { expect(versions[1].name).toBe('version1.txt') }) }) + + describe('Asset Cascade Bug (Reproduction)', () => { + it('should NOT cascade delete children from DB before their S3 files are deleted', async () => { + const { project } = await setupBasicAssets() + const { s3Service } = await import('@/services/s3/s3') + + // 1. Create Folder A + const folderA = await prisma.asset.create({ + data: { + name: 'Folder A', + type: AssetType.folder, + status: 'pending_purge', + isDeleted: true, + projectId: project.id, + }, + }) + + // 2. Create 101 children files for Folder A + // Each has an S3 key. + const childCount = 101 + const childData = Array.from({ length: childCount }).map((_, i) => ({ + name: `File ${i}`, + type: AssetType.file, + status: 'pending_purge', + isDeleted: true, + projectId: project.id, + parentId: folderA.id, + key: `key-${i}`, + })) + + await prisma.asset.createMany({ data: childData }) + + // 3. Trigger the purge job for the FIRST batch (100 items) + // eslint-disable-next-line @typescript-eslint/no-explicit-any + await (assetService as any).purgePendingAssets() + + // 4. Verify some items were processed in S3 + // We don't know the order, but if Folder A was in the first 100, + // then its synchronous cascade will have wiped the remaining children. + + // 5. Trigger the purge job for the SECOND batch + // eslint-disable-next-line @typescript-eslint/no-explicit-any + await (assetService as any).purgePendingAssets() + + // 6. Verify S3 deletion was called for EVERY child + for (let i = 0; i < childCount; i++) { + expect(s3Service.deleteObject).toHaveBeenCalledWith(expect.any(String), `key-${i}`) + } + + // Verify folder and all children are gone from DB + expect(await prisma.asset.findUnique({ where: { id: folderA.id } })).toBeNull() + const remainingChildren = await prisma.asset.count({ where: { parentId: folderA.id } }) + expect(remainingChildren).toBe(0) + }) + }) })