Skip to content

Commit

Permalink
[8.x] Add method to MigrationsStarted/MigrationEnded events (#40334)
Browse files Browse the repository at this point in the history
* Add method (up/down) to MigrationsStarted/Ended events

* add missing tests

* Update MigrationsEvent.php

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
kohenkatz and taylorotwell authored Jan 11, 2022
1 parent 20d9fff commit d320012
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 10 deletions.
4 changes: 1 addition & 3 deletions src/Illuminate/Database/Events/MigrationsEnded.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace Illuminate\Database\Events;

use Illuminate\Contracts\Database\Events\MigrationEvent as MigrationEventContract;

class MigrationsEnded implements MigrationEventContract
class MigrationsEnded extends MigrationsEvent
{
//
}
26 changes: 26 additions & 0 deletions src/Illuminate/Database/Events/MigrationsEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Illuminate\Database\Events;

use Illuminate\Contracts\Database\Events\MigrationEvent as MigrationEventContract;

abstract class MigrationsEvent implements MigrationEventContract
{
/**
* The migration method that was invoked.
*
* @var string
*/
public $method;

/**
* Create a new event instance.
*
* @param string $method
* @return void
*/
public function __construct($method)
{
$this->method = $method;
}
}
4 changes: 1 addition & 3 deletions src/Illuminate/Database/Events/MigrationsStarted.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace Illuminate\Database\Events;

use Illuminate\Contracts\Database\Events\MigrationEvent as MigrationEventContract;

class MigrationsStarted implements MigrationEventContract
class MigrationsStarted extends MigrationsEvent
{
//
}
8 changes: 4 additions & 4 deletions src/Illuminate/Database/Migrations/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public function runPending(array $migrations, array $options = [])

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

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

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

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

/**
Expand Down Expand Up @@ -265,7 +265,7 @@ protected function rollbackMigrations(array $migrations, $paths, array $options)

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

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

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

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

return $rolledBack;
}
Expand Down
13 changes: 13 additions & 0 deletions tests/Integration/Database/MigratorEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ public function testMigrationEventsContainTheMigrationAndMethod()
$this->artisan('migrate', $this->migrateOptions());
$this->artisan('migrate:rollback', $this->migrateOptions());

Event::assertDispatched(MigrationsStarted::class, function ($event) {
return $event->method === 'up';
});
Event::assertDispatched(MigrationsStarted::class, function ($event) {
return $event->method === 'down';
});
Event::assertDispatched(MigrationsEnded::class, function ($event) {
return $event->method === 'up';
});
Event::assertDispatched(MigrationsEnded::class, function ($event) {
return $event->method === 'down';
});

Event::assertDispatched(MigrationStarted::class, function ($event) {
return $event->method === 'up' && $event->migration instanceof Migration;
});
Expand Down

0 comments on commit d320012

Please sign in to comment.