API Reference
Class and method documentation for WPZylos Migrations.
Migrator
Migration runner — executes, rolls back, and tracks database migrations.
Namespace: WPZylos\Framework\Migrations
Constructor
public function __construct(
ContextInterface $context,
Connection $db,
MigrationRepository $repository,
?string $migrationsPath = null
)
| Parameter | Type | Default | Description |
|---|---|---|---|
$context | ContextInterface | — | Plugin context |
$db | Connection | — | Database connection |
$repository | MigrationRepository | — | Tracks which migrations have run |
$migrationsPath | ?string | null | Custom path (default: database/migrations) |
Methods
| Method | Return | Description |
|---|---|---|
run() | string[] | Run all pending migrations, returns names of those that ran |
rollback(int $steps = 1) | string[] | Rollback last $steps batches, returns names rolled back |
status() | array | Get status of all migrations (ran vs pending) |
getPending() | string[] | Get list of pending migration names |
Example
$migrator = $app->make(Migrator::class);
// Run pending
$ran = $migrator->run();
// ['2024_01_15_create_products_table']
// Rollback last batch
$rolled = $migrator->rollback();
// Check status
$status = $migrator->status();
// Get pending only
$pending = $migrator->getPending();
Migration (Base Class)
Abstract base class for all migrations. Extend this and implement up().
Namespace: WPZylos\Framework\Migrations
Public Methods
| Method | Return | Description |
|---|---|---|
setConnection(Connection $db) | void | Set the database connection (called automatically by Migrator) |
up() | void | Abstract — Run the migration |
down() | void | Reverse the migration (override if rollback needed) |
Protected Helper Methods
| Method | Return | Description |
|---|---|---|
create(string $table, array $columns, array $keys = []) | array | Create a table using dbDelta() |
dropTable(string $table) | bool | Drop a table (without prefix) |
drop(string $table) | bool | Alias for dropTable() |
charsetCollate() | string | Get WordPress charset collation string |
runDbDelta(string $sql) | array | Execute raw SQL via dbDelta() |
Migration Example
<?php
declare(strict_types=1);
namespace MyPlugin\Database\Migrations;
use WPZylos\Framework\Migrations\Migration;
class CreateProductsTable extends Migration
{
public function up(): void
{
$this->create('myplugin_products', [
'id' => 'bigint(20) unsigned NOT NULL AUTO_INCREMENT',
'name' => 'varchar(255) NOT NULL',
'price' => 'decimal(10,2) NOT NULL DEFAULT 0',
'created_at' => 'datetime DEFAULT CURRENT_TIMESTAMP',
], [
'PRIMARY KEY (id)',
]);
}
public function down(): void
{
$this->drop('myplugin_products');
}
}
MigrationRepository
Tracks which migrations have run using WordPress options.
Namespace: WPZylos\Framework\Migrations
Constructor
public function __construct(
ContextInterface $context,
bool $networkActivated = false
)
Methods
| Method | Return | Description |
|---|---|---|
getRan() | array | Get list of all migrations that have been run |
log(string $migration) | void | Record a migration as run |
remove(string $migration) | void | Remove a migration record |
getLastBatch() | int | Get the last batch number |
incrementBatch() | int | Increment and return new batch number |
clear() | void | Clear all migration records |
MigrationsServiceProvider
Registers the migration system with the container.
Registered Services
| Binding | Type | Description |
|---|---|---|
MigrationRepository::class | Singleton | Migration tracking |
Migrator::class | Singleton | Migration runner |
Dependencies
wpzylos-database—Connectionclasswpzylos-core—ContextInterface