Become a Gold Sponsor

drizzle-kit migrate

This guide assumes familiarity with:

drizzle-kit migrate lets you apply SQL migrations generated by drizzle-kit generate. It’s designed to cover code first(option 3) approach of managing Drizzle migrations.

How it works under the hood?

Drizzle Kit migrate command triggers a sequence of events:

  1. Reads through migration folder and read all .sql migration files
  2. Connects to the database and fetches entries from drizzle migrations log table
  3. Based on previously applied migrations it will decide which new migrations to run
  4. Runs SQL migrations and logs applied migrations to drizzle migrations table
  β”œ πŸ“‚ drizzle       
  β”‚ β”œ πŸ“‚ _meta
  β”‚ β”œ πŸ“œ 0000_premium_mister_fear.sql
  β”‚ β”” πŸ“œ 0001_delicate_professor_xavie.sql
  β”” …
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                  
β”‚ $ drizzle-kit migrate β”‚                  
β””β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                  
  β”‚                                                         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                                         
  β”” 1. reads migration.sql files in migrations folder       β”‚                          β”‚
    2. fetch migration history from database -------------> β”‚                          β”‚
  β”Œ 3. pick previously unapplied migrations <-------------- β”‚         DATABASE         β”‚
  β”” 4. apply new migration to the database ---------------> β”‚                          β”‚
                                                            β”‚                          β”‚
                                                            β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
[βœ“] done!        

drizzle-kit migrate command requires you to specify both dialect and database connection credentials, you can provide them either via drizzle.config.ts config file or via CLI options

With config file
As CLI options
// drizzle.config.ts
import { defineConfig } from "drizzle-kit";

export default defineConfig({
  dialect: "postgresql",
  schema: "./src/schema.ts",
  dbCredentials: {
    url: "postgresql://user:password@host:port/dbname"
  },
});
npx drizzle-kit migrate

Applied migrations log in the database

Upon running migrations Drizzle Kit will persist records about successfully applied migrations in your database. It will store them in migrations log table named __drizzle_migrations.

You can customise both table and schema(PostgreSQL only) of that table via drizzle config file:

drizzle.config.ts
export default defineConfig({
  dialect: "postgresql",
  schema: "./src/schema.ts",
  dbCredentials: {
    url: "postgresql://user:password@host:port/dbname"
  },
  migrations: {
    table: 'my-migrations-table', // `__drizzle_migrations` by default
    schema: 'public', // used in PostgreSQL only, `drizzle` by default
  },
});

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 on the same project:

npm
yarn
pnpm
bun
npx drizzle-kit migrate --config=drizzle-dev.config.ts
npx drizzle-kit migrate --config=drizzle-prod.config.ts
πŸ“¦ <project root>
 β”œ πŸ“‚ drizzle
 β”œ πŸ“‚ src
 β”œ πŸ“œ .env
 β”œ πŸ“œ drizzle-dev.config.ts
 β”œ πŸ“œ drizzle-prod.config.ts
 β”œ πŸ“œ package.json
 β”” πŸ“œ tsconfig.json

Extended example

Let’s generate SQL migration and apply it to our database using drizzle-kit generate and drizzle-kit migrate commands

πŸ“¦ <project root>
 β”œ πŸ“‚ drizzle
 β”œ πŸ“‚ src
 β”‚ β”œ πŸ“œ schema.ts
 β”‚ β”” πŸ“œ index.ts
 β”œ πŸ“œ drizzle.config.ts
 β”” …
drizzle.config.ts
src/schema.ts
import { defineConfig } from "drizzle-kit";

export default defineConfig({
  dialect: "postgresql",
  schema: "./src/schema.ts",
  dbCredentials: {
    url: "postgresql://user:password@host:port/dbname"
  },
  migrations: {
    table: 'journal', 
    schema: 'drizzle', 
  },
});

Now let’s run

npx drizzle-kit generate --name=init

it will generate

πŸ“¦ <project root>
 β”œ …
 β”œ πŸ“‚ migrations
 β”‚ β”œ πŸ“‚ _meta
 β”‚ β”” πŸ“œ 0000_init.sql 
 β”” …
-- ./drizzle/0000_init.sql

CREATE TABLE "users"(
  id serial primary key,
  name text,
)

Now let’s run

npx drizzle-kit migrate

and our SQL migration is now successfully applied to the database βœ