migrate command executes all pending database migrations in your application.
Syntax
Basic Usage
Run all pending migrations: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
- SQLite - File-based database (default)
- PostgreSQL - Advanced relational database
- MySQL - Popular relational database
- MariaDB - MySQL-compatible database
2. Migration Tracking
Lyger creates amigrations 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:- Creates
migrationstable if it doesn’t exist - Scans
database/migrations/directory - Runs all migration files
- Records each migration in the
migrationstable
Subsequent Runs
Running migrations again:- Checks
migrationstable for already-run migrations - Skips migrations that have already been executed
- Only runs new migrations
- Updates the
migrationstable
Source Code
The migration execution logic:rawr (lines 425-461)
Helper Functions
Get ran migrations:rawr (lines 474-478)
rawr (lines 480-485)
Complete Workflow
Common Scenarios
Fresh Installation
Setting up a new database:Adding New Tables
After initial setup:Team Development
When pulling changes with new migrations:Related Commands
migrate:rollback
Rollback the last migration batch:Rollback functionality is being implemented. Currently shows:
migrate:status
Check which migrations have been run:Full status display is in development and will show a table of all migrations with their run status.
Error Handling
No Migrations Folder
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:- Check the migration file for syntax errors
- Verify table/column names don’t conflict
- Ensure foreign key constraints are valid
- 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
- Modify migrations after they’ve been run in production
- Delete migration files
- Skip migrations
- Manually modify the migrations table
Production Considerations
Before Running Migrations
-
Backup your database
-
Test in staging environment
-
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:- Delete the newer migration file
- The older one will remain tracked in the database
Reset All Migrations
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