Skip to content
 
 

Latest commit

 

History

24 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

prisma-migrations-runner

Run Prisma migrations in code without the CLI. This package is created for the this Prisma issue.

Notice

This package is not a replacement for the Prisma CLI. Currently, this package supports MySQL and PostgreSQL. Rollbacks are currently not yet supported.

Why?

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.

Features

  • Get migrations that have not been applied
  • Run any migrations for the prisma database

Installation

npm install prisma-migrations-runner

Usage

import { PrismaClient } from "@prisma/client";
import { runMigrations } from "prisma-migrations-runner";

const prisma = new PrismaClient();

await runMigrations(prisma);

MySQL vs PostgreSQL

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

Why no transaction for PostgreSQL?

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.

Importing directly

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";

License

MIT

About

Run Prisma migrations in code without the CLI.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages