Cloudflare D1 Prisma Migration Script
Cloudflare D1 and Prisma now work together using a new adapter! This is great if you want the convenience and developer experience that Prisma provides, while using the ultra-fast, globally-available database built into Cloudflare Workers and Pages.
However, the adapter doesn't support Prisma migrations out-of-the-box. Instead, you have to generate the migrations manually using prisma migrate diff
, and then apply them manually using wrangler d1 migrations apply
.
We wanted to simplify this process further by creating a simple migrations CLI. It automatically detects your Prisma schema and database bindings in wrangler.toml
, but all of that is configurable. And since you're copy/pasting the file into your codebase, you can tweak it however you like.
Here's how to use it:
- Follow the instructions in the Cloudflare blog post for getting Prisma and D1 set up in your project. The gist that has the migrate script also summarizes the setup instructions.
- Download the
migrate.ts
file from the gist here and put it in theprisma/
folder in your project. - Add
"db:migrate":"npx tsx prisma/migrate.ts"
as a script to yourpackage.json
file.
Now you can create and apply your migrations by running npm run db:migrate create
.
Enjoy!