Migration (Database)

A database migration is a controlled change to your database structure — adding a table, renaming a column, or creating a relationship. Migrations track changes in files so you can apply them consistently across environments. For vibe coders, AI generates migrations when you modify your data model, but understanding them prevents data loss.

Example

You add a 'bio' field to your User model. Running 'npx prisma migrate dev' creates a migration file that adds the bio column to your users table. This same migration runs on your production database during deployment, keeping everything in sync.

Migrations are version control for your database. Just like Git tracks code changes, migrations track database structure changes.

Why Migrations Exist

You can't just edit a database like a text file:

  • Data already exists in the tables
  • Other environments need the same changes
  • You need to undo changes if something goes wrong
  • Team members need to stay in sync

Migrations solve all of this.

How Migrations Work

  1. Change your schema — Modify the data model
  2. Generate migration — Tool creates a migration file describing the change
  3. Review — Check that the migration does what you expect
  4. Apply — Run the migration to update the database
  5. Commit — Save the migration file in version control

Common Migration Operations

OperationExample
Add tableCreate a new products table
Add columnAdd avatar_url to users
Remove columnDrop unused legacy_field
Add indexSpeed up queries on email
Add relationshipConnect users to posts

Migration Dangers

Always be careful with:

  • Dropping columns — Data is permanently deleted
  • Renaming columns — Can break existing code
  • Changing types — May lose data precision
  • Production migrations — Test in staging first

AI and Migrations

AI generates migrations automatically when you change your schema. Always:

  1. Read the generated migration before applying
  2. Check for data-destructive operations
  3. Back up your database before risky migrations
  4. Test migrations on a copy of production data