src/content/docs/drizzle-kit-generate.mdx
import CodeTab from "@mdx/CodeTab.astro"; import CodeTabs from "@mdx/CodeTabs.astro"; import Section from "@mdx/Section.astro"; import Tab from "@mdx/Tab.astro"; import Tabs from "@mdx/Tabs.astro"; import Callout from "@mdx/Callout.astro"; import Prerequisites from "@mdx/Prerequisites.astro" import Npx from "@mdx/Npx.astro"; import SchemaFilePaths from "@mdx/SchemaFilePaths.mdx" import Dialects from "@mdx/Dialects.mdx"
drizzle-kit generatedrizzle-kit generate lets you generate SQL migrations based on your Drizzle schema upon declaration or on subsequent schema changes.
<Callout collapsed="How it works under the hood?">
Drizzle Kit generate command triggers a sequence of events:
migration.sql and snapshot.json in migration folder under current timestampexport const users = p.pgTable("users", { id: p.serial().primaryKey(), name: p.text(), email: p.text().unique(), };
┌────────────────────────┐
│ $ drizzle-kit generate │
└─┬──────────────────────┘
│
└ 1. read previous migration folders
2. find diff between current and previous schema
3. prompt developer for renames if necessary
┌ 4. generate SQL migration and persist to file
│ ┌─┴───────────────────────────────────────┐
│ 📂 drizzle
│ └ 📂 20242409125510_premium_mister_fear
│ ├ 📜 migration.sql
│ └ 📜 snapshot.json
v
```sql
-- drizzle/20242409125510_premium_mister_fear/migration.sql
CREATE TABLE "users" (
"id" SERIAL PRIMARY KEY,
"name" TEXT,
"email" TEXT UNIQUE
);
It's designed to cover code first approach of managing Drizzle migrations.
You can apply generated migrations using drizzle-kit migrate, using drizzle-orm's migrate(),
using external migration tools like bytebase or running migrations yourself directly on the database.
drizzle-kit generate command requires you to provide both dialect and schema path options,
you can set them either via drizzle.config.ts config file or via CLI options
<CodeTabs items={["With config file", "As CLI options"]}>
export default defineConfig({ dialect: "postgresql", schema: "./src/schema.ts", });
```shell
npx drizzle-kit generate
npx drizzle-kit generate --dialect=postgresql --schema=./src/schema.ts
You can have a single schema.ts file or as many schema files as you want spread out across the project.
Drizzle Kit requires you to specify path(s) to them as a glob via schema configuration option.
You can set custom migration file names by providing --name CLI option
npx drizzle-kit generate --name=init
📦 <project root>
├ 📂 drizzle
│ └ 📂 20242409125510_init
│ ├ 📜 snapshot.json
│ └ 📜 migration.sql
├ 📂 src
└ …
You can have multiple config files in the project, it's very useful when you have multiple database stages or multiple databases or different databases on the same project: <Npx> drizzle-kit generate --config=drizzle-dev.config.ts drizzle-kit generate --config=drizzle-prod.config.ts </Npx>
📦 <project root>
├ 📂 drizzle
├ 📂 src
├ 📜 .env
├ 📜 drizzle-dev.config.ts
├ 📜 drizzle-prod.config.ts
├ 📜 package.json
└ 📜 tsconfig.json
You can generate empty migration files to write your own custom SQL migrations for DDL alternations currently not supported by Drizzle Kit or data seeding. Extended docs on custom migrations - see here
drizzle-kit generate --custom --name=seed-users
INSERT INTO "users" ("name") VALUES('Dan'); INSERT INTO "users" ("name") VALUES('Andrew'); INSERT INTO "users" ("name") VALUES('Dandrew');
</Section>
### Ignore conflicts
<Callout type='warning'>
`--ignore-conflicts` available starting from `[email protected]`
</Callout>
In case you need `generate` command to skip commutativity checks and bypass it, you can use `--ignore-conflicts`. If there is a situation you want to use it, then
there is a big chance that `drizzle-kit` didn't check migrations right and it's a bug. Please report us your case, so we can fix it
```shell
drizzle-kit generate --ignore-conflicts
drizzle-kit generate has a list of cli-only options
custom | generate empty SQL for custom migration |
name | generate migration with custom name |
We recommend configuring drizzle-kit through drizzle.config.ts file,
yet you can provide all configuration options through CLI if necessary, e.g. in CI/CD pipelines, etc.
dialect | required | Database dialect, one of <Dialects/> |
schema | required | Path to typescript schema file(s) or folder(s) with multiple schema files |
out | Migrations output folder, default is ./drizzle | |
config | Configuration file path, default is drizzle.config.ts | |
breakpoints | SQL statements breakpoints, default is true |
Example of how to create a custom postgresql migration file named 0001_seed-users.sql
with Drizzle schema located in ./src/schema.ts and migrations folder named ./migrations instead of default ./drizzle.
We will also place drizzle config file in the configs folder.
Let's create config file:
📦 <project root>
├ 📂 migrations
├ 📂 configs
│ └ 📜 drizzle.config.ts
├ 📂 src
└ …
import { defineConfig } from "drizzle-kit";
export default defineConfig({
dialect: "postgresql",
schema: "./src/schema.ts",
out: "./migrations",
});
Now let's run
npx drizzle-kit generate --config=./configs/drizzle.config.ts --name=seed-users --custom
And it will successfully generate
<Section> ```plaintext {6} 📦 <project root> ├ … ├ 📂 migrations │ ├ 📂 20242409125510_init │ └ 📂 20242409125510_seed-users └ … ``` ```sql -- ./drizzle/20242409125510_seed-users/migration.sqlINSERT INTO "users" ("name") VALUES('Dan'); INSERT INTO "users" ("name") VALUES('Andrew'); INSERT INTO "users" ("name") VALUES('Dandrew');
</Section>