Migrator
class Migrator
Properties
| static protected Closure|null | $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.
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.
Flush all static state.
Details
at line 74
__construct(MigrationRepositoryInterface $repository, ConnectionResolverInterface $resolver, Filesystem $files)
Create a new migrator instance.
at line 88
array
run(array|string $paths = [], array $options = [])
Run the pending migrations at a given path.
at line 115
protected array
pendingMigrations(array $files, array $ran)
Get the migration files that have not yet run.
at line 133
protected array
migrationsToSkip()
Get list of pending migrations to skip.
at line 146
void
runPending(array $migrations, array $options = [])
Run an array of migrations.
at line 191
protected void
runUp(string $file, int $batch, bool $pretend)
Run "up" a migration instance.
at line 231
array
rollback(array|string $paths = [], array $options = [])
Rollback the last migration operation.
at line 256
protected array
getMigrationsForRollback(array $options)
Get the migrations for a rollback operation.
at line 276
protected array
rollbackMigrations(array $migrations, array|string $paths, array $options)
Rollback the given migrations.
at line 317
array
reset(array|string $paths = [], bool $pretend = false)
Rolls all of the currently applied migrations back.
at line 341
protected array
resetMigrations(array $migrations, array $paths, bool $pretend = false)
Reset the given migrations.
at line 358
protected void
runDown(string $file, object $migration, bool $pretend)
Run "down" a migration instance.
at line 384
protected void
runMigration(object $migration, string $method, string|null $name = null)
Run a migration inside a transaction if the database supports it.
at line 409
protected void
pretendToRun(object $migration, string $method)
Pretend to run the migrations.
at line 430
protected array
getQueries(object $migration, string $method)
Get all of the queries that would be run for a migration.
at line 455
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 475
object
resolve(string $file)
Resolve a migration instance from a file.
at line 485
protected object
resolvePath(string $path)
Resolve a migration instance from a migration path.
at line 507
protected string
getMigrationClass(string $migrationName)
Generate a migration class name based on the migration file name.
at line 517
array
getMigrationFiles(array|string $paths)
Get all of the migration files in a given path.
at line 533
void
requireFiles(array $files)
Require in all the migration files in a given path.
at line 543
string
getMigrationName(string $path)
Get the name of the migration.
at line 551
void
path(string $path)
Register a custom migration path.
at line 561
array
paths()
Get all of the custom migration paths.
at line 574
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 582
string|null
getConnection()
Get the default connection name.
at line 605
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.
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 644
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 the stored migrator connection on entry, then restores them directly in finally without routing back through setConnection() — otherwise the restoration would apply migrations_connection to the saved alias and leave the wrong default in place.
at line 676
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 698
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 721
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 729
protected Grammar
getSchemaGrammar(Connection $connection)
Get the schema grammar out of a migration connection.
at line 743
MigrationRepositoryInterface
getRepository()
Get the migration repository instance.
at line 751
bool
repositoryExists()
Determine if the migration repository exists.
at line 759
bool
hasRunAnyMigrations()
Determine if any migrations have been run.
at line 767
void
deleteRepository()
Delete the migration repository data store.
at line 775
Filesystem
getFilesystem()
Get the file system instance.
at line 783
Migrator
setOutput(OutputStyle $output)
Set the output implementation that should be used by the console.
at line 795
protected void
write(string $component, mixed ...$arguments)
Write to the console's output.
at line 816
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 828
static void
flushState()
Flush all static state.