Skip to content

fix(cli): fix race condition during push db (#2491)#2587

Merged
ymc9 merged 2 commits into
zenstackhq:devfrom
jesus-gomez-aetonix:dev
Apr 21, 2026
Merged

fix(cli): fix race condition during push db (#2491)#2587
ymc9 merged 2 commits into
zenstackhq:devfrom
jesus-gomez-aetonix:dev

Conversation

@jesus-gomez-aetonix

@jesus-gomez-aetonix jesus-gomez-aetonix commented Apr 17, 2026

Copy link
Copy Markdown
Contributor

Because a hardcoded prefix creating the temporal schema file ~schema.prisma, multiple processes running push db will suffer a race condition.

Change the hardcoded prefix with a sort of UUID.

Fix: 2491.

Summary by CodeRabbit

  • New Features
    • Added a CLI flag to optionally generate randomized temporary Prisma schema filenames for migrate and db push flows.
  • Bug Fixes
    • Reduces temporary schema filename collisions/overwrites during concurrent CLI operations.
  • Tests
    • Added tests validating default and randomized temporary schema generation and cleanup.

Because a hardcoded prefix creating the temporal schema file `~schema.prisma`, multiple processes running `push db` will suffer a race condition.

Change the hardcoded prefix with a sort of UUID.
@coderabbitai

coderabbitai Bot commented Apr 17, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 1767a14f-93c6-4e3a-ae7c-a5b143287744

📥 Commits

Reviewing files that changed from the base of the PR and between 933ad1a and 3bd3535.

📒 Files selected for processing (5)
  • packages/cli/src/actions/action-utils.ts
  • packages/cli/src/actions/db.ts
  • packages/cli/src/actions/migrate.ts
  • packages/cli/src/index.ts
  • packages/cli/test/action-utils.test.ts
✅ Files skipped from review due to trivial changes (1)
  • packages/cli/test/action-utils.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/cli/src/actions/migrate.ts

📝 Walkthrough

Walkthrough

generateTempPrismaSchema was changed to accept an options object ({ folder?: string; randomName?: boolean }), to optionally emit randomized temp Prisma schema filenames, and callers plus CLI options were updated; new tests validate default and randomized filename behaviors.

Changes

Cohort / File(s) Summary
Temp schema utility
packages/cli/src/actions/action-utils.ts
Refactored generateTempPrismaSchema(zmodelPath, opts = {}) to use opts.folder ?? path.dirname(zmodelPath) and, when opts.randomName is true, write ~schema.<uuid>.prisma (via crypto.randomUUID()); always resolves and returns the full schema path.
DB action & API types
packages/cli/src/actions/db.ts
Added randomPrismaSchemaName?: boolean to PushOptions; runPush now calls generateTempPrismaSchema(schemaFile, { randomName: options.randomPrismaSchemaName }).
Migrate actions & options
packages/cli/src/actions/migrate.ts
Added randomPrismaSchemaName?: boolean to CommonOptions; callers pass { folder: prismaSchemaDir, randomName: options.randomPrismaSchemaName } into generateTempPrismaSchema.
CLI surface
packages/cli/src/index.ts
Introduced new CLI flag --random-prisma-schema-name (boolean, default false) and registered it on migrate subcommands and db push.
Tests
packages/cli/test/action-utils.test.ts
New Vitest suite that verifies default filename ~schema.prisma and randomized ~schema.<uuid>.prisma outputs exist and are distinct; cleans up generated files.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

Poem

🐇 I dug a tiny schema den,

where names now hop and change again.
Some wear ~schema.prisma plain,
some sport UUIDs in a chain.
Hooray — no collisions in my glen!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: addressing a race condition in the db push command by using random Prisma schema names to avoid collisions.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Comment thread packages/cli/src/actions/action-utils.ts Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
packages/cli/src/index.ts (1)

79-83: Optional: minor help-text polish.

The description says (default: "~schema.prisma"), but that's the default filename when the flag is not passed — which is a little confusing to read as a "default" for this boolean flag. Consider phrasing it as behavior instead, e.g.:

✏️ Suggested wording
-    const randomPrismaSchemaNameOption = new Option(
-        '--random-prisma-schema-name',
-        'append a random UUID to the temporary Prisma schema filename to avoid collisions between concurrent runs sharing a working directory (default: "~schema.prisma")',
-    ).default(false);
+    const randomPrismaSchemaNameOption = new Option(
+        '--random-prisma-schema-name',
+        'append a random UUID to the temporary Prisma schema filename (e.g., ~schema.<uuid>.prisma) to avoid collisions between concurrent runs sharing a working directory',
+    ).default(false);

Also worth verifying: reusing a single Option instance across multiple .addOption(...) calls (migrate dev/reset/deploy/status/resolve + db push) is done elsewhere in this file (e.g., schemaOption, noVersionCheckOption), so this follows established convention — no change needed there.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/cli/src/index.ts` around lines 79 - 83, Update the help text for the
Option instance randomPrismaSchemaNameOption to avoid implying a boolean flag
has a filename "default"; reword the description to describe behavior instead
(e.g., "when set, append a random UUID to the temporary Prisma schema filename
to avoid collisions between concurrent runs sharing a working directory;
otherwise the temporary filename is \"~schema.prisma\""). Edit the Option
constructor call that creates randomPrismaSchemaNameOption to use the new
phrasing so the flag's purpose and fallback filename are clear.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@packages/cli/src/index.ts`:
- Around line 79-83: Update the help text for the Option instance
randomPrismaSchemaNameOption to avoid implying a boolean flag has a filename
"default"; reword the description to describe behavior instead (e.g., "when set,
append a random UUID to the temporary Prisma schema filename to avoid collisions
between concurrent runs sharing a working directory; otherwise the temporary
filename is \"~schema.prisma\""). Edit the Option constructor call that creates
randomPrismaSchemaNameOption to use the new phrasing so the flag's purpose and
fallback filename are clear.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: c2f3efd1-2253-4d64-8a85-3355e42c44d8

📥 Commits

Reviewing files that changed from the base of the PR and between f962351 and 933ad1a.

📒 Files selected for processing (5)
  • packages/cli/src/actions/action-utils.ts
  • packages/cli/src/actions/db.ts
  • packages/cli/src/actions/migrate.ts
  • packages/cli/src/index.ts
  • packages/cli/test/action-utils.test.ts
✅ Files skipped from review due to trivial changes (1)
  • packages/cli/test/action-utils.test.ts

…om-prisma-schema-name

The UUID suffix introduced in f962351 to avoid collisions between
concurrent `zen db push` runs is now opt-in. Default behavior restores
the upstream `~schema.prisma` filename; pass `--random-prisma-schema-name`
to `db push` or any `migrate` subcommand to re-enable the UUID suffix.

Addresses the maintainer's review comment on zenstackhq#2491.

Assisted-by: Cursor:claude-opus-4.7 [Shell] [Read] [StrReplace] [Write]

@ymc9 ymc9 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks great to me! Thanks for the quick follow-up.

@ymc9 ymc9 merged commit b749cd0 into zenstackhq:dev Apr 21, 2026
9 checks passed
@claude claude Bot mentioned this pull request Apr 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Race condition because zen push db creates a temporal file with a fixed name

2 participants