Database Migrations.mp4

Database Migrations

When you're working with databases for the first time, the process of managing changes can feel overwhelming. But understanding how databases, schemas, and migrations work together is crucial to building real applications.

In this lesson, you'll learn how to manage database changes using a three-step process: define your schema, generate migrations, and apply those migrations to your database.

The Database Files

When you run the seed script, SQLite creates files on your file system that represent your entire database. Unlike databases like Postgres that run on remote servers, SQLite lets you see and even delete these files directly.

The data.db files in the file system

If you ever need to reset your database completely, you can simply delete the data.db file and re-run the seed script.

Deleting the data.db file from the codebase

The Three-Step Process

Managing database changes follows this pattern:

  1. Edit your schema in schema.ts
  2. Run pnpm db:generate to create migration files
  3. Run pnpm db:migrate to apply those migrations to your database

This process keeps your database synchronized with your code.

Steps To Complete

Look for scripts like db:seed, db:generate, and db:migrate. These are the tools you'll use to manage your database.

package.json showing the db scripts