Skip to main content
The migrate command executes all pending database migrations in your application.

Syntax

Basic Usage

Run all pending migrations:
Output:
The migrate command only runs migrations that haven’t been executed yet. Previously run migrations are tracked in the migrations table.

How It Works

1. Database Connection

The command reads your database configuration from .env:
.env
Supported databases:
  • SQLite - File-based database (default)
  • PostgreSQL - Advanced relational database
  • MySQL - Popular relational database
  • MariaDB - MySQL-compatible database

2. Migration Tracking

Lyger creates a migrations table to track which migrations have been run:

3. Execution Order

Migrations run in chronological order based on their timestamp prefix:

Database Configuration

SQLite (Default)

.env
SQLite is perfect for development and small applications. No server setup required!

PostgreSQL

.env

MySQL

.env

Migration Process

Initial Run

First time running migrations:
What happens:
  1. Creates migrations table if it doesn’t exist
  2. Scans database/migrations/ directory
  3. Runs all migration files
  4. Records each migration in the migrations table

Subsequent Runs

Running migrations again:
What happens:
  1. Checks migrations table for already-run migrations
  2. Skips migrations that have already been executed
  3. Only runs new migrations
  4. Updates the migrations table

Source Code

The migration execution logic:
rawr (lines 425-461)

Helper Functions

Get ran migrations:
rawr (lines 474-478)
Record migration:
rawr (lines 480-485)

Complete Workflow

Common Scenarios

Fresh Installation

Setting up a new database:

Adding New Tables

After initial setup:
Output:

Team Development

When pulling changes with new migrations:

migrate:rollback

Rollback the last migration batch:
Rollback functionality is being implemented. Currently shows:

migrate:status

Check which migrations have been run:
Output:
Full status display is in development and will show a table of all migrations with their run status.

Error Handling

No Migrations Folder

Output:
Solution:

Database Connection Error

If database connection fails, check your .env file and ensure:
  • Database file exists (SQLite)
  • Database server is running (MySQL/PostgreSQL)
  • Credentials are correct
  • Database exists

Migration Error

If a migration fails:
  1. Check the migration file for syntax errors
  2. Verify table/column names don’t conflict
  3. Ensure foreign key constraints are valid
  4. Check database permissions

Best Practices

Do:
  • Run migrations in development before committing
  • Test migrations can run on fresh database
  • Keep migrations in version control
  • Create migrations for all schema changes
Don’t:
  • Modify migrations after they’ve been run in production
  • Delete migration files
  • Skip migrations
  • Manually modify the migrations table

Production Considerations

Before Running Migrations

  1. Backup your database
  2. Test in staging environment
  3. Review migration files

Running in Production

Troubleshooting

Migrations Not Running

Check if migrations have already been run:

Duplicate Migration

If you accidentally create a duplicate:
  1. Delete the newer migration file
  2. The older one will remain tracked in the database

Reset All Migrations

This will drop all tables and data!

Example Migration Set

Complete example for a blog application:

Next Steps

Make Migration

Create new migration files

Make Model

Create models for your tables

Schema Builder

Learn about available schema methods

Seeding

Populate tables with test data