fix: resolve migration issue for oidv_issuer schema modal#1610
Conversation
Signed-off-by: pranalidhanavade <pranali.dhanavade@ayanworks.com>
📝 WalkthroughWalkthroughThis PR reverts a previous migration and introduces a replacement migration that adds ChangesDatabase Constraint Tightening
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 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: 2
🤖 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/migrations/20260504110536_add_orgid_and_isprimary_in_oidc_issuer/migration.sql`:
- Around line 2-6: The migration currently adds oidc_issuer.orgId and
immediately creates a FK, but it never backfills orgId from the existing
orgAgentId mapping so pre-existing issuers stay NULL; change the migration to
(1) add "orgId" as nullable on oidc_issuer (keep "isPrimary" as is), (2) run an
UPDATE that sets oidc_issuer.orgId = org_agents.orgId by joining
oidc_issuer.orgAgentId = org_agents.id (use the table names oidc_issuer and
org_agents and the columns orgAgentId and orgId), (3) if schema requires
non-null, ALTER COLUMN to NOT NULL afterwards, and only then add the FOREIGN KEY
constraint "oidc_issuer_orgId_fkey" referencing organisation(id) with the same
ON DELETE/ON UPDATE behavior.
- Around line 1-6: Restore the deleted April migration 20260427080006 exactly as
it was in commit f95dcd3 so history remains immutable: re-add the original SQL
that modified both oidc_issuer and the oid4vc_credentials /
issued_oid4vc_credentials foreign keys (the exact ALTER TABLE and ADD CONSTRAINT
statements previously present) and do not modify its checksum; then keep the
current May migration (20260504110536) only as an additive follow-up that
includes exclusively the new changes (e.g., adding isPrimary and orgId on
oidc_issuer and its FK), ensuring the April migration file contents and names
match f95dcd3 precisely and the May migration only appends new ALTERs.
🪄 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: 70300b48-0df0-4027-a7c3-434980bb7184
📒 Files selected for processing (3)
libs/prisma-service/prisma/migrations/20260427080006_added_org_id_and_is_primary_in_oidc_issuer/migration.sqllibs/prisma-service/prisma/migrations/20260504110536_add_orgid_and_isprimary_in_oidc_issuer/migration.sqllibs/prisma-service/prisma/schema.prisma
💤 Files with no reviewable changes (1)
- libs/prisma-service/prisma/migrations/20260427080006_added_org_id_and_is_primary_in_oidc_issuer/migration.sql
| -- AlterTable | ||
| ALTER TABLE "oidc_issuer" ADD COLUMN "isPrimary" BOOLEAN NOT NULL DEFAULT false, | ||
| ADD COLUMN "orgId" UUID; | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE "oidc_issuer" ADD CONSTRAINT "oidc_issuer_orgId_fkey" FOREIGN KEY ("orgId") REFERENCES "organisation"("id") ON DELETE SET NULL ON UPDATE CASCADE; |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
set -euo pipefail
base="$(git merge-base HEAD origin/main 2>/dev/null || true)"
if [ -n "$base" ]; then
git diff --unified=40 "$base" -- \
libs/prisma-service/prisma/migrations/20260427080006_added_org_id_and_is_primary_in_oidc_issuer/migration.sql \
libs/prisma-service/prisma/migrations/20260504110536_add_orgid_and_isprimary_in_oidc_issuer/migration.sql
else
fd '20260427080006_added_org_id_and_is_primary_in_oidc_issuer|20260504110536_add_orgid_and_isprimary_in_oidc_issuer' \
libs/prisma-service/prisma/migrations \
-x sh -c 'echo "=== $1 ==="; sed -n "1,160p" "$1"' sh {}
fiRepository: credebl/platform
Length of output: 305
🏁 Script executed:
#!/bin/bash
set -euo pipefail
# Find and read the migration files from the directories
echo "=== April Migration (20260427080006) ==="
cat "libs/prisma-service/prisma/migrations/20260427080006_added_org_id_and_is_primary_in_oidc_issuer/migration.sql" 2>/dev/null || echo "File not found"
echo ""
echo "=== May Migration (20260504110536) ==="
cat "libs/prisma-service/prisma/migrations/20260504110536_add_orgid_and_isprimary_in_oidc_issuer/migration.sql" 2>/dev/null || echo "File not found"
# Check git diff for changes to the April migration
echo ""
echo "=== Git diff for April migration (if base exists) ==="
base="$(git merge-base HEAD origin/main 2>/dev/null || true)"
if [ -n "$base" ]; then
git diff "$base" -- libs/prisma-service/prisma/migrations/20260427080006_added_org_id_and_is_primary_in_oidc_issuer/migration.sql || echo "No differences found"
else
echo "Could not determine merge base"
fiRepository: credebl/platform
Length of output: 546
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo "=== All migration directories ==="
ls -la libs/prisma-service/prisma/migrations/ | grep "202604\|202605"
echo ""
echo "=== Files in migration directories ==="
find libs/prisma-service/prisma/migrations/ -name "*202604*" -o -name "*202605*" | sort
echo ""
echo "=== Git status for migration files ==="
git status --short -- libs/prisma-service/prisma/migrations/ | head -20
echo ""
echo "=== Recent migration-related commits ==="
git log --oneline -20 -- libs/prisma-service/prisma/migrations/ 2>/dev/null || echo "No recent commits found"Repository: credebl/platform
Length of output: 450
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo "=== Git log for April migration (all history) ==="
git log --full-history --oneline -- libs/prisma-service/prisma/migrations/20260427080006_added_org_id_and_is_primary_in_oidc_issuer/ 2>/dev/null | head -20
echo ""
echo "=== Git log for April migration with status ==="
git log --full-history -p --follow -- libs/prisma-service/prisma/migrations/20260427080006_added_org_id_and_is_primary_in_oidc_issuer/ 2>/dev/null | head -100
echo ""
echo "=== Show recent migration fix commit ==="
git show c73ff1d --stat 2>/dev/null || echo "Commit not found"
echo ""
echo "=== Files changed in that commit ==="
git diff-tree --no-commit-id --name-only -r c73ff1d 2>/dev/null | grep -i migration || echo "No migration files in that commit"Repository: credebl/platform
Length of output: 50372
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo "=== Check if April migration appears in git show for c73ff1d ==="
git show c73ff1d | grep -i "20260427080006" || echo "April migration not mentioned in commit"
echo ""
echo "=== Files added/deleted/modified in migrations folder ==="
git show --name-status c73ff1d -- libs/prisma-service/prisma/migrations/ 2>/dev/null | head -50 || echo "No migration changes found"
echo ""
echo "=== Check git log --full-history for April migration ==="
git log --full-history --all --oneline -- 'libs/prisma-service/prisma/migrations/*20260427080006*' 2>/dev/null || echo "No history found"
echo ""
echo "=== List all commits that touched migrations ==="
git log --oneline --all -- libs/prisma-service/prisma/migrations/ 2>/dev/null | head -10Repository: credebl/platform
Length of output: 5304
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo "=== Show April migration in f95dcd3 ==="
git show f95dcd3:libs/prisma-service/prisma/migrations/20260427080006_added_org_id_and_is_primary_in_oidc_issuer/migration.sql 2>/dev/null || echo "Not found in that commit"
echo ""
echo "=== Check if April migration was deleted in c73ff1d ==="
git show c73ff1d --raw -- libs/prisma-service/prisma/migrations/ 2>/dev/null | grep "20260427080006" || echo "April migration not in c73ff1d"
echo ""
echo "=== Show May migration in c73ff1d ==="
git show c73ff1d:libs/prisma-service/prisma/migrations/20260504110536_add_orgid_and_isprimary_in_oidc_issuer/migration.sql 2>/dev/null || echo "Not found"
echo ""
echo "=== Show commit message of c73ff1d ==="
git show --format=fuller c73ff1d | head -20Repository: credebl/platform
Length of output: 2172
Restore the April migration and keep it immutable.
The April migration (20260427080006) was created in commit f95dcd3 with essential schema changes to both oidc_issuer and oid4vc_credentials tables. The "fix: resolve migration issue" commit deleted it entirely and created a May migration containing only a subset of those changes. This creates two critical issues:
- Migration checksum drift: Any environment that already applied the April migration will fail to reconcile with the current migration history.
- Incomplete schema changes: The May migration omits the
oid4vc_credentialsandissued_oid4vc_credentialsforeign key updates from the original April migration.
Restore the April migration exactly as it existed in f95dcd3 and ship only this May migration as an additive follow-up if needed.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In
`@libs/prisma-service/prisma/migrations/20260504110536_add_orgid_and_isprimary_in_oidc_issuer/migration.sql`
around lines 1 - 6, Restore the deleted April migration 20260427080006 exactly
as it was in commit f95dcd3 so history remains immutable: re-add the original
SQL that modified both oidc_issuer and the oid4vc_credentials /
issued_oid4vc_credentials foreign keys (the exact ALTER TABLE and ADD CONSTRAINT
statements previously present) and do not modify its checksum; then keep the
current May migration (20260504110536) only as an additive follow-up that
includes exclusively the new changes (e.g., adding isPrimary and orgId on
oidc_issuer and its FK), ensuring the April migration file contents and names
match f95dcd3 precisely and the May migration only appends new ALTERs.
| ALTER TABLE "oidc_issuer" ADD COLUMN "isPrimary" BOOLEAN NOT NULL DEFAULT false, | ||
| ADD COLUMN "orgId" UUID; | ||
|
|
||
| -- AddForeignKey | ||
| ALTER TABLE "oidc_issuer" ADD CONSTRAINT "oidc_issuer_orgId_fkey" FOREIGN KEY ("orgId") REFERENCES "organisation"("id") ON DELETE SET NULL ON UPDATE CASCADE; |
There was a problem hiding this comment.
Backfill oidc_issuer.orgId from the existing orgAgentId mapping.
Line 595 in libs/prisma-service/prisma/schema.prisma already makes orgAgentId mandatory, and org_agents.orgId is the current org link. Without a data update here, every pre-existing issuer keeps orgId = NULL, so the new direct organisation relation misses historical rows.
Suggested migration patch
-- AlterTable
ALTER TABLE "oidc_issuer" ADD COLUMN "isPrimary" BOOLEAN NOT NULL DEFAULT false,
ADD COLUMN "orgId" UUID;
+UPDATE "oidc_issuer" AS oi
+SET "orgId" = oa."orgId"
+FROM "org_agents" AS oa
+WHERE oa."id" = oi."orgAgentId"
+ AND oi."orgId" IS NULL;
+
-- AddForeignKey
ALTER TABLE "oidc_issuer" ADD CONSTRAINT "oidc_issuer_orgId_fkey" FOREIGN KEY ("orgId") REFERENCES "organisation"("id") ON DELETE SET NULL ON UPDATE CASCADE;📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| ALTER TABLE "oidc_issuer" ADD COLUMN "isPrimary" BOOLEAN NOT NULL DEFAULT false, | |
| ADD COLUMN "orgId" UUID; | |
| -- AddForeignKey | |
| ALTER TABLE "oidc_issuer" ADD CONSTRAINT "oidc_issuer_orgId_fkey" FOREIGN KEY ("orgId") REFERENCES "organisation"("id") ON DELETE SET NULL ON UPDATE CASCADE; | |
| ALTER TABLE "oidc_issuer" ADD COLUMN "isPrimary" BOOLEAN NOT NULL DEFAULT false, | |
| ADD COLUMN "orgId" UUID; | |
| UPDATE "oidc_issuer" AS oi | |
| SET "orgId" = oa."orgId" | |
| FROM "org_agents" AS oa | |
| WHERE oa."id" = oi."orgAgentId" | |
| AND oi."orgId" IS NULL; | |
| -- AddForeignKey | |
| ALTER TABLE "oidc_issuer" ADD CONSTRAINT "oidc_issuer_orgId_fkey" FOREIGN KEY ("orgId") REFERENCES "organisation"("id") ON DELETE SET NULL ON UPDATE CASCADE; |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In
`@libs/prisma-service/prisma/migrations/20260504110536_add_orgid_and_isprimary_in_oidc_issuer/migration.sql`
around lines 2 - 6, The migration currently adds oidc_issuer.orgId and
immediately creates a FK, but it never backfills orgId from the existing
orgAgentId mapping so pre-existing issuers stay NULL; change the migration to
(1) add "orgId" as nullable on oidc_issuer (keep "isPrimary" as is), (2) run an
UPDATE that sets oidc_issuer.orgId = org_agents.orgId by joining
oidc_issuer.orgAgentId = org_agents.id (use the table names oidc_issuer and
org_agents and the columns orgAgentId and orgId), (3) if schema requires
non-null, ALTER COLUMN to NOT NULL afterwards, and only then add the FOREIGN KEY
constraint "oidc_issuer_orgId_fkey" referencing organisation(id) with the same
ON DELETE/ON UPDATE behavior.



What
Summary by CodeRabbit
Refactor