Drizzle Migration Workflow After Schema Conversion
Best practices for using Drizzle Kit to generate and run migrations after converting SQL schemas to Drizzle ORM. Covers drizzle-kit push, generate, and migrate.
Detailed Explanation
Migration Workflow with Drizzle Kit
After converting your SQL to a Drizzle schema, the next step is setting up Drizzle Kit for migrations. Drizzle Kit is the CLI companion to Drizzle ORM that handles schema migrations.
Project Setup
First, install the necessary packages:
npm install drizzle-orm
npm install -D drizzle-kit
drizzle.config.ts
Create a configuration file at the project root:
import { defineConfig } from "drizzle-kit";
export default defineConfig({
schema: "./src/db/schema.ts",
out: "./drizzle",
dialect: "postgresql", // or "mysql" or "sqlite"
dbCredentials: {
url: process.env.DATABASE_URL!,
},
});
Two Migration Strategies
Drizzle Kit offers two approaches:
1. drizzle-kit push (Development)
Pushes schema changes directly to the database without generating SQL migration files. Ideal for rapid prototyping:
npx drizzle-kit push
This compares your TypeScript schema with the live database and applies changes directly. No migration files are created.
2. drizzle-kit generate + drizzle-kit migrate (Production)
Generates SQL migration files that can be reviewed and version-controlled:
npx drizzle-kit generate # Creates SQL migration files
npx drizzle-kit migrate # Runs pending migrations
Recommended Workflow
- Convert SQL to Drizzle using this tool
- Save the schema to
src/db/schema.ts - Use
pushduring development for quick iteration - Switch to
generatebefore deploying to staging/production - Review generated SQL migration files before running them
- Commit migration files to version control
Schema Organization
For larger projects, split your schema across multiple files:
src/db/
schema/
users.ts
posts.ts
comments.ts
index.ts # re-exports all tables
In drizzle.config.ts, use a glob pattern:
schema: "./src/db/schema/*.ts",
Introspect Existing Database
If you prefer to generate the Drizzle schema directly from a live database instead of converting SQL:
npx drizzle-kit introspect
This connects to your database, reads the schema, and generates Drizzle TypeScript files. The SQL converter tool is useful when you have SQL files but not a running database instance.
Handling Edge Cases
After conversion, review the generated schema for:
- ENUM types that need manual
pgEnum()definitions - Partial indexes that require raw SQL in migrations
- Triggers and functions that are beyond Drizzle's schema scope
- Column comments that need to be added manually
Use Case
You have just converted your SQL schema to Drizzle ORM and need to understand the next steps for setting up Drizzle Kit, generating migrations, and deploying schema changes to your database.