Skip to content

Commit 2096aaf

Browse files
pass options to migration events (#54151)
* pass options to migration events * Update MigrationsEvent.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent b7ae0c2 commit 2096aaf

File tree

3 files changed

+69
-5
lines changed

3 files changed

+69
-5
lines changed

src/Illuminate/Database/Events/MigrationsEvent.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,23 @@ abstract class MigrationsEvent implements MigrationEventContract
1313
*/
1414
public $method;
1515

16+
/**
17+
* The options provided when the migration method was invoked.
18+
*
19+
* @var array<string, mixed>
20+
*/
21+
public $options;
22+
1623
/**
1724
* Create a new event instance.
1825
*
1926
* @param string $method
27+
* @param array<string, mixed> $options
2028
* @return void
2129
*/
22-
public function __construct($method)
30+
public function __construct($method, array $options = [])
2331
{
2432
$this->method = $method;
33+
$this->options = $options;
2534
}
2635
}

src/Illuminate/Database/Migrations/Migrator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public function runPending(array $migrations, array $options = [])
169169

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

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

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

@@ -184,7 +184,7 @@ public function runPending(array $migrations, array $options = [])
184184
}
185185
}
186186

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

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

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

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

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

@@ -302,7 +302,7 @@ protected function rollbackMigrations(array $migrations, $paths, array $options)
302302
);
303303
}
304304

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

307307
return $rolledBack;
308308
}

tests/Integration/Database/MigratorEventsTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,61 @@ public function testMigrationEventsAreFired()
3434
Event::assertDispatched(MigrationEnded::class, 2);
3535
}
3636

37+
public function testMigrationEventsContainTheOptionsAndPretendFalse()
38+
{
39+
Event::fake();
40+
41+
$this->artisan('migrate', $this->migrateOptions());
42+
$this->artisan('migrate:rollback', $this->migrateOptions());
43+
44+
Event::assertDispatched(MigrationsStarted::class, function ($event) {
45+
return $event->method === 'up'
46+
&& is_array($event->options)
47+
&& isset($event->options['pretend'])
48+
&& $event->options['pretend'] === false;
49+
});
50+
Event::assertDispatched(MigrationsStarted::class, function ($event) {
51+
return $event->method === 'down'
52+
&& is_array($event->options)
53+
&& isset($event->options['pretend'])
54+
&& $event->options['pretend'] === false;
55+
});
56+
Event::assertDispatched(MigrationsEnded::class, function ($event) {
57+
return $event->method === 'up'
58+
&& is_array($event->options)
59+
&& isset($event->options['pretend'])
60+
&& $event->options['pretend'] === false;
61+
});
62+
Event::assertDispatched(MigrationsEnded::class, function ($event) {
63+
return $event->method === 'down'
64+
&& is_array($event->options)
65+
&& isset($event->options['pretend'])
66+
&& $event->options['pretend'] === false;
67+
});
68+
}
69+
70+
public function testMigrationEventsContainTheOptionsAndPretendTrue()
71+
{
72+
Event::fake();
73+
74+
$this->artisan('migrate', $this->migrateOptions() + ['--pretend' => true]);
75+
$this->artisan('migrate:rollback', $this->migrateOptions()); // doesn't support pretend
76+
77+
Event::assertDispatched(MigrationsStarted::class, function ($event) {
78+
return $event->method === 'up'
79+
&& is_array($event->options)
80+
&& isset($event->options['pretend'])
81+
&& $event->options['pretend'] === true;
82+
});
83+
84+
Event::assertDispatched(MigrationsEnded::class, function ($event) {
85+
return $event->method === 'up'
86+
&& is_array($event->options)
87+
&& isset($event->options['pretend'])
88+
&& $event->options['pretend'] === true;
89+
});
90+
}
91+
3792
public function testMigrationEventsContainTheMigrationAndMethod()
3893
{
3994
Event::fake();

0 commit comments

Comments
 (0)