Skip to content

fix: harden seed script org settings upsert and P2002 error handling#28527

Draft
romitg2 wants to merge 2 commits intomainfrom
devin/1774072978-fix-flaky-integration-tests-seed-script
Draft

fix: harden seed script org settings upsert and P2002 error handling#28527
romitg2 wants to merge 2 commits intomainfrom
devin/1774072978-fix-flaky-integration-tests-seed-script

Conversation

@romitg2
Copy link
Member

@romitg2 romitg2 commented Mar 21, 2026

What does this PR do?

Fixes flaky org-admin integration tests (isAdmin, retrieveScopedAccessibleUsers, _get, _patch) that fail intermittently when the CI database is cached.

Root cause: The seed script in createOrganizationAndAddMembersAndTeams had two bugs:

  1. Stale org settings on re-seed: When an organization already existed (cached DB), the function returned immediately without ensuring organizationSettings.isAdminAPIEnabled was set. This caused isAdminGuard to return false for org admins.

  2. P2002 error abandoned entire org creation: If any single org member hit a unique constraint violation, the batch-level catch abandoned the entire function — meaning the org team, memberships, profiles, and settings were never created, even though some member users already existed.

Changes

scripts/seed.ts:

  • Upsert organizationSettings when the org already exists, instead of silently returning
  • Move P2002 error handling inside each member's callback so one member's duplicate doesn't abandon the entire org
  • On P2002, recover the existing user from the DB and return it with membership metadata so downstream org setup (profiles, memberships, teams) still proceeds correctly
  • Remove PII (username) from P2002 log message
  • Use prisma.user.upsert for usersOutsideOrg instead of prisma.user.create to avoid P2002 there too

Test files (isAdmin.integration-test.ts, retrieveScopedAccessibleUsers.integration-test.ts):

  • Remove beforeAll hooks that were workarounds for the seed script bug — the proper fix is in the seed script itself

Updates since last revision

Addressed Cubic AI review feedback (confidence ≥ 9/10):

Mandatory Tasks (DO NOT REMOVE)

  • I have self-reviewed the code (A decent size PR without self-review might be rejected).
  • I have updated the developer docs in /docs if this PR makes changes that would require a documentation change. N/A — seed script and test-only changes.
  • I confirm automated tests are in place that prove my fix is effective or that my feature works.

How should this be tested?

The integration test suite should pass consistently on both fresh and cached CI databases:

VITEST_MODE=integration yarn test apps/api/v1/test/lib/utils/isAdmin.integration-test.ts
VITEST_MODE=integration yarn test apps/api/v1/test/lib/utils/retrieveScopedAccessibleUsers.integration-test.ts
VITEST_MODE=integration yarn test apps/api/v1/test/lib/bookings/_get.integration-test.ts
VITEST_MODE=integration yarn test "apps/api/v1/test/lib/bookings/[id]/_patch.integration-test.ts"

To reproduce the flaky state: run the seed twice (simulating a cached DB) and verify the tests still pass on the second run.

Human Review Checklist

  • Verify the email_username compound unique constraint exists in the Prisma schema (used in both the usersOutsideOrg upsert and the P2002 recovery findUnique)
  • Confirm orgData.organizationSettings shape is compatible with prisma.organizationSettings.upsert for both update and create fields
  • Verify the P2002 recovery path: when an existing user is fetched and returned, confirm that downstream code (profile creation, membership creation, team assignment) handles it correctly — particularly that it doesn't attempt to re-create resources that already exist
  • Verify removing beforeAll workarounds from test files is safe — integration tests should always run against a seeded DB

Link to Devin session: https://app.devin.ai/sessions/6be6fd80dedc4fc8891cb523d7d4ddbe


Open with Devin

Co-Authored-By: romitgabani1 <romitgabani1.work@gmail.com>
@devin-ai-integration
Copy link
Contributor

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR that start with 'DevinAI' or '@devin'.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

@romitg2 romitg2 added ready-for-e2e run-ci Approve CI to run for external contributors labels Mar 21, 2026
Copy link
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 4 additional findings.

Open in Devin Review

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 issues found across 3 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="scripts/seed.ts">

<violation number="1" location="scripts/seed.ts:333">
P2: The new org-settings upsert uses `existingTeam.id` without verifying `existingTeam.isOrganization`, so it can update the wrong team type on slug/root collisions.</violation>

<violation number="2" location="scripts/seed.ts:410">
P1: Custom agent: **Avoid Logging Sensitive Information**

Do not log usernames directly; this added log line exposes PII in seed/CI logs.</violation>

<violation number="3" location="scripts/seed.ts:411">
P1: Skipping P2002 members (`return null`) drops existing users from org membership/profile creation, so retries can still produce incomplete organization seeding.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

@github-actions
Copy link
Contributor

Devin AI is addressing Cubic AI's review feedback

A Devin session has been created to address the issues identified by Cubic AI.

View Devin Session

…eturning null

- Replace username interpolation in log message with generic text (Cubic violation #2, confidence 9/10)
- On P2002, fetch the existing user from DB and return it with membership data instead of returning null, which was dropping users from org setup on retries (Cubic violation #3, confidence 9/10)

Co-Authored-By: bot_apk <apk@cognition.ai>
@romitg2 romitg2 marked this pull request as draft March 21, 2026 09:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-e2e run-ci Approve CI to run for external contributors size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant