Migrator
class Migrator
Constants
| protected STATE_CONTEXT_KEY_PREFIX |
|
Properties
| static protected null|Connection | $connectionResolverCallback | The custom connection resolver callback. |
|
| protected string|null | $connection | The name of the default connection. |
|
| protected string[] | $paths | The paths to all of the migration files. |
|
| static protected array<string, null|Migration> | $requiredPathCache | The paths that have already been required. |
|
| protected OutputStyle|null | $output | The output interface implementation. |
|
| static protected list<string> | $withoutMigrations | The pending migrations to skip. |
Methods
Create a new migrator instance.
Run the pending migrations at a given path.
Get the migration files eligible to run, respecting registered exclusions.
Get the migration files that have not yet run.
Get list of pending migrations to skip.
Run an array of migrations.
Run "up" a migration instance.
Rollback the last migration operation.
Get the migrations for a rollback operation.
Rollback the given migrations.
Rolls all of the currently applied migrations back.
Reset the given migrations.
Run "down" a migration instance.
Run a migration inside a transaction if the database supports it.
Pretend to run the migrations.
Get all of the queries that would be run for a migration.
Run a migration method on the given connection.
Resolve a migration instance from a file.
Resolve a migration instance from a migration path.
Determine if the migration at the given path should be skipped.
Get the distinct connections declared by the migrations at the given paths.
Generate a migration class name based on the migration file name.
Get all of the migration files in a given path.
Require in all the migration files in a given path.
Get the name of the migration.
Register a custom migration path.
Get all of the custom migration paths.
Set the pending migrations to skip.
Get the default connection name.
Resolve a connection name through the "migrations_connection" config key.
Execute the given callback using the given connection as the default connection.
Set the default connection name.
Resolve the database connection instance.
Get the migration repository instance.
Determine if the migration repository exists.
Determine if any migrations have been run.
Delete the migration repository data store.
Get the file system instance.
Write to the console's output.
Determine whether the given migration event has listeners.
Flush all static state.
Details
at line 95
__construct(MigrationRepositoryInterface $repository, ConnectionResolverInterface $resolver, Filesystem $files)
Create a new migrator instance.
at line 109
array
run(array|string $paths = [], array $options = [])
Run the pending migrations at a given path.
at line 136
array
getPendingMigrations(array $files, array $ran)
Get the migration files eligible to run, respecting registered exclusions.
at line 148
protected array
pendingMigrations(array $files, array $ran)
Get the migration files that have not yet run.
at line 166
protected array
migrationsToSkip()
Get list of pending migrations to skip.
at line 179
void
runPending(array $migrations, array $options = [])
Run an array of migrations.
at line 230
protected void
runUp(string $file, int $batch, bool $pretend)
Run "up" a migration instance.
at line 272
array
rollback(array|string $paths = [], array $options = [])
Rollback the last migration operation.
at line 300
protected array
getMigrationsForRollback(array $options)
Get the migrations for a rollback operation.
at line 321
protected array
rollbackMigrations(array $migrations, array|string $paths, array $options)
Rollback the given migrations.
at line 366
array
reset(array|string $paths = [], bool $pretend = false)
Rolls all of the currently applied migrations back.
at line 390
protected array
resetMigrations(array $migrations, array $paths, bool $pretend = false)
Reset the given migrations.
at line 407
protected void
runDown(string $file, object $migration, bool $pretend)
Run "down" a migration instance.
at line 433
protected void
runMigration(object $migration, string $method, string|null $name = null)
Run a migration inside a transaction if the database supports it.
at line 466
protected void
pretendToRun(object $migration, string $method)
Pretend to run the migrations.
at line 487
protected array
getQueries(object $migration, string $method)
Get all of the queries that would be run for a migration.
at line 512
protected void
runMethod(Connection $connection, object $migration, string $method)
Run a migration method on the given connection.
Sets the coroutine Context key so Schema/DB facade calls inside the migration body resolve to the correct connection. This handles both the migrator's --database override and per-migration $connection properties. Context is the single source of truth for the scoped default — no worker-global state is mutated.
at line 532
object
resolve(string $file)
Resolve a migration instance from a file.
at line 542
protected object
resolvePath(string $path)
Resolve a migration instance from a migration path.
at line 564
bool
shouldSkip(string $path)
Determine if the migration at the given path should be skipped.
at line 576
array
getMigrationConnections(array|string $paths, string|null $defaultConnection = null)
Get the distinct connections declared by the migrations at the given paths.
at line 607
protected string
getMigrationClass(string $migrationName)
Generate a migration class name based on the migration file name.
at line 617
array
getMigrationFiles(array|string $paths)
Get all of the migration files in a given path.
at line 633
void
requireFiles(array $files)
Require in all the migration files in a given path.
at line 643
string
getMigrationName(string $path)
Get the name of the migration.
at line 653
void
path(string $path)
Register a custom migration path.
Paths remain on the shared migrator and affect every subsequent run.
at line 664
array
paths()
Get all of the custom migration paths.
at line 677
static void
withoutMigrations(array $migrations)
Set the pending migrations to skip.
The list persists in a static property for the worker lifetime and applies to every subsequent migration run.
at line 686
string|null
getConnection()
Get the default connection name.
at line 716
static string|null
resolveMigrationConnectionName(string|null $name)
Resolve a connection name through the "migrations_connection" config key.
Allows pooled connections (PgBouncer, pgdog, Neon, Supabase, etc.) to declare an unpooled sibling that migration operations should route through. Session state required by migrations — advisory locks, LOCK TABLE, temp tables — is incompatible with transaction-pooling mode.
The target must be terminal: it may omit migrations_connection or reference itself, but it may not route migrations to another connection.
When $name is null, falls back to the "effective default connection" — the current coroutine's Context override first, then the configured default (database.default). This mirrors DatabaseManager::getDefaultConnection() so programmatic flows that wrap migrations in DB::usingConnection() or DatabaseManager::setDefaultConnection() route through the scoped default.
Defensively passes the name through when the container has no "config" binding so unit tests that construct Migrator without a booted framework still work.
at line 778
mixed
usingConnection(string|null $name, callable $callback)
Execute the given callback using the given connection as the default connection.
Snapshots the prior coroutine Context value and stored migrator connection independently, then restores both directly in finally. The two values can differ, so routing restoration through setConnection() would collapse them.
at line 809
void
setConnection(string|null $name)
Set the default connection name.
Honors the target connection's "migrations_connection" config key. The swapped name is propagated via coroutine Context (so Schema/DB facade calls during migration resolve correctly), via the repository source (so the migrations table lands on the same target), and via the stored connection (so subsequent resolveConnection() calls use it). The migrator and database repository keep this mutable state in the current coroutine.
at line 831
Connection
resolveConnection(string|null $connection)
Resolve the database connection instance.
Applies the "migrations_connection" swap so per-migration connection overrides (via Migration::getConnection()) are also routed to the unpooled sibling when the named connection opts in.
at line 856
static void
resolveConnectionsUsing(Closure $callback)
Set a connection resolver callback.
The callback persists in a static property for the worker lifetime and runs on every migration's connection resolution.
at line 865
protected Grammar
getSchemaGrammar(Connection $connection)
Get the schema grammar out of a migration connection.
at line 879
MigrationRepositoryInterface
getRepository()
Get the migration repository instance.
at line 887
bool
repositoryExists()
Determine if the migration repository exists.
at line 895
bool
hasRunAnyMigrations()
Determine if any migrations have been run.
at line 903
void
deleteRepository()
Delete the migration repository data store.
at line 911
Filesystem
getFilesystem()
Get the file system instance.
at line 921
Migrator
setOutput(OutputStyle $output)
Set the output implementation that should be used by the console.
at line 968
protected void
write(string $component, mixed ...$arguments)
Write to the console's output.
at line 985
protected bool
hasMigrationEventListeners(string $event)
Determine whether the given migration event has listeners.
at line 1000
void
fireMigrationEvent(MigrationEvent $event)
Fire the given event for the migration.
Fetches the dispatcher from the container each time to ensure Event::fake() and other runtime swaps are respected (the Migrator may be constructed before fakes are set up).
at line 1012
static void
flushState()
Flush all static state.