Run Prisma migrations in code without the CLI. This package is created for the this Prisma issue.
This package is not a replacement for the Prisma CLI. Currently, this package supports MySQL and PostgreSQL. Rollbacks are currently not yet supported.
I am building applications that are going to be deployed to environments where I don't have access to the Prisma CLI. Which is why I created this package, to be able to run migrations in code.
- Get migrations that have not been applied
- Run any migrations for the prisma database
npm install prisma-migrations-runnerimport { PrismaClient } from "@prisma/client";
import { runMigrations } from "prisma-migrations-runner";
const prisma = new PrismaClient();
await runMigrations(prisma);The runner auto-detects the provider from your Prisma client config, but the two implementations differ in important ways:
| MySQL | PostgreSQL | |
|---|---|---|
| Transaction handling | Each migration runs inside a $transaction block |
Migrations run without a transaction wrapper |
| SQL execution | Parsed line-by-line, statements executed individually | Entire migration file executed as a single multi-statement query via $executeRawUnsafe |
| Failure recording | Migration is not recorded on failure (transaction rolls back) | Failed migration is recorded in _prisma_migrations with the error in the logs column, matching Prisma CLI behavior |
| Applied-migration tracking | Finds the last applied migration and runs everything after it (ordered) | Tracks all applied migrations as a set, skipping any that are already present (handles out-of-order and rolled-back cases) |
| Timestamp columns | DATETIME(3) |
TIMESTAMPTZ |
PostgreSQL DDL statements like CREATE INDEX CONCURRENTLY and ALTER TYPE ... ADD VALUE cannot run inside a transaction block. Since Prisma migrations frequently use these, the PostgreSQL runner executes each migration outside of a transaction — the same approach the Prisma CLI takes.
You can also import the provider-specific module directly:
// PostgreSQL
import { runMigrations } from "prisma-migrations-runner/postgres";
// MySQL
import { runMigrations } from "prisma-migrations-runner/mysql";MIT