Schema and migrations

Generate, review, and apply migrations

Turn a TypeScript schema change into reviewed SQL, apply it once, and understand when direct schema push is the wrong workflow.

8 minute lesson

~~~

A migration is a database change with history. Treat generated SQL as a draft you must understand before it touches important data.

Run generate after changing the schema. Drizzle Kit compares its snapshots and writes SQL. Read that SQL, commit it with the schema change, then run migrate. Drizzle records applied migrations so the same file is not applied twice.

npx drizzle-kit generate --name=initial_schema
# Read the generated SQL in ./drizzle
npx drizzle-kit migrate

drizzle-kit push skips versioned SQL files and updates the database from the schema difference. That is fast for disposable prototypes. For a shared or production database, reviewed migrations give you evidence, coordination, and a repeatable deployment path.

A migration that adds a required column to a populated table may fail or invent unsafe data. Plan the data transition in steps instead of accepting a generated statement blindly.

Generate and apply the initial migration, then add an optional archivedAt column. Review the SQL before applying it and prove a second migrate run makes no duplicate change.

Lesson completed

Take this course offline

Get every free book and course as PDF and EPUB files.

Get the download library →