Migrator
class Migrator
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 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.
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 77
__construct(MigrationRepositoryInterface $repository, ConnectionResolverInterface $resolver, Filesystem $files)
Create a new migrator instance.
at line 91
array
run(array|string $paths = [], array $options = [])
Run the pending migrations at a given path.
at line 118
protected array
pendingMigrations(array $files, array $ran)
Get the migration files that have not yet run.
at line 136
protected array
migrationsToSkip()
Get list of pending migrations to skip.
at line 149
void
runPending(array $migrations, array $options = [])
Run an array of migrations.
at line 200
protected void
runUp(string $file, int $batch, bool $pretend)
Run "up" a migration instance.
at line 242
array
rollback(array|string $paths = [], array $options = [])
Rollback the last migration operation.
at line 270
protected array
getMigrationsForRollback(array $options)
Get the migrations for a rollback operation.
at line 291
protected array
rollbackMigrations(array $migrations, array|string $paths, array $options)
Rollback the given migrations.
at line 336
array
reset(array|string $paths = [], bool $pretend = false)
Rolls all of the currently applied migrations back.
at line 360
protected array
resetMigrations(array $migrations, array $paths, bool $pretend = false)
Reset the given migrations.
at line 377
protected void
runDown(string $file, object $migration, bool $pretend)
Run "down" a migration instance.
at line 403
protected void
runMigration(object $migration, string $method, string|null $name = null)
Run a migration inside a transaction if the database supports it.
at line 432
protected void
pretendToRun(object $migration, string $method)
Pretend to run the migrations.
at line 453
protected array
getQueries(object $migration, string $method)
Get all of the queries that would be run for a migration.
at line 478
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 498
object
resolve(string $file)
Resolve a migration instance from a file.
at line 508
protected object
resolvePath(string $path)
Resolve a migration instance from a migration path.
at line 532
array
getMigrationConnections(array|string $paths, string|null $defaultConnection = null)
Get the distinct connections declared by the migrations at the given paths.
at line 563
protected string
getMigrationClass(string $migrationName)
Generate a migration class name based on the migration file name.
at line 573
array
getMigrationFiles(array|string $paths)
Get all of the migration files in a given path.
at line 589
void
requireFiles(array $files)
Require in all the migration files in a given path.
at line 599
string
getMigrationName(string $path)
Get the name of the migration.
at line 607
void
path(string $path)
Register a custom migration path.
at line 617
array
paths()
Get all of the custom migration paths.
at line 630
static void
withoutMigrations(array $migrations)
Set the pending migrations to skip.
Boot or tests only. The list persists in a static property for the worker lifetime and applies to every subsequent migration run.
at line 638
string|null
getConnection()
Get the default connection name.
at line 668
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 730
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 762
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). Uses CoroutineContext rather than $this->resolver->setDefaultConnection() so no worker-global state is mutated — concurrent coroutines are unaffected.
at line 784
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 809
static void
resolveConnectionsUsing(Closure $callback)
Set a connection resolver callback.
Boot-only. The callback persists in a static property for the worker lifetime and runs on every migration's connection resolution.
at line 817
protected Grammar
getSchemaGrammar(Connection $connection)
Get the schema grammar out of a migration connection.
at line 831
MigrationRepositoryInterface
getRepository()
Get the migration repository instance.
at line 839
bool
repositoryExists()
Determine if the migration repository exists.
at line 847
bool
hasRunAnyMigrations()
Determine if any migrations have been run.
at line 855
void
deleteRepository()
Delete the migration repository data store.
at line 863
Filesystem
getFilesystem()
Get the file system instance.
at line 871
Migrator
setOutput(OutputStyle $output)
Set the output implementation that should be used by the console.
at line 883
protected void
write(string $component, mixed ...$arguments)
Write to the console's output.
at line 900
protected bool
hasMigrationEventListeners(string $event)
Determine whether the given migration event has listeners.
at line 915
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 927
static void
flushState()
Flush all static state.