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 β βββ¬ββββββββββββββββββββββ β ββββββββββββββββββββββββββββ β 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
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: