drizzle-kit generate lets you generate SQL migrations based on you Drizzle schema upon declaration or on subsequent schema changes.
How it works under the hood?
Drizzle Kit generate command triggers a sequence of events:
It will read through your Drizzle schema file(s) and compose a json snapshot of your schema
It will read through your previous migrations folders and compare current json snapshot to the most recent one
Based on json differences it will generate SQL migrations
Save migration.sql and snapshot.json in migration folder under current timestamp
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
With config file
As CLI options
Schema files path
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.
Example 1
Example 2
Example 3
Example 4
Custom migration file name
You can set custom migration file names by providing --name CLI option
Multiple configuration files in one project
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:
npm
yarn
pnpm
bun
Custom migrations
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
Extended list of available configurations
drizzle-kit generate has a list of cli-only options
custom
generate empty SQL for custom migration
name
generate migration with custom name
npm
yarn
pnpm
bun
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 postgresqlmysqlsqliteturso
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
Extended example
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.