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

[8.x] Add DatabaseRefreshed event to be emitted after database refreshed #34952

Merged
merged 3 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Illuminate/Database/Console/Migrations/FreshCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Illuminate\Console\Command;
use Illuminate\Console\ConfirmableTrait;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Events\DatabaseRefreshed;
use Symfony\Component\Console\Input\InputOption;

class FreshCommand extends Command
Expand Down Expand Up @@ -57,6 +59,10 @@ public function handle()
$this->runSeeder($database);
}

$this->laravel[Dispatcher::class]->dispatch(
new DatabaseRefreshed()
);

Comment on lines +62 to +65
Copy link
Contributor

@carestad carestad Nov 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to move this so it dispatches before the seeders are being run? Or have a separate event or something for that? It will run at different times now depending on whether you run migrate:fresh --seed or migrate:fresh && db:seed

return 0;
}

Expand Down
6 changes: 6 additions & 0 deletions src/Illuminate/Database/Console/Migrations/RefreshCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Illuminate\Console\Command;
use Illuminate\Console\ConfirmableTrait;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Events\DatabaseRefreshed;
use Symfony\Component\Console\Input\InputOption;

class RefreshCommand extends Command
Expand Down Expand Up @@ -67,6 +69,10 @@ public function handle()
$this->runSeeder($database);
}

$this->laravel[Dispatcher::class]->dispatch(
new DatabaseRefreshed()
);

return 0;
}

Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Database/Events/DatabaseRefreshed.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\MigrationEvent as MigrationEventContract;

class DatabaseRefreshed implements MigrationEventContract
{
//
}
6 changes: 6 additions & 0 deletions tests/Database/DatabaseMigrationRefreshCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Illuminate\Tests\Database;

use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Console\Migrations\MigrateCommand;
use Illuminate\Database\Console\Migrations\RefreshCommand;
use Illuminate\Database\Console\Migrations\ResetCommand;
use Illuminate\Database\Console\Migrations\RollbackCommand;
use Illuminate\Database\Events\DatabaseRefreshed;
use Illuminate\Foundation\Application;
use Mockery as m;
use PHPUnit\Framework\TestCase;
Expand All @@ -25,6 +27,7 @@ public function testRefreshCommandCallsCommandsWithProperArguments()
$command = new RefreshCommand();

$app = new ApplicationDatabaseRefreshStub(['path.database' => __DIR__]);
$dispatcher = $app->instance(Dispatcher::class, $events = m::mock());
$console = m::mock(ConsoleApplication::class)->makePartial();
$console->__construct();
$command->setLaravel($app);
Expand All @@ -35,6 +38,7 @@ public function testRefreshCommandCallsCommandsWithProperArguments()

$console->shouldReceive('find')->with('migrate:reset')->andReturn($resetCommand);
$console->shouldReceive('find')->with('migrate')->andReturn($migrateCommand);
$dispatcher->shouldReceive('dispatch')->once()->with(m::type(DatabaseRefreshed::class));

$quote = DIRECTORY_SEPARATOR == '\\' ? '"' : "'";
$resetCommand->shouldReceive('run')->with(new InputMatcher("--force=1 {$quote}migrate:reset{$quote}"), m::any());
Expand All @@ -48,6 +52,7 @@ public function testRefreshCommandCallsCommandsWithStep()
$command = new RefreshCommand();

$app = new ApplicationDatabaseRefreshStub(['path.database' => __DIR__]);
$dispatcher = $app->instance(Dispatcher::class, $events = m::mock());
$console = m::mock(ConsoleApplication::class)->makePartial();
$console->__construct();
$command->setLaravel($app);
Expand All @@ -58,6 +63,7 @@ public function testRefreshCommandCallsCommandsWithStep()

$console->shouldReceive('find')->with('migrate:rollback')->andReturn($rollbackCommand);
$console->shouldReceive('find')->with('migrate')->andReturn($migrateCommand);
$dispatcher->shouldReceive('dispatch')->once()->with(m::type(DatabaseRefreshed::class));

$quote = DIRECTORY_SEPARATOR == '\\' ? '"' : "'";
$rollbackCommand->shouldReceive('run')->with(new InputMatcher("--step=2 --force=1 {$quote}migrate:rollback{$quote}"), m::any());
Expand Down