Become a Gold Sponsor

Migrations with Drizzle Kit

This guide assumes familiarity with:

Drizzle Kit is a CLI tool for managing SQL database migrations with Drizzle.

npm
yarn
pnpm
bun
npm i drizzle-kit
IMPORTANT

Make sure to first go through Drizzle get started and migration foundamentals and pick SQL migration flow that suites your business needs best.

Based on your schema, Drizzle Kit let’s you generate and run SQL migration files, push schema directly to the database, pull schema from database, spin up drizzle studio and has a couple of utility commands.

npm
yarn
pnpm
bun
npx drizzle-kit generate
npx drizzle-kit migrate
npx drizzle-kit push
npx drizzle-kit pull
npx drizzle-kit check
npx drizzle-kit up
npx drizzle-kit studio
drizzle-kit generatelets you generate SQL migration files based on your Drizzle schema either upon declaration or on subsequent changes, see here.
drizzle-kit migratelets you apply generated SQL migration files to your database, see here.
drizzle-kit pulllets you pull(introspect) database schema, convert it to Drizzle schema and save it to your codebase, see here
drizzle-kit pushlets you push your Drizzle schema to database either upon declaration or on subsequent schema changes, see here
drizzle-kit studiowill connect to your database and spin up proxy server for Drizzle Studio which you can use for convenient database browsing, see here
drizzle-kit checkwill walk through all generate migrations and check for any race conditions(collisions) of generated migrations, see here
drizzle-kit upused to upgrade snapshots of previously generated migrations, see here

Drizzle Kit is configured through drizzle.config.ts configuration file or via CLI params.
It’s required to at least provide SQL dialect and schema path for Drizzle Kit to know how to generate migrations.

πŸ“¦ <project root>
 β”œ πŸ“‚ drizzle
 β”œ πŸ“‚ src
 β”œ πŸ“œ .env
 β”œ πŸ“œ drizzle.config.ts  <--- Drizzle config file
 β”œ πŸ“œ package.json
 β”” πŸ“œ tsconfig.json
simple config
extended config
import { defineConfig } from "drizzle-kit";

export default defineConfig({
  dialect: "postgresql",
  schema: "./src/schema.ts",
});

You can provide Drizzle Kit config path via CLI param, it’s very useful when you have multiple database stages or multiple databases or different databases on the same project:

npm
yarn
pnpm
bun
npx drizzle-kit push --config=drizzle-dev.drizzle.config
npx drizzle-kit push --config=drizzle-prod.drizzle.config
πŸ“¦ <project root>
 β”œ πŸ“‚ drizzle
 β”œ πŸ“‚ src
 β”œ πŸ“œ .env
 β”œ πŸ“œ drizzle-dev.config.ts
 β”œ πŸ“œ drizzle-prod.config.ts
 β”œ πŸ“œ package.json
 β”” πŸ“œ tsconfig.json