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

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

array
getMigrationConnections(array|string $paths, string|null $defaultConnection = null)

Get the distinct connections declared by the migrations at the given paths.

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.

bool
hasMigrationEventListeners(string $event)

Determine whether the given migration event has listeners.

void
fireMigrationEvent(MigrationEvent $event)

Fire the given event for the migration.

static void
flushState()

Flush all static state.

Details

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

Create a new migrator instance.

Parameters

MigrationRepositoryInterface $repository
ConnectionResolverInterface $resolver
Filesystem $files

at line 91
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 118
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 136
protected array migrationsToSkip()

Get list of pending migrations to skip.

Return Value

array

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

Run an array of migrations.

Parameters

array $migrations
array $options

Return Value

void

at line 200
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 242
array rollback(array|string $paths = [], array $options = [])

Rollback the last migration operation.

Parameters

array|string $paths
array $options

Return Value

array

at line 270
protected array getMigrationsForRollback(array $options)

Get the migrations for a rollback operation.

Parameters

array $options

Return Value

array

at line 291
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 336
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 360
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 377
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 403
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 432
protected void pretendToRun(object $migration, string $method)

Pretend to run the migrations.

Parameters

object $migration
string $method

Return Value

void

at line 453
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 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.

Parameters

Connection $connection
object $migration
string $method

Return Value

void

at line 498
object resolve(string $file)

Resolve a migration instance from a file.

Parameters

string $file

Return Value

object

at line 508
protected object resolvePath(string $path)

Resolve a migration instance from a migration path.

Parameters

string $path

Return Value

object

at line 532
array getMigrationConnections(array|string $paths, string|null $defaultConnection = null)

Get the distinct connections declared by the migrations at the given paths.

Parameters

array|string $paths
string|null $defaultConnection

Return Value

array

at line 563
protected string getMigrationClass(string $migrationName)

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

Parameters

string $migrationName

Return Value

string

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

Get all of the migration files in a given path.

Parameters

array|string $paths

Return Value

array

at line 589
void requireFiles(array $files)

Require in all the migration files in a given path.

Parameters

array $files

Return Value

void

at line 599
string getMigrationName(string $path)

Get the name of the migration.

Parameters

string $path

Return Value

string

at line 607
void path(string $path)

Register a custom migration path.

Parameters

string $path

Return Value

void

at line 617
array paths()

Get all of the custom migration paths.

Return Value

array

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.

Parameters

array $migrations

Return Value

void

at line 638
string|null getConnection()

Get the default connection name.

Return Value

string|null

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.

Parameters

string|null $name

Return Value

string|null

Exceptions

InvalidArgumentException

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.

Parameters

string|null $name
callable $callback

Return Value

mixed

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.

Parameters

string|null $name

Return Value

void

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.

Parameters

string|null $connection

Return Value

Connection

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.

Parameters

Closure $callback

Return Value

void

at line 817
protected Grammar getSchemaGrammar(Connection $connection)

Get the schema grammar out of a migration connection.

Parameters

Connection $connection

Return Value

Grammar

at line 831
MigrationRepositoryInterface getRepository()

Get the migration repository instance.

at line 839
bool repositoryExists()

Determine if the migration repository exists.

Return Value

bool

at line 847
bool hasRunAnyMigrations()

Determine if any migrations have been run.

Return Value

bool

at line 855
void deleteRepository()

Delete the migration repository data store.

Return Value

void

at line 863
Filesystem getFilesystem()

Get the file system instance.

Return Value

Filesystem

at line 871
Migrator setOutput(OutputStyle $output)

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

Parameters

OutputStyle $output

Return Value

Migrator

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

Write to the console's output.

Parameters

string $component
mixed ...$arguments

Return Value

void

at line 900
protected bool hasMigrationEventListeners(string $event)

Determine whether the given migration event has listeners.

Parameters

string $event

Return Value

bool

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

Parameters

MigrationEvent $event

Return Value

void

at line 927
static void flushState()

Flush all static state.

Return Value

void