Skip to content

feat(cli): warn on mismatch of global and local versions upon generate command#20951

Closed
akhill10 wants to merge 6 commits intoprisma:mainfrom
akhill10:feat/warn-on-mismatch-of-global-local-versions
Closed

feat(cli): warn on mismatch of global and local versions upon generate command#20951
akhill10 wants to merge 6 commits intoprisma:mainfrom
akhill10:feat/warn-on-mismatch-of-global-local-versions

Conversation

@akhill10
Copy link
Copy Markdown

@akhill10 akhill10 commented Sep 4, 2023

  • Compared the versions of global and local versions of prisma. If the versions doesn't match throw a warning.
  • Upon user confirmation ( press any key ) it continues of execution of generate command.

Fixes #1911
/claim #1911

@akhill10 akhill10 requested a review from a team as a code owner September 4, 2023 18:32
@akhill10 akhill10 requested review from Druue and SevInf and removed request for a team September 4, 2023 18:32
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Sep 4, 2023

CLA assistant check
All committers have signed the CLA.

Copy link
Copy Markdown
Contributor

@jharrell jharrell left a comment

Choose a reason for hiding this comment

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

@akhill10 thank you for your first PR!

However, I will need to request changes. This PR should do the following:

If a user is running prisma generate, meaning the generate command run from prisma that is globally installed (npm i -g prisma) and the local version of prisma and/or @prisma/client differ from the globally installed version, we should explicitly require the user to confirm they want to proceed.

Please make the following changes:

  1. check against globally installed prisma not .prisma/client
  2. check against locally installed prisma and @prisma/client (checking the package.json is probably a good first step)
  3. Please ask for explicit confirmation from the user. Y/y/yes/etc. Not any key.
  4. Please provide tests that fail on main and pass after your implementation

Thank you for your time!

@akhill10
Copy link
Copy Markdown
Author

akhill10 commented Sep 6, 2023

@jharrell Thanks for the feedback. Updated the PR with the requested changes.

https://www.loom.com/share/6c2def1c11164690a314dc456182c466?sid=138bb99a-8e3e-4332-adfe-27e8933872b1

  • I need help in writing the tests through spec files. Can you please help me with the steps on how to set up different global and local versions through jest?

@akhill10 akhill10 requested a review from jharrell September 6, 2023 12:47
@akhill10 akhill10 changed the title feat: warn on mismatch of global and local versions upon generate command feat(cli): warn on mismatch of global and local versions upon generate command Sep 7, 2023
@akhill10
Copy link
Copy Markdown
Author

akhill10 commented Sep 9, 2023

Hi @jharrell, did you get a chance to look at this?

@jharrell
Copy link
Copy Markdown
Contributor

@akhill10 thank you very much for the loom video and apologies for the delay on my end!

For this case, I think you should only test what is needed. That is to say, mock the results of isCurrentBinInstalledGlobally, getInstalledPrismaClientVersion, and getLocalPrismaVersion to show in the case of a mismatch we require specific confirmation.

@akhill10
Copy link
Copy Markdown
Author

akhill10 commented Sep 14, 2023

@jharrell I tried mocking the functions but result is not giving the mocked results in the generate.test.ts

//imports
import { Generate, getLocalPrismaVersion } from '../../Generate'
import { getInstalledPrismaClientVersion } from '../../utils/getClientVersion'

it('should warn on mismatch of global version and local client version', async () => {
    ctx.fixture('example-project')
    const versions = { getInstalledPrismaClientVersion, isCurrentBinInstalledGlobally, getLocalPrismaVersion }
    jest.spyOn(versions, 'getInstalledPrismaClientVersion').mockReturnValue(Promise.resolve('0.0.1'))
    jest.spyOn(versions, 'isCurrentBinInstalledGlobally').mockReturnValue('npm')
    jest.spyOn(versions, 'getLocalPrismaVersion').mockReturnValue(Promise.resolve('0.0.2'))

    const data = await ctx.cli('generate')
    expect(data.stdout).toMatchInlineSnapshot(`warn: Global prisma@0.0.0 and Local @prisma/client@0.0.1 don't match. This might lead to unexpected behavior. Would you like to proceed ? [yes/no]`)
});

Its still getting the example-project version value i.e 0.0.0 rather than 0.0.1. am I doing anything wrong here ?

@jharrell
Copy link
Copy Markdown
Contributor

@akhill10 I'm not sure. Please push your changes to this branch so that I can review.

@akhill10
Copy link
Copy Markdown
Author

akhill10 commented Sep 14, 2023

@jharrell Pushed the test case commit. Please take a look

@akhill10
Copy link
Copy Markdown
Author

Hello @jharrell , did you get a chance to review this? am I doing anything wrong in mocking the functions ?

@jharrell
Copy link
Copy Markdown
Contributor

Have you tried using .mock instead of .spyOn?

@akhill10
Copy link
Copy Markdown
Author

Yeah, I did but no luck.

@jharrell
Copy link
Copy Markdown
Contributor

Unfortunately, I'm not sure here. We'll need a way to mock these calls before we move forward.

@akhill10
Copy link
Copy Markdown
Author

Okay thank you, I try will some alternative approaches.
If am stuck, Any help from prisma team members would be appreciated.

@jharrell jharrell removed their request for review June 10, 2024 17:10
@jharrell jharrell requested review from jharrell and removed request for jharrell June 10, 2024 17:11
@wmadden
Copy link
Copy Markdown
Contributor

wmadden commented Jan 28, 2025

Hi @akhill10,

It looks like there haven't been any updates since @jharrell's last message to you. I'm closing this PR for now as part of our ongoing efforts to reduce the number of open issues and PRs in the repo.

If you feel like this is a mistake, feel free to reopen the PR and address the comments :)

@wmadden wmadden closed this Jan 28, 2025
Iceshen87 pushed a commit to Iceshen87/prisma that referenced this pull request Mar 3, 2026
Fixes prisma#1911

When using a globally installed prisma CLI with a different local
@prisma/client or prisma version, users will now see a warning message.

Implementation:
- Added versionMismatchChecker.ts with dependency injection for testability
- Modified Generate.ts to check version mismatch before running generate
- Added comprehensive test suite (11 tests, all passing)

The solution uses dependency injection to make the version checking
functions mockable in tests, addressing the issue from the previous
attempt in PR prisma#20951 where jest.spyOn could not properly mock the
module-level functions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Warn on mismatch between global prisma and local prisma or @prisma/client

6 participants