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:
Reads through migration folder and read all .sql migration files
Connects to the database and fetches entries from drizzle migrations log table
Based on previously applied migrations it will decide which new migrations to run
Runs SQL migrations and logs applied migrations to drizzle migrations table
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
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:
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
Extended example
Letβs generate SQL migration and apply it to our database using drizzle-kit generate and drizzle-kit migrate commands
drizzle.config.ts
src/schema.ts
Now letβs run
it will generate
Now letβs run
and our SQL migration is now successfully applied to the database β