Skip to main content

Overview

The Migration system provides version control for your database schema, allowing you to easily modify and share database structure across environments. It consists of two classes: Migration (base class for migrations) and Migrator (manages migration execution).

Class Reference

Lyger\Database\Migration

Base class for all database migrations. Location: Lyger/Database/Migration.php

Lyger\Database\Migrator

Manages database migration execution. Location: Lyger/Database/Migration.php:43

Migration Class

Abstract Methods

up()

Run the migration to create or modify database structures. Example:

down()

Reverse the migration (rollback changes). Example:

Helper Methods

getConnection()

Get the database PDO connection. Returns: PDO instance Example:

getSchema()

Get a Schema builder instance. Returns: Schema instance Example:

Migrator Class

Constructor

Create a new Migrator instance.
string
Path to migrations directory. Defaults to database/migrations/
Example:

Methods

path()

Set the migrations directory path.
string
required
Path to migrations directory
Returns: $this for method chaining Example:

run()

Run all pending migrations. Example:

rollback()

Rollback the last batch of migrations. Example:

reset()

Rollback all migrations. Example:

status()

Display the status of all migrations. Example:

make()

Create a new migration file.
string
required
Name of the migration (e.g., “create_users_table”)
Example:

getMigrationsPath()

Get the migrations directory path. Returns: Migration directory path

getMigrations()

Get all loaded migrations. Returns: Array of migration name => file path

loadMigrations()

Load all migration files from the migrations directory.

Creating Migrations

Using make()

The easiest way to create a migration:
This generates a timestamped file like 2026_03_08_120000_CreateUsersTable.php:

Manual Migration Creation

Create a file in database/migrations/ with the format: YYYY_MM_DD_HHiiss_DescriptiveName.php

Migration Examples

Creating a Table

Adding Columns to Existing Table

Creating Pivot Tables

Using Raw SQL

Complex Migration

Running Migrations

Run All Pending Migrations

Check Migration Status

Rollback Last Batch

Reset All Migrations

Fresh Migration (Reset + Run)

Migration Batching

Migrations are run in batches. Each time you call run(), all pending migrations are executed as a single batch. When you rollback(), the last batch is reversed. Example workflow:

Migrations Table

The Migrator automatically creates a migrations table to track which migrations have been run:
This table is created automatically the first time you run migrations. You don’t need to create it manually.

Best Practices

  • Never modify existing migrations that have been run in production. Create a new migration instead.
  • Always provide a down() method to reverse your migration
  • Test migrations locally before deploying to production
  • Use descriptive names for migrations (e.g., create_users_table, not just users)
  • Keep migrations focused - one logical change per migration
  • Order matters - migrations run in alphabetical order by filename
  • Use the Schema builder instead of raw SQL when possible for database portability

Common Patterns

Renaming a Table

Seeding Data in Migration

Conditional Migrations