prisma: Detect schema drift helper / `migrate dev --exit-code`

Problem

I want to add a CI-job to make sure no schema changes have been done without corresponding migration files have been added. Had a near-miss today where we could’ve borked a whole table since the autogenerated migration later contained a drop column.

Suggested solution

Add an --exit-code that would fail migrate dev if there are changes (similar to git diff --exit-code)

Alternatives

Another command, i.e. migrate detect-drift

Additional context

I was asked to create this ticket on Slack - https://prisma.slack.com/archives/CBFFY3066/p1633945797065100

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 3
  • Comments: 15 (4 by maintainers)

Most upvoted comments

Steps I want to reproduce:

  1. Changing for instance a field in my schema.prisma from not-nullable to nullable
  2. Commit & push
  3. Run CI --> ❌ fails

and

  1. Changing for instance a field in my schema.prisma from not-nullable to nullable
  2. Run migrate dev and create my migration file
  3. Commit & push
  4. Run CI --> ✅ yay, you’ve checked in your migration correctly

For reference, the following command seems to do the job prisma migrate diff --preview-feature --exit-code --from-migrations <path to migrations> --to-schema-datamodel <path to schema> --shadow-database-url <database connection string>

I can’t get it working with prisma migrate status .

      # My **intention** with the following line is that we'll create any "pending migrations" 
      # however, if there isn't a pending it actually creates an _empty_ migration
      - run: yarn prisma migrate dev --create-only
      # This script will check if `prisma migrate status` is up-to-date
      - name: Detect schema drift
        run: |
          if yarn prisma migrate status | grep -q 'Database schema is up to date'; then
            echo "All OK"
          else;
            echo "Missing migration files"
            exit 1
          fi;

Full PR where I’ve tried to implement it: https://github.com/calendso/calendso/pull/901

hey @KATT - as you mentioned #11514 as a potential fix to this issue, we actually dont return an exit code as of now with diff. However, we want to change that this sprint, see #11566

  • --create-only flag is not a good option to detect pending migrations (it creates empty migration files if migration history and prisma schema are in sync)
  • there is a workaround via migrate status to detect if a pending migration hasn’t been generated yet
    • as it doesn’t return exit codes, a bash script (as shown in the PR) can be used to detect state
  • implementing an exit code flag seems like a vlid improvement to the DX

Can be done with further scripting, of course, and see if the created migration is an empty migration. However, it’s an even flakier solution. I’d rather just have a command from prisma that could do this reliably and exit 1.

We have a prisma migrate status command, I think that could work for you?

Any feedback / improvements on that command are welcome!

See docs at https://www.prisma.io/docs/reference/api-reference/command-reference#migrate-status