Skip to main content

Migrations

Migrations let you evolve your database schema over time. Each migration has an up (apply) and down (rollback) function, and Prism tracks which migrations have been applied in a _prism_migrations table.

Defining Migrations

Migration Definitions

Running Migrations

1

Create a Migrator

2

Run Pending Migrations

3

Check Status

Prism automatically creates a _prism_migrations table to track applied migrations.

Rolling Back

Rollback Last Migration
Rollbacks can cause data loss. Always back up your database before rolling back in production.

Migration Workflow

A typical workflow for adding a new feature:
Adding Comments Feature

Server Startup Pattern

Migrate on Boot
Keep migration version numbers sequential and never modify a migration that’s already been applied in production. Instead, create a new migration to make changes.

Best Practices

Use names like create_users, add_user_avatar, create_comments_index so you can tell what each migration does at a glance.
Even if you think you won’t need to roll back, having a down function makes development much smoother.
Before running migrations in production, test them against a copy of your production database to catch issues early.
Each migration should do one thing. It’s better to have 10 small migrations than one giant one.