Skip to content

Commit d320012

Browse files
[8.x] Add method to MigrationsStarted/MigrationEnded events (#40334)
* Add method (up/down) to MigrationsStarted/Ended events * add missing tests * Update MigrationsEvent.php Co-authored-by: Taylor Otwell <[email protected]>
1 parent 20d9fff commit d320012

File tree

5 files changed

+45
-10
lines changed

5 files changed

+45
-10
lines changed

src/Illuminate/Database/Events/MigrationsEnded.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace Illuminate\Database\Events;
44

5-
use Illuminate\Contracts\Database\Events\MigrationEvent as MigrationEventContract;
6-
7-
class MigrationsEnded implements MigrationEventContract
5+
class MigrationsEnded extends MigrationsEvent
86
{
97
//
108
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Illuminate\Database\Events;
4+
5+
use Illuminate\Contracts\Database\Events\MigrationEvent as MigrationEventContract;
6+
7+
abstract class MigrationsEvent implements MigrationEventContract
8+
{
9+
/**
10+
* The migration method that was invoked.
11+
*
12+
* @var string
13+
*/
14+
public $method;
15+
16+
/**
17+
* Create a new event instance.
18+
*
19+
* @param string $method
20+
* @return void
21+
*/
22+
public function __construct($method)
23+
{
24+
$this->method = $method;
25+
}
26+
}

src/Illuminate/Database/Events/MigrationsStarted.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace Illuminate\Database\Events;
44

5-
use Illuminate\Contracts\Database\Events\MigrationEvent as MigrationEventContract;
6-
7-
class MigrationsStarted implements MigrationEventContract
5+
class MigrationsStarted extends MigrationsEvent
86
{
97
//
108
}

src/Illuminate/Database/Migrations/Migrator.php

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

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

161-
$this->fireMigrationEvent(new MigrationsStarted);
161+
$this->fireMigrationEvent(new MigrationsStarted('up'));
162162

163163
// Once we have the array of migrations, we will spin through them and run the
164164
// migrations "up" so the changes are made to the databases. We'll then log
@@ -171,7 +171,7 @@ public function runPending(array $migrations, array $options = [])
171171
}
172172
}
173173

174-
$this->fireMigrationEvent(new MigrationsEnded);
174+
$this->fireMigrationEvent(new MigrationsEnded('up'));
175175
}
176176

177177
/**
@@ -265,7 +265,7 @@ protected function rollbackMigrations(array $migrations, $paths, array $options)
265265

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

268-
$this->fireMigrationEvent(new MigrationsStarted);
268+
$this->fireMigrationEvent(new MigrationsStarted('down'));
269269

270270
// Next we will run through all of the migrations and call the "down" method
271271
// which will reverse each migration in order. This getLast method on the
@@ -287,7 +287,7 @@ protected function rollbackMigrations(array $migrations, $paths, array $options)
287287
);
288288
}
289289

290-
$this->fireMigrationEvent(new MigrationsEnded);
290+
$this->fireMigrationEvent(new MigrationsEnded('down'));
291291

292292
return $rolledBack;
293293
}

tests/Integration/Database/MigratorEventsTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ public function testMigrationEventsContainTheMigrationAndMethod()
4141
$this->artisan('migrate', $this->migrateOptions());
4242
$this->artisan('migrate:rollback', $this->migrateOptions());
4343

44+
Event::assertDispatched(MigrationsStarted::class, function ($event) {
45+
return $event->method === 'up';
46+
});
47+
Event::assertDispatched(MigrationsStarted::class, function ($event) {
48+
return $event->method === 'down';
49+
});
50+
Event::assertDispatched(MigrationsEnded::class, function ($event) {
51+
return $event->method === 'up';
52+
});
53+
Event::assertDispatched(MigrationsEnded::class, function ($event) {
54+
return $event->method === 'down';
55+
});
56+
4457
Event::assertDispatched(MigrationStarted::class, function ($event) {
4558
return $event->method === 'up' && $event->migration instanceof Migration;
4659
});

0 commit comments

Comments
 (0)