fix(application): nullify file FK references before deleting application files on uninstall

https://sonarly.com/issue/25146?type=bug

Uninstalling an application fails with a foreign key constraint violation because `deleteApplicationFiles` attempts to delete file records that are still referenced by the application's `packageJsonFileId`/`yarnLockFileId` columns with `onDelete: RESTRICT`.

Fix: Added an `applicationRepository.update()` call to nullify `packageJsonFileId` and `yarnLockFileId` on the application row BEFORE calling `deleteApplicationFiles()`.

**Why this fixes the bug:** The `application` table has two `OneToOne` FK columns (`packageJsonFileId`, `yarnLockFileId`) pointing to `file.id` with `onDelete: RESTRICT`. When `deleteApplicationFiles` tries to delete file rows matching `applicationId`, PostgreSQL blocks it because the application row still references those files via these FK columns. By setting them to `null` first, the FK constraint is satisfied and the file deletion succeeds.

**Operation order after fix:**
1. `UPDATE application SET packageJsonFileId = NULL, yarnLockFileId = NULL` — breaks the FK reference
2. `DELETE FROM file WHERE applicationId = X` — now succeeds (no RESTRICT violation)
3. `DELETE FROM application WHERE universalIdentifier = X` — succeeds (files already deleted, no RESTRICT from file.applicationId)
This commit is contained in:
Sonarly Claude Code 2026-04-14 09:49:18 +00:00
parent eb13378760
commit feb047b910

View file

@ -452,6 +452,13 @@ export class ApplicationService {
);
}
// Nullify file FK references before deleting files to avoid
// RESTRICT constraint violation (application references file via packageJsonFileId/yarnLockFileId)
await this.applicationRepository.update(
{ universalIdentifier, workspaceId },
{ packageJsonFileId: null, yarnLockFileId: null },
);
await this.fileStorageService.deleteApplicationFiles({
workspaceId,
applicationUniversalIdentifier: universalIdentifier,