Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions apps/api/src/trigger/policies/update-policy-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,12 @@ export async function updatePolicyInDatabase(
}

await db.$transaction(async (tx) => {
// Clear version references first to avoid FK constraint issues during deletion
if (policy.versions.length > 0) {
await tx.policy.update({
where: { id: policyId },
data: { currentVersionId: null, pendingVersionId: null },
});
await tx.policyVersion.deleteMany({ where: { policyId } });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -561,8 +561,12 @@ export async function updatePolicyInDatabase(

// Use transaction to ensure atomicity - if any step fails, all are rolled back
await db.$transaction(async (tx) => {
// Delete all existing versions
// Clear version references first to avoid FK constraint issues during deletion
if (policy.versions.length > 0) {
await tx.policy.update({
where: { id: policyId },
data: { currentVersionId: null, pendingVersionId: null },
});
await tx.policyVersion.deleteMany({
where: { policyId },
});
Expand Down
Loading