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

__construct(MigrationRepositoryInterface $repository, ConnectionResolverInterface $resolver, Filesystem $files)

Create a new migrator instance.

array
run(array|string $paths = [], array $options = [])

Run the pending migrations at a given path.

array
pendingMigrations(array $files, array $ran)

Get the migration files that have not yet run.

array
migrationsToSkip()

Get list of pending migrations to skip.

void
runPending(array $migrations, array $options = [])

Run an array of migrations.

void
runUp(string $file, int $batch, bool $pretend)

Run "up" a migration instance.

array
rollback(array|string $paths = [], array $options = [])

Rollback the last migration operation.

array
getMigrationsForRollback(array $options)

Get the migrations for a rollback operation.

array
rollbackMigrations(array $migrations, array|string $paths, array $options)

Rollback the given migrations.

array
reset(array|string $paths = [], bool $pretend = false)

Rolls all of the currently applied migrations back.

array
resetMigrations(array $migrations, array $paths, bool $pretend = false)

Reset the given migrations.

void
runDown(string $file, object $migration, bool $pretend)

Run "down" a migration instance.

void
runMigration(object $migration, string $method, string|null $name = null)

Run a migration inside a transaction if the database supports it.

void
pretendToRun(object $migration, string $method)

Pretend to run the migrations.

array
getQueries(object $migration, string $method)

Get all of the queries that would be run for a migration.

void
runMethod(Connection $connection, object $migration, string $method)

Run a migration method on the given connection.

object
resolve(string $file)

Resolve a migration instance from a file.

object
resolvePath(string $path)

Resolve a migration instance from a migration path.

string
getMigrationClass(string $migrationName)

Generate a migration class name based on the migration file name.

array
getMigrationFiles(array|string $paths)

Get all of the migration files in a given path.

void
requireFiles(array $files)

Require in all the migration files in a given path.

string
getMigrationName(string $path)

Get the name of the migration.

void
path(string $path)

Register a custom migration path.

array
paths()

Get all of the custom migration paths.

static void
withoutMigrations(array $migrations)

Set the pending migrations to skip.

string|null
getConnection()

Get the default connection name.

static string|null
resolveMigrationConnectionName(string|null $name)

Resolve a connection name through the "migrations_connection" config key.

mixed
usingConnection(string|null $name, callable $callback)

Execute the given callback using the given connection as the default connection.

void
setConnection(string|null $name)

Set the default connection name.

resolveConnection(string|null $connection)

Resolve the database connection instance.

static void
resolveConnectionsUsing(Closure $callback)

Set a connection resolver callback.

getSchemaGrammar(Connection $connection)

Get the schema grammar out of a migration connection.

getRepository()

Get the migration repository instance.

bool
repositoryExists()

Determine if the migration repository exists.

bool
hasRunAnyMigrations()

Determine if any migrations have been run.

void
deleteRepository()

Delete the migration repository data store.

getFilesystem()

Get the file system instance.

setOutput(OutputStyle $output)

Set the output implementation that should be used by the console.

void
write(string $component, mixed ...$arguments)

Write to the console's output.

void
fireMigrationEvent(MigrationEvent $event)

Fire the given event for the migration.

static void
flushState()

Flush all static state.

Details

at line 74
__construct(MigrationRepositoryInterface $repository, ConnectionResolverInterface $resolver, Filesystem $files)

Create a new migrator instance.

Parameters

MigrationRepositoryInterface $repository
ConnectionResolverInterface $resolver
Filesystem $files

at line 88
array run(array|string $paths = [], array $options = [])

Run the pending migrations at a given path.

Parameters

array|string $paths
array $options

Return Value

array

at line 115
protected array pendingMigrations(array $files, array $ran)

Get the migration files that have not yet run.

Parameters

array $files
array $ran

Return Value

array

at line 133
protected array migrationsToSkip()

Get list of pending migrations to skip.

Return Value

array

at line 146
void runPending(array $migrations, array $options = [])

Run an array of migrations.

Parameters

array $migrations
array $options

Return Value

void

at line 191
protected void runUp(string $file, int $batch, bool $pretend)

Run "up" a migration instance.

Parameters

string $file
int $batch
bool $pretend

Return Value

void

at line 231
array rollback(array|string $paths = [], array $options = [])

Rollback the last migration operation.

Parameters

array|string $paths
array $options

Return Value

array

at line 256
protected array getMigrationsForRollback(array $options)

Get the migrations for a rollback operation.

Parameters

array $options

Return Value

array

at line 276
protected array rollbackMigrations(array $migrations, array|string $paths, array $options)

Rollback the given migrations.

Parameters

array $migrations
array|string $paths
array $options

Return Value

array

at line 317
array reset(array|string $paths = [], bool $pretend = false)

Rolls all of the currently applied migrations back.

Parameters

array|string $paths
bool $pretend

Return Value

array

at line 341
protected array resetMigrations(array $migrations, array $paths, bool $pretend = false)

Reset the given migrations.

Parameters

array $migrations
array $paths
bool $pretend

Return Value

array

at line 358
protected void runDown(string $file, object $migration, bool $pretend)

Run "down" a migration instance.

Parameters

string $file
object $migration
bool $pretend

Return Value

void

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.

Parameters

object $migration
string $method
string|null $name

Return Value

void

at line 409
protected void pretendToRun(object $migration, string $method)

Pretend to run the migrations.

Parameters

object $migration
string $method

Return Value

void

at line 430
protected array getQueries(object $migration, string $method)

Get all of the queries that would be run for a migration.

Parameters

object $migration
string $method

Return Value

array

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.

Parameters

Connection $connection
object $migration
string $method

Return Value

void

at line 475
object resolve(string $file)

Resolve a migration instance from a file.

Parameters

string $file

Return Value

object

at line 485
protected object resolvePath(string $path)

Resolve a migration instance from a migration path.

Parameters

string $path

Return Value

object

at line 507
protected string getMigrationClass(string $migrationName)

Generate a migration class name based on the migration file name.

Parameters

string $migrationName

Return Value

string

at line 517
array getMigrationFiles(array|string $paths)

Get all of the migration files in a given path.

Parameters

array|string $paths

Return Value

array

at line 533
void requireFiles(array $files)

Require in all the migration files in a given path.

Parameters

array $files

Return Value

void

at line 543
string getMigrationName(string $path)

Get the name of the migration.

Parameters

string $path

Return Value

string

at line 551
void path(string $path)

Register a custom migration path.

Parameters

string $path

Return Value

void

at line 561
array paths()

Get all of the custom migration paths.

Return Value

array

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.

Parameters

array $migrations

Return Value

void

at line 582
string|null getConnection()

Get the default connection name.

Return Value

string|null

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.

Parameters

string|null $name

Return Value

string|null

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.

Parameters

string|null $name
callable $callback

Return Value

mixed

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.

Parameters

string|null $name

Return Value

void

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.

Parameters

string|null $connection

Return Value

Connection

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.

Parameters

Closure $callback

Return Value

void

at line 729
protected Grammar getSchemaGrammar(Connection $connection)

Get the schema grammar out of a migration connection.

Parameters

Connection $connection

Return Value

Grammar

at line 743
MigrationRepositoryInterface getRepository()

Get the migration repository instance.

at line 751
bool repositoryExists()

Determine if the migration repository exists.

Return Value

bool

at line 759
bool hasRunAnyMigrations()

Determine if any migrations have been run.

Return Value

bool

at line 767
void deleteRepository()

Delete the migration repository data store.

Return Value

void

at line 775
Filesystem getFilesystem()

Get the file system instance.

Return Value

Filesystem

at line 783
Migrator setOutput(OutputStyle $output)

Set the output implementation that should be used by the console.

Parameters

OutputStyle $output

Return Value

Migrator

at line 795
protected void write(string $component, mixed ...$arguments)

Write to the console's output.

Parameters

string $component
mixed ...$arguments

Return Value

void

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).

Parameters

MigrationEvent $event

Return Value

void

at line 828
static void flushState()

Flush all static state.

Return Value

void