drizzle-kit push

This guide assumes familiarity with:

drizzle-kit push lets you literally push your schema and subsequent schema changes directly to the database while omitting SQL files generation, it’s designed to cover code first approach of Drizzle migrations.

How it works under the hood?

When you run Drizzle Kit push command it will:

  1. Read through your Drizzle schema file(s) and compose a json snapshot of your schema
  2. Pull(introspect) database schema
  3. Based on differences between those two it will generate SQL migrations
  4. Apply SQL migrations to the database
src/schema.ts
import * as p from "drizzle-orm/singlestore-core";

export const users = p.singlestoreTable("users", {
  id: p.int().primaryKey().autoincrement(),
  name: p.varchar({ length: 255 }),
};
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                  
β”‚ ~ drizzle-kit push  β”‚                  
β””β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                  
  β”‚                                           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”” Pull current datatabase schema ---------> β”‚                          β”‚
                                              β”‚                          β”‚
  β”Œ Generate alternations based on diff <---- β”‚         DATABASE         β”‚
  β”‚                                           β”‚                          β”‚
  β”” Apply migrations to the database -------> β”‚                          β”‚
                                       β”‚      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                       β”‚
  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
   create table users(`id` int auto_increment primary key, `name` varchar(255));

It’s the best approach for rapid prototyping and we’ve seen dozens of teams and solo developers successfully using it as a primary migrations flow in their production applications. It pairs exceptionally well with blue/green deployment strategy and serverless databases like Planetscale, Neon, Turso and others.




drizzle-kit push requires you to specify dialect, path to the schema file(s) and either database connection url or user:password@host:port/db params, you can provide them either via drizzle.config.ts config file or via CLI options:

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

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

Schema files path

You can have a single schema.ts file or as many schema files as you want spread out across the project. Drizzle Kit requires you to specify path(s) to them as a glob via schema configuration option.

Example 1
Example 2
Example 3
Example 4
πŸ“¦ <project root>
 β”œ ...
 β”œ πŸ“‚ drizzle
 β”œ πŸ“‚ src
 β”‚ β”œ ...
 β”‚ β”œ πŸ“œ index.ts
 β”‚ β”” πŸ“œ schema.ts 
 β”œ πŸ“œ drizzle.config.ts
 β”” πŸ“œ package.json
import { defineConfig } from "drizzle-kit";

export default defineConfig({
  schema: "./src/schema.ts",
});

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

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

Specifying database driver

Drizzle Kit does not come with a pre-bundled database driver, it will automatically pick an available SingleStore driver from your current project based on dialect: "singlestore" - see discussion.

SingleStore uses MySQL-compatible connection parameters, so you usually only need to provide dbCredentials.

Including tables

drizzle-kit push will manage all SingleStore tables by default. You can configure the list of tables via tablesFilter.

tablesFilterglob based table names filter, e.g. ["users", "user_info"] or "user*". Default is "*"

Let’s configure drizzle-kit to operate with all tables in the connected SingleStore database.

drizzle.config.ts
import { defineConfig } from "drizzle-kit";

export default defineConfig({
  dialect: "singlestore",
  schema: "./src/schema.ts",
  dbCredentials: {
    url: "mysql://user:password@host:3306/dbname",
  },
  tablesFilter: ["*"],
});
npx drizzle-kit push

Extended list of configurations

drizzle-kit push has a list of cli-only options

verboseprint all SQL statements prior to execution
strictalways ask for approval before executing SQL statements
forceauto-accept all data-loss statements

npm
yarn
pnpm
bun
npx drizzle-kit push --strict --verbose --force



We recommend configuring drizzle-kit through drizzle.config.ts file, yet you can provide all configuration options through CLI if necessary, e.g. in CI/CD pipelines, etc.

dialectrequiredDatabase dialect, one of postgresql mysql sqlite turso singlestore mssql cockroach
schemarequiredPath to typescript schema file(s) or folder(s) with multiple schema files
tablesFilterTable name filter
urlDatabase connection string
userDatabase user
passwordDatabase password
hostHost
portPort
databaseDatabase name
configConfiguration file path, default=drizzle.config.ts
npm
yarn
pnpm
bun
npx drizzle-kit push dialect=singlestore schema=src/schema.ts url=mysql://user:password@host:3306/dbname
npx drizzle-kit push dialect=singlestore schema=src/schema.ts --tablesFilter=β€˜user*’ url=mysql://user:password@host:3306/dbname

Extended example

Let’s declare drizzle schema in the project and push it to the database via drizzle-kit push command

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

export default defineConfig({
  dialect: "singlestore",
  schema: "./src/schema.ts",
  dbCredentials: {
    url: "mysql://user:password@host:3306/dbname"
  },
});

Now let’s run

npx drizzle-kit push

it will pull existing(empty) schema from the database and generate SQL migration and apply it under the hood

CREATE TABLE `users`(
  `id` int auto_increment primary key,
  `name` varchar(255)
)

DONE βœ