Skip to main content

What are Migrations?

Migrations are like version control for your database. They’re scripts that create, modify, or delete database tables and columns in a structured way.
Real-world analogy: Migrations are like renovation blueprints for a house. Each migration is a set of instructions to change the structure - add a room, remove a wall, install new plumbing. You can apply them in order or roll them back if needed.

Creating Migrations

Using the CLI

Migration File Structure

Migration Naming: IlanaORM automatically adds timestamps to migration names to ensure they run in the correct order.

Creating Tables

Basic Table Creation

Column Types Reference

Primary Keys

String Types

Numeric Types

Date/Time Types

Other Types

Column Modifiers

Available Modifiers

Modifying Tables

Adding Columns

Modifying Columns

Dropping Columns

Indexes and Constraints

Creating Indexes

Foreign Key Constraints

Constraint Actions

UUID Primary Keys

Pivot Tables for Many-to-Many

Polymorphic Tables

Running Migrations

Basic Commands

Rollback Migrations

Fresh Migrations

Migration Status

Advanced Migration Patterns

Conditional Migrations

Data Migrations

Raw SQL in Migrations

Database-Specific Features

PostgreSQL

MySQL

Migration Best Practices

1. Always Write Down Methods

2. Use Descriptive Names

3. Test Migrations Both Ways

4. Keep Migrations Small

5. Don’t Modify Old Migrations

Troubleshooting

Problem: Error saying migration already existsSolutions:
  • Check if migration was already run: npx ilana migrate:status
  • Use different migration name
  • Roll back if needed: npx ilana migrate:rollback
Problem: Cannot create foreign key constraintSolutions:
  • Ensure referenced table exists first
  • Check column types match (both should be same type)
  • Verify referenced column exists
  • Run migrations in correct order
Problem: Column already exists errorSolutions:
  • Check if column exists: schema.hasColumn()
  • Use conditional migration
  • Check migration history: npx ilana migrate:list

Next Steps

Database Seeders

Learn to populate your database with sample data

Model Factories

Generate realistic test data automatically