Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pass options to migration events #54151

Open
wants to merge 1 commit into
base: 11.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/Illuminate/Database/Events/MigrationsEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,23 @@ abstract class MigrationsEvent implements MigrationEventContract
*/
public $method;

/**
* The options provided when the migration method was invoked.
*
* @var array<string, mixed>
*/
public $options;

/**
* Create a new event instance.
*
* @param string $method
* @param array<string, mixed> $options
* @return void
*/
public function __construct($method)
public function __construct($method, $options)
{
$this->method = $method;
$this->options = $options;
}
}
8 changes: 4 additions & 4 deletions src/Illuminate/Database/Migrations/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public function runPending(array $migrations, array $options = [])

$step = $options['step'] ?? false;

$this->fireMigrationEvent(new MigrationsStarted('up'));
$this->fireMigrationEvent(new MigrationsStarted('up', $options));

$this->write(Info::class, 'Running migrations.');

Expand All @@ -184,7 +184,7 @@ public function runPending(array $migrations, array $options = [])
}
}

$this->fireMigrationEvent(new MigrationsEnded('up'));
$this->fireMigrationEvent(new MigrationsEnded('up', $options));

$this->output?->writeln('');
}
Expand Down Expand Up @@ -278,7 +278,7 @@ protected function rollbackMigrations(array $migrations, $paths, array $options)

$this->requireFiles($files = $this->getMigrationFiles($paths));

$this->fireMigrationEvent(new MigrationsStarted('down'));
$this->fireMigrationEvent(new MigrationsStarted('down', $options));

$this->write(Info::class, 'Rolling back migrations.');

Expand All @@ -302,7 +302,7 @@ protected function rollbackMigrations(array $migrations, $paths, array $options)
);
}

$this->fireMigrationEvent(new MigrationsEnded('down'));
$this->fireMigrationEvent(new MigrationsEnded('down', $options));

return $rolledBack;
}
Expand Down
55 changes: 55 additions & 0 deletions tests/Integration/Database/MigratorEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,61 @@ public function testMigrationEventsAreFired()
Event::assertDispatched(MigrationEnded::class, 2);
}

public function testMigrationEventsContainTheOptionsAndPretendFalse()
{
Event::fake();

$this->artisan('migrate', $this->migrateOptions());
$this->artisan('migrate:rollback', $this->migrateOptions());

Event::assertDispatched(MigrationsStarted::class, function ($event) {
return $event->method === 'up'
&& is_array($event->options)
&& isset($event->options['pretend'])
&& $event->options['pretend'] === false;
});
Event::assertDispatched(MigrationsStarted::class, function ($event) {
return $event->method === 'down'
&& is_array($event->options)
&& isset($event->options['pretend'])
&& $event->options['pretend'] === false;
});
Event::assertDispatched(MigrationsEnded::class, function ($event) {
return $event->method === 'up'
&& is_array($event->options)
&& isset($event->options['pretend'])
&& $event->options['pretend'] === false;
});
Event::assertDispatched(MigrationsEnded::class, function ($event) {
return $event->method === 'down'
&& is_array($event->options)
&& isset($event->options['pretend'])
&& $event->options['pretend'] === false;
});
}

public function testMigrationEventsContainTheOptionsAndPretendTrue()
{
Event::fake();

$this->artisan('migrate', $this->migrateOptions() + ['--pretend' => true]);
$this->artisan('migrate:rollback', $this->migrateOptions()); // doesn't support pretend

Event::assertDispatched(MigrationsStarted::class, function ($event) {
return $event->method === 'up'
&& is_array($event->options)
&& isset($event->options['pretend'])
&& $event->options['pretend'] === true;
});

Event::assertDispatched(MigrationsEnded::class, function ($event) {
return $event->method === 'up'
&& is_array($event->options)
&& isset($event->options['pretend'])
&& $event->options['pretend'] === true;
});
}

public function testMigrationEventsContainTheMigrationAndMethod()
{
Event::fake();
Expand Down
Loading