Back

Cloudflare D1 Prisma Migration Script

Alex Anderson
Alex AndersonFriday, April 5, 2024
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.

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:

  1. 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.
  2. Download the migrate.ts file from the gist here and put it in the prisma/ folder in your project.
  3. Add "db:migrate":"npx tsx prisma/migrate.ts" as a script to your package.json file.

Now you can create and apply your migrations by running npm run db:migrate create.

Enjoy!

Share this post

twitterfacebooklinkedin

Related Posts:

Interested in working with us?

Give us some details about your project, and our team will be in touch with how we can help.

Get in Touch