fix: migration related issue for oid4vc schema#1609
Conversation
Signed-off-by: pranalidhanavade <pranali.dhanavade@ayanworks.com>
📝 WalkthroughWalkthroughThree models in the Prisma schema ( ChangesOrganisation Relation Referential Integrity
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Review rate limit: 0/1 reviews remaining, refill in 60 minutes.Comment |
|
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@libs/prisma-service/prisma/schema.prisma`:
- Line 603: The DB schema uses onDelete: SetNull for the organisation FK but
deleteOrganisation in apps/organization/repositories/organization.repository.ts
currently throws ConflictException when oidc_issuer, oid4vc_credentials, or
issued_oid4vc_credentials reference the org, creating a behavioral mismatch;
update deleteOrganisation to mirror how schema and credential_definition are
handled by calling updateMany on the three models (oidc_issuer,
oid4vc_credentials, issued_oid4vc_credentials) to set orgId: null for rows with
the target org id before invoking prisma.organisation.delete, so the
application-level logic aligns with the declared onDelete: SetNull behavior.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: cc21d6d2-9ae7-4d7d-9655-f0bcd8d2df37
📒 Files selected for processing (1)
libs/prisma-service/prisma/schema.prisma
| isPrimary Boolean @default(false) | ||
|
|
||
| organisation organisation? @relation(fields: [orgId], references: [id]) | ||
| organisation organisation? @relation(fields: [orgId], references: [id], onDelete: SetNull) |
There was a problem hiding this comment.
Application-level deletion guard is now inconsistent with the declared onDelete: SetNull behavior
Prisma's implicit default onDelete for optional relations is already SetNull, so making this explicit is the correct fix for the migration failure. However, a behavioral inconsistency has been introduced across the codebase.
deleteOrganisation in apps/organization/repositories/organization.repository.ts (lines 860–876):
- Pre-checks record counts in
oidc_issuer,oid4vc_credentials, andissued_oid4vc_credentials - Throws
ConflictExceptionif any rows reference the org — preventing deletion entirely for these three tables
Compare this with how schema and credential_definition are handled: both declare onDelete: SetNull and the same deleteOrganisation method explicitly calls updateMany({ data: { orgId: null } }) before the delete, keeping the DB-level action and application logic aligned.
For the three models in this PR, the contract is contradictory:
- DB level: deletion of
organisationrows setsorgIdtonullvia the FK cascade - App level: deletion is blocked with a 409 Conflict if any such rows exist
In practice this means onDelete: SetNull never fires for app-initiated deletes (the guard prevents reaching prisma.organisation.delete), but will fire for any direct DB-level deletion (migrations with cascades, admin tooling, raw SQL). If that happens, getCredentialAllocations in apps/oid4vc-issuance/src/status-list-allocator.service.ts queries { where: { orgId, issuanceSessionId } } and would silently return no results for any credential whose orgId was nulled out.
The fix should align the two layers — either:
- Preferred (consistent with
schema/credential_definition): UpdatedeleteOrganisationto null outorgIdin these three tables instead of throwing, relying on the DB-levelSetNullcascade. - Alternative: Keep the ConflictException and change the referential action to
onDelete: Restrict(verify that this doesn't re-introduce the migration failure).
Also applies to: 623-623, 670-670
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@libs/prisma-service/prisma/schema.prisma` at line 603, The DB schema uses
onDelete: SetNull for the organisation FK but deleteOrganisation in
apps/organization/repositories/organization.repository.ts currently throws
ConflictException when oidc_issuer, oid4vc_credentials, or
issued_oid4vc_credentials reference the org, creating a behavioral mismatch;
update deleteOrganisation to mirror how schema and credential_definition are
handled by calling updateMany on the three models (oidc_issuer,
oid4vc_credentials, issued_oid4vc_credentials) to set orgId: null for rows with
the target org id before invoking prisma.organisation.delete, so the
application-level logic aligns with the declared onDelete: SetNull behavior.



What
Summary by CodeRabbit