orm.drizzle.team Open in urlscan Pro
2606:4700:3031::6815:1b66  Public Scan

Submitted URL: https://orm.drizzle.team/kit-docs/overview#running-migrations
Effective URL: https://orm.drizzle.team/kit-docs/overview
Submission: On April 24 via api from US — Scanned from DE

Form analysis 0 forms found in the DOM

Text Content

From 0 to production - The Modern React Tutorial! Watch now →
SearchK

19k+

Light

Dark

System

Docs
Drizzle Kit
Learn

Overview
Overview Migration files Schema updates Running migrations Configuration
Prototyping with db push Overview How it works? Introspecting with db pull
Drizzle Studio [NEW]
Quick start Configuration List of commands Config Reference FAQ &
Troubleshooting
System Light Dark

Become a Sponsor

Twitter

Discord


DRIZZLE KIT

Our most fundamental design principles of Drizzle ORM is to always stay
explicit, provide opt-in solutions and never interfere. Our migrations toolkit
is not an exception.

npm
yarn
pnpm
bun

npm i -D drizzle-kit

yarn add -D drizzle-kit

pnpm add -D drizzle-kit

bun add -D drizzle-kit


OVERVIEW

Drizzle Kit — is a CLI companion for automatic SQL migrations generation and
rapid prototyping.

Conceptually it’s very simple, you just declare a Drizzle ORM TypeScript schema
and generate an SQL migration from it.

You can then change the original schema and Drizzle Kit will generate new
migrations, that way you can have DDL source of truth in one type-safe place and
under version control.

Drizzle Kit lets you split your schema in different files and even have multiple
schemas for different databases in one project. You can rapidly prototype
database schema and push it directly to the database.

Last but not least — you can pull the schema from an existing database in a
matter of seconds ⚡️


MIGRATION FILES

Migrations history is stored in .sql files in the migrations folder



📦 <project root>
 ├ ...
 ├ 📂 drizzle
 │ ├ 📂 _meta
 │ ├ 📜 0000_premium_mister_fear.sql
 │ ├ 📜 0001_absurd_toad.sql
 │ ├ 📜 0002_adorable_human_torch.sql
 │ └ 📜 0003_boring_silver_sable.sql
 ├ ...
 ├ 📂 src
 ├ 📜 package.json
 └ ...

Each SQL migration file contains statements which you apply to the database
through. Drizzle ORM migration package or any other way suitable for your
business case or personal preference.

0000_premium_mister_fear.sql
schema.ts

CREATE TABLE IF NOT EXISTS "user" (
  "id" serial,
  "name" text,
  "email" text,
  "password" text,
  "role" text,
  "created_at" timestamp,
  "updated_at" timestamp
);



export const user = pgTable("user", {
  id: serial("id"),
  name: text("name"),
  email: text("email"),
  password: text("password"),
  role: text("role").$type<"admin" | "customer">(),
  createdAt: timestamp("created_at"),
  updatedAt: timestamp("updated_at"),
});


SCHEMA UPDATES

Whenever you apply changes to the schema you just rerun $ drizzle-kit
generate...{:bash} and it will generate SQL migration for you completely
automatically in most of the cases.

0001_absurd_toad.sql
schema.ts

ALTER TABLE "user" ADD COLUMN "is_verified" boolean;



export const user = pgTable("user", {
  id: serial("id"),
  name: text("name"),
  email: text("email"),
  password: text("password"),
  isVerified: boolean("is_verified"),
  role: text("role").$type<"admin" | "customer">(),
  createdAt: timestamp("created_at"),
  updatedAt: timestamp("updated_at"),
});


RUNNING MIGRATIONS

We’re unopinionated on how you should run your migrations, you can run them
manually using SQL generated files, using external tools, etc. or use Drizzle
ORM.

We provide you a useful way to run generated migrations with migrate function
which we implement for each driver and dialect specifically.

Drizzle will automatically keep track of applied migrations in your database.

ℹ️

drizzle and migrate imports depend on the database driver you’re using.

PostgreSQL
MySQL
SQLite

import { drizzle } from "drizzle-orm/postgres-js";
import { migrate } from "drizzle-orm/postgres-js/migrator";
import postgres from "postgres";

const sql = postgres("...", { max: 1 })
const db = drizzle(sql);

await migrate(db, { migrationsFolder: "drizzle" });

await sql.end();

import { drizzle } from "drizzle-orm/mysql2";
import { migrate } from "drizzle-orm/mysql2/migrator";
import { createConnection } from "mysql2";

const connection = createConnection("...")
const db = drizzle(connection);

await migrate(db, { migrationsFolder: "drizzle" });

await connection.end();

import { drizzle } from "drizzle-orm/better-sqlite3";
import { migrate } from "drizzle-orm/better-sqlite3/migrator";
import Database from "better-sqlite3";

const betterSqlite = new Database(":memory:");
const db = drizzle(betterSqlite);

migrate(db, { migrationsFolder: "drizzle" });

betterSqlite.close()


CONFIGURATION

Drizzle Kit lets you declare configurations in TypeScript, JavaScript or JSON
configuration files.

You can have autocomplete experience and a very convenient environment variables
flow!

drizzle.config.ts
drizzle.config.js
drizzle.config.json

import { defineConfig } from 'drizzle-kit'

export default defineConfig({
 schema: "./schema.ts",
  driver: 'pg',
  dbCredentials: {
    connectionString: process.env.DB_URL,
  },
  verbose: true,
  strict: true,
})

/** @type { import("drizzle-kit").Config } */
export default {
  schema: "./schema.ts",
  driver: 'pg',
  dbCredentials: {
    connectionString: process.env.DB_URL,
  }
};

{
  "out": "./migrations-folder",
  "schema": "./src/db",
  "breakpoints": false
}

For list of all configuration params — see here.


PROTOTYPING WITH DB PUSH

Drizzle Kit lets you alter you database schema and rapidly move forward with a
db push command.

That’s very handy when you have remote databases like Neon, Planetscale or
Turso.


OVERVIEW

If you want to iterate quickly during local development or if your project
doesn’t require migration files, Drizzle offers a useful command called
drizzle-kit push.

When do you need to use the ‘push’ command?

 1. During the prototyping and experimentation phase of your schema on a local
    environment.
 2. When you are utilizing an external provider that manages migrations and
    schema changes for you (e.g., PlanetScale).
 3. If you are comfortable modifying the database schema before your code
    changes can be deployed.


HOW IT WORKS?

When you run the command drizzle-kit push drizzle executes the following steps:

 1. It retrieves your schema from the database and converts it to the
    “drizzle-schema” format.
 2. The command reads all your schema files containing drizzle tables and
    converts them to the “drizzle-schema” format as well.
 3. Drizzle then compares the two schemas and generates a set of statements that
    need to be executed against your database. These statements ensure that the
    database is synchronized with the schemas defined in your code.



$ drizzle-kit push:mysql

$ drizzle-kit push:sqlite

For extended push examples see here.


INTROSPECTING WITH DB PULL

Drizzle Kit lets you pull DDL from existing database and prints a TypeScript
schema file completely automatically.

$ drizzle-kit introspect:mysql ...



📦 <project root>
 ├ ...
 ├ 📂 drizzle
 │ ├ 📂 _meta
 │ ├ 📜 0000_premium_mister_fear.sql
 │ └ 📜 schema.ts
 ├ ...
 ├ 📂 src
 ├ 📜 package.json
 └ ...



 export const user = pgTable("user", {
    id: serial("id"),
    name: text("name"),
    email: text("email"),
    password: text("password"),
    role: text("role").$type<"admin" | "customer">(),
    createdAt: timestamp("created_at"),
    updatedAt: timestamp("updated_at"),
  });

For extended introspect examples — see here.


DRIZZLE STUDIO [NEW]

Drizzle Kit comes with bundled Drizzle Studio database browser and lets you
launch it locally with one command.



Studio requires drizzle config file with schema and dbCredentials provided.



drizzle-kit studio

drizzle-kit studio --port 3000 ## custom port
drizzle-kit studio --verbose   ## log all sql statements

🖥 Drizzle Studio Demo 🖥 Open Drizzle Studio

GitHub Discord
Benchmarks


Studio


Become a Gold Sponsor

GitLab is the only place where enterprises build mission‑critical software. ads
via Carbon