The most important thing about Drizzle is that you can use its schema as a source of truth for everything else. drizzle-kit is a CLI companion for Drizzle ORM which you can use to generate SQL migrations automatically based on your schema changes
or apply those changes directly to the database.
See detailed docs for extended examples and walk-throughs.
Quick start
Declare your schema
💡
The schema files SHOULD NOT contain any runtime logic besides defining your DB schema.
In particular, your DB connection should be defined separately.
Otherwise, that logic will be executed whenever you run any drizzle-kit commands.
Schema-related type definitions, on the other hand, are allowed and even encouraged, as they are not executed at runtime.
Create the config
Create a drizzle.config.ts file in your project root:
Generate the migration
This will generate a migration SQL file:
Run the migrations
Drizzle ORM is designed to be an opt-in solution at any point of your development flow.
You can either run the generated migrations via Drizzle, or treat them as generic SQL migrations and run them with any other tool.
To run the migrations with Drizzle, you can use the migrate() helper, available for every supported driver:
Configurations
Custom table for migrations
By default, all information about executed migrations will be stored in the database inside the __drizzle_migrations table,
and for PostgreSQL, inside the drizzle schema. However, you can configure where to store those records.
To add a custom table name for migrations stored inside your database, you should use the migrationsTable option
Usage example
Custom schema for migrations
💡
Works only with PostgreSQL databases
To add a custom schema name for migrations stored inside your database, you should use the migrationsSchema option