Skip to content

fix(migrate): stabilize migrate diff when config has no datasource#29539

Open
narcilee7 wants to merge 1 commit into
prisma:mainfrom
narcilee7:fix-migrate-diff-datasource-29062
Open

fix(migrate): stabilize migrate diff when config has no datasource#29539
narcilee7 wants to merge 1 commit into
prisma:mainfrom
narcilee7:fix-migrate-diff-datasource-29062

Conversation

@narcilee7
Copy link
Copy Markdown

Summary

Fixes a prisma migrate diff failure mode where schema-engine could exit with Error in Schema engine in schema-only diff flows when prisma.config.ts has no datasource configured.

Closes #29062.

Root cause

SchemaEngineCLI only passed --datasource when config.datasource existed. In schema-only diff scenarios (--from/--to-schema), this could lead schema-engine startup to fail with a required --datasource argument error.

Changes

  • Always pass --datasource to schema-engine startup in SchemaEngineCLI.
  • Use JSON.stringify(this.datasource ?? {}) as fallback when datasource is absent.
  • Add regression coverage in MigrateDiff.test.ts for --from-empty --to-schema with no datasource in Prisma config.
  • Add dedicated fixture: schema-only-sqlite-no-config.

Testing

  • pnpm --filter @prisma/migrate test MigrateDiff.test.ts --runInBand --testNamePattern "without datasource in Prisma config"
  • The targeted regression test passes locally.
  • Full MigrateDiff.test.ts run in this environment has unrelated DB connectivity failures for provider suites requiring running local services (Postgres/MySQL/MSSQL/Cockroach).

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented May 7, 2026

CLA assistant check
All committers have signed the CLA.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 7, 2026

Review Change Stack

Summary by CodeRabbit

  • Bug Fixes
    • Fixed an issue where schema diffing failed when no datasource was explicitly configured in the Prisma schema. The migration tool now correctly handles datasourceless schema configurations during the diff operation.

Walkthrough

This PR fixes a flaky schema engine invocation error by ensuring SchemaEngineCLI always passes an explicit --datasource argument—even when no datasource is configured—using an empty object as a fallback. A new test case validates the fix by running a schema diff on a Prisma schema with no datasource URL.

Changes

Schema Engine Datasource Initialization

Layer / File(s) Summary
Core Fix
packages/migrate/src/SchemaEngineCLI.ts
internalInit() now unconditionally pushes --datasource with this.datasource ?? {} instead of conditionally adding the flag only when this.datasource is truthy.
Test Fixture
packages/migrate/src/__tests__/fixtures/schema-only-sqlite-no-config/prisma/schema.prisma
New Prisma schema fixture with SQLite datasource, prisma-client-js generator, and a Blog model for testing diff scenarios without datasource URL.
Integration Test
packages/migrate/src/__tests__/MigrateDiff.test.ts
New SQLite test case verifies MigrateDiff succeeds when diffing --from-empty to --to-schema with no datasource, and validates the Blog table is detected as added.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: fixing migrate diff when the Prisma config lacks a datasource, which is the primary objective addressed in this PR.
Description check ✅ Passed The description comprehensively explains the root cause, implementation details, testing approach, and is directly related to the changeset.
Linked Issues check ✅ Passed The PR addresses all coding requirements from #29062: always passing --datasource to schema-engine startup (avoiding missing argument errors), and includes regression test coverage for schema-only diffs without datasource configuration.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fix the identified issue: SchemaEngineCLI modification, MigrateDiff test addition, and schema-only test fixture creation.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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

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

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


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.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/migrate/src/__tests__/MigrateDiff.test.ts`:
- Around line 251-257: Add an explicit assertion that no error output was
produced for this test case: after the existing
expect(ctx.normalizedCapturedStdout()).toMatchInlineSnapshot(...) add an
assertion that ctx.mocked['console.error'] (or the stderr capture used in this
suite) is empty or was not called (e.g.,
expect(ctx.mocked['console.error']).toEqual([]) or toHaveBeenCalledTimes(0)).
This ensures the test checks both stdout via ctx.normalizedCapturedStdout() and
that there is no stderr via ctx.mocked['console.error'] for the Added tables
case.
🪄 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: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: e1f897f5-e011-4f21-9d15-7a0bdedfb841

📥 Commits

Reviewing files that changed from the base of the PR and between d6d9fc9 and 83b414c.

📒 Files selected for processing (3)
  • packages/migrate/src/SchemaEngineCLI.ts
  • packages/migrate/src/__tests__/MigrateDiff.test.ts
  • packages/migrate/src/__tests__/fixtures/schema-only-sqlite-no-config/prisma/schema.prisma

Comment on lines +251 to +257
expect(ctx.normalizedCapturedStdout()).toMatchInlineSnapshot(`
"
[+] Added tables
- Blog
"
`)
})
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 | 🔵 Trivial | ⚡ Quick win

Add an explicit stderr/console.error assertion for this new case.

For consistency with adjacent tests and to catch partial regressions, consider asserting no error output (e.g., ctx.mocked['console.error'] is empty) after Line 250.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/migrate/src/__tests__/MigrateDiff.test.ts` around lines 251 - 257,
Add an explicit assertion that no error output was produced for this test case:
after the existing
expect(ctx.normalizedCapturedStdout()).toMatchInlineSnapshot(...) add an
assertion that ctx.mocked['console.error'] (or the stderr capture used in this
suite) is empty or was not called (e.g.,
expect(ctx.mocked['console.error']).toEqual([]) or toHaveBeenCalledTimes(0)).
This ensures the test checks both stdout via ctx.normalizedCapturedStdout() and
that there is no stderr via ctx.mocked['console.error'] for the Added tables
case.

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.

Prisma migrate diff flaky Error in Schema engine

2 participants