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)
Steps I want to reproduce:
schema.prisma
from not-nullable to nullableand
schema.prisma
from not-nullable to nullablemigrate dev
and create my migration fileFor 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
.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)migrate status
to detect if a pending migration hasn’t been generated yetCan 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