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

[5.8] Migration Events #28342

Merged
merged 11 commits into from
May 2, 2019
8 changes: 8 additions & 0 deletions src/Illuminate/Contracts/Database/Events/Migration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Illuminate\Contracts\Database\Events;

interface Migration
driesvints marked this conversation as resolved.
Show resolved Hide resolved
{
//
}
8 changes: 8 additions & 0 deletions src/Illuminate/Database/Events/MigrationEnded.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Illuminate\Database\Events;

class MigrationEnded extends MigrationEvent
{
//
}
36 changes: 36 additions & 0 deletions src/Illuminate/Database/Events/MigrationEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Illuminate\Database\Events;

use Illuminate\Database\Migrations\Migration;
use Illuminate\Contracts\Database\Events\Migration as MigrationContract;

abstract class MigrationEvent implements MigrationContract
{
/**
* An instance of the migration.
*
* @var \Illuminate\Database\Migrations\Migration
*/
public $migration;

/**
* The direction of the migration.
*
* @var string
*/
public $direction;

/**
* Create a new event instance.
*
* @param \Illuminate\Database\Migrations\Migration $migration
* @param string $direction
* @return void
*/
public function __construct(Migration $migration, $direction)
{
$this->migration = $migration;
$this->direction = $direction;
}
}
8 changes: 8 additions & 0 deletions src/Illuminate/Database/Events/MigrationStarted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Illuminate\Database\Events;

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

namespace Illuminate\Database\Events;

use Illuminate\Contracts\Database\Events\Migration as MigrationContract;

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

namespace Illuminate\Database\Events;

use Illuminate\Contracts\Database\Events\Migration as MigrationContract;

class MigrationsStarted implements MigrationContract
{
//
}
2 changes: 1 addition & 1 deletion src/Illuminate/Database/MigrationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected function registerMigrator()
$this->app->singleton('migrator', function ($app) {
$repository = $app['migration.repository'];

return new Migrator($repository, $app['db'], $app['files']);
return new Migrator($repository, $app['db'], $app['files'], $app['events']);
});
}

Expand Down
42 changes: 41 additions & 1 deletion src/Illuminate/Database/Migrations/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,22 @@
use Illuminate\Support\Collection;
use Illuminate\Console\OutputStyle;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Events\MigrationEnded;
use Illuminate\Database\Events\MigrationsEnded;
use Illuminate\Database\Events\MigrationStarted;
use Illuminate\Database\Events\MigrationsStarted;
use Illuminate\Database\ConnectionResolverInterface as Resolver;

class Migrator
{
/**
* The event dispatcher instance.
*
* @var \Illuminate\Contracts\Events\Dispatcher
*/
protected $events;

/**
* The migration repository implementation.
*
Expand Down Expand Up @@ -59,13 +71,16 @@ class Migrator
* @param \Illuminate\Database\Migrations\MigrationRepositoryInterface $repository
* @param \Illuminate\Database\ConnectionResolverInterface $resolver
* @param \Illuminate\Filesystem\Filesystem $files
* @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
* @return void
*/
public function __construct(MigrationRepositoryInterface $repository,
Resolver $resolver,
Filesystem $files)
Filesystem $files,
Dispatcher $dispatcher = null)
{
$this->files = $files;
$this->events = $dispatcher;
$this->resolver = $resolver;
$this->repository = $repository;
}
Expand Down Expand Up @@ -140,6 +155,8 @@ public function runPending(array $migrations, array $options = [])

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

$this->fireMigrationEvent(new MigrationsStarted);

// 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
// that the migration was run so we don't repeat it next time we execute.
Expand All @@ -150,6 +167,8 @@ public function runPending(array $migrations, array $options = [])
$batch++;
}
}

$this->fireMigrationEvent(new MigrationsEnded);
}

/**
Expand Down Expand Up @@ -239,6 +258,8 @@ protected function rollbackMigrations(array $migrations, $paths, array $options)

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

$this->fireMigrationEvent(new MigrationsStarted);

// 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
// repository already returns these migration's names in reverse order.
Expand All @@ -259,6 +280,8 @@ protected function rollbackMigrations(array $migrations, $paths, array $options)
);
}

$this->fireMigrationEvent(new MigrationsEnded);

return $rolledBack;
}

Expand Down Expand Up @@ -357,7 +380,11 @@ protected function runMigration($migration, $method)

$callback = function () use ($migration, $method) {
if (method_exists($migration, $method)) {
$this->fireMigrationEvent(new MigrationStarted($migration, $method));

$migration->{$method}();

$this->fireMigrationEvent(new MigrationEnded($migration, $method));
}
};

Expand Down Expand Up @@ -591,4 +618,17 @@ protected function note($message)
$this->output->writeln($message);
}
}

/**
* Fire the given event for the migration.
*
* @param \Illuminate\Contracts\Database\Events\Migration $event
* @return void
*/
public function fireMigrationEvent($event)
{
if ($this->events) {
$this->events->dispatch($event);
}
}
driesvints marked this conversation as resolved.
Show resolved Hide resolved
}
28 changes: 28 additions & 0 deletions tests/Integration/Database/MigrateWithRealpathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

namespace Illuminate\Tests\Integration\Database;

use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Events\MigrationEnded;
use Illuminate\Database\Events\MigrationsEnded;
use Illuminate\Database\Events\MigrationStarted;
use Illuminate\Database\Events\MigrationsStarted;

class MigrateWithRealpathTest extends DatabaseTestCase
{
Expand Down Expand Up @@ -35,4 +40,27 @@ public function test_migrations_has_the_migrated_table()
'batch' => 1,
]);
}

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

Event::listen(MigrationsStarted::class, function ($event) {
return $this->assertInstanceOf(MigrationsStarted::class, $event);
});

Event::listen(MigrationsEnded::class, function ($event) {
return $this->assertInstanceOf(MigrationsEnded::class, $event);
});

Event::listen(MigrationStarted::class, function ($event) {
return $this->assertInstanceOf(MigrationStarted::class, $event);
});

Event::listen(MigrationEnded::class, function ($event) {
return $this->assertInstanceOf(MigrationEnded::class, $event);
});

$this->artisan('migrate');
}
}