Skip to content

Commit

Permalink
pass options to migration events
Browse files Browse the repository at this point in the history
  • Loading branch information
willpower232 committed Jan 10, 2025
1 parent 4b9aeae commit 47e33a2
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
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

0 comments on commit 47e33a2

Please sign in to comment.