Skip to content

[5.4] Add more information to migrations #18220

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

Closed
wants to merge 2 commits into from
Closed
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
13 changes: 4 additions & 9 deletions src/Illuminate/Database/Console/Migrations/MigrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,12 @@ public function fire()
// Next, we will check to see if a path option has been defined. If it has
// we will use the path relative to the root of this installation folder
// so that migrations may be run for any path within the applications.

Copy link
Contributor

@fernandobandeira fernandobandeira Mar 6, 2017

Choose a reason for hiding this comment

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

IMO this line should be removed...

$this->migrator->run($this->getMigrationPaths(), [
'pretend' => $this->option('pretend'),
'step' => $this->option('step'),
]);

// Once the migrator has run we will grab the note output and send it out to
// the console screen, since the migrator itself functions without having
// any instances of the OutputInterface contract passed into the class.
foreach ($this->migrator->getNotes() as $note) {
$this->output->writeln($note);
}
'step' => $this->option('step'),
], $this->getOutput());


// Finally, if the "seed" option has been given, we will re-run the database
// seed task to re-populate the database, which is convenient when adding
Expand Down
34 changes: 24 additions & 10 deletions src/Illuminate/Database/Migrations/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Illuminate\Database\Migrations;

use Carbon\Carbon;
use Illuminate\Database\ConnectionResolverInterface as Resolver;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Collection;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Database\ConnectionResolverInterface as Resolver;
use Illuminate\Support\Str;
use Symfony\Component\Console\Output\OutputInterface;

class Migrator
{
Expand Down Expand Up @@ -72,11 +74,12 @@ public function __construct(MigrationRepositoryInterface $repository,
/**
* Run the pending migrations at a given path.
*
* @param array|string $paths
* @param array $options
* @param array|string $paths
* @param array $options
* @param OutputInterface $output
* @return array
*/
public function run($paths = [], array $options = [])
public function run($paths = [], array $options = [], OutputInterface $output = NULL)
{
$this->notes = [];

Expand All @@ -92,7 +95,7 @@ public function run($paths = [], array $options = [])
// Once we have all these migrations that are outstanding we are ready to run
// we will go ahead and run them "up". This will execute each migration as
// an operation against a database. Then we'll return this list of them.
$this->runPending($migrations, $options);
$this->runPending($migrations, $options, $output);

return $migrations;
}
Expand All @@ -115,11 +118,12 @@ protected function pendingMigrations($files, $ran)
/**
* Run an array of migrations.
*
* @param array $migrations
* @param array $options
* @param array $migrations
* @param array $options
* @param OutputInterface $output
* @return void
*/
public function runPending(array $migrations, array $options = [])
public function runPending(array $migrations, array $options = [], OutputInterface $output = NULL)
{
// First we will just make sure that there are any migrations to run. If there
// aren't, we will just make a note of it to the developer so they're aware
Expand All @@ -143,7 +147,17 @@ public function runPending(array $migrations, array $options = [])
// 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.
foreach ($migrations as $file) {
if (!is_null($output)){
$output->write('<info>Running:</info> ' . $this->getMigrationName($file) . '...');
}

$now = Carbon::now();
$this->runUp($file, $batch, $pretend);
$difference = Carbon::now()->diffInSeconds($now);

if (!is_null($output)){
$output->writeln("Done! <comment>({$difference}s)</comment>");
}

if ($step) {
$batch++;
Expand Down
15 changes: 5 additions & 10 deletions tests/Database/DatabaseMigrationMigrateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public function testBasicMigrationsCallMigratorWithProperArguments()
$command->setLaravel($app);
$migrator->shouldReceive('paths')->once()->andReturn([]);
$migrator->shouldReceive('setConnection')->once()->with(null);
$migrator->shouldReceive('run')->once()->with([__DIR__.DIRECTORY_SEPARATOR.'migrations'], ['pretend' => false, 'step' => false]);
$migrator->shouldReceive('getNotes')->andReturn([]);
$migrator->shouldReceive('run')->once()->with([__DIR__.DIRECTORY_SEPARATOR.'migrations'], ['pretend' => false, 'step' => false], \Mockery::type('\Symfony\Component\Console\Output\OutputInterface'));
$migrator->shouldReceive('repositoryExists')->once()->andReturn(true);

$this->runCommand($command);
Expand All @@ -38,8 +37,7 @@ public function testMigrationRepositoryCreatedWhenNecessary()
$command->setLaravel($app);
$migrator->shouldReceive('paths')->once()->andReturn([]);
$migrator->shouldReceive('setConnection')->once()->with(null);
$migrator->shouldReceive('run')->once()->with([__DIR__.DIRECTORY_SEPARATOR.'migrations'], ['pretend' => false, 'step' => false]);
$migrator->shouldReceive('getNotes')->andReturn([]);
$migrator->shouldReceive('run')->once()->with([__DIR__.DIRECTORY_SEPARATOR.'migrations'], ['pretend' => false, 'step' => false], \Mockery::type('\Symfony\Component\Console\Output\OutputInterface'));
$migrator->shouldReceive('repositoryExists')->once()->andReturn(false);
$command->expects($this->once())->method('call')->with($this->equalTo('migrate:install'), $this->equalTo(['--database' => null]));

Expand All @@ -54,8 +52,7 @@ public function testTheCommandMayBePretended()
$command->setLaravel($app);
$migrator->shouldReceive('paths')->once()->andReturn([]);
$migrator->shouldReceive('setConnection')->once()->with(null);
$migrator->shouldReceive('run')->once()->with([__DIR__.DIRECTORY_SEPARATOR.'migrations'], ['pretend' => true, 'step' => false]);
$migrator->shouldReceive('getNotes')->andReturn([]);
$migrator->shouldReceive('run')->once()->with([__DIR__.DIRECTORY_SEPARATOR.'migrations'], ['pretend' => true, 'step' => false], \Mockery::type('\Symfony\Component\Console\Output\OutputInterface'));
$migrator->shouldReceive('repositoryExists')->once()->andReturn(true);

$this->runCommand($command, ['--pretend' => true]);
Expand All @@ -69,8 +66,7 @@ public function testTheDatabaseMayBeSet()
$command->setLaravel($app);
$migrator->shouldReceive('paths')->once()->andReturn([]);
$migrator->shouldReceive('setConnection')->once()->with('foo');
$migrator->shouldReceive('run')->once()->with([__DIR__.DIRECTORY_SEPARATOR.'migrations'], ['pretend' => false, 'step' => false]);
$migrator->shouldReceive('getNotes')->andReturn([]);
$migrator->shouldReceive('run')->once()->with([__DIR__.DIRECTORY_SEPARATOR.'migrations'], ['pretend' => false, 'step' => false], \Mockery::type('\Symfony\Component\Console\Output\OutputInterface'));
$migrator->shouldReceive('repositoryExists')->once()->andReturn(true);

$this->runCommand($command, ['--database' => 'foo']);
Expand All @@ -84,8 +80,7 @@ public function testStepMayBeSet()
$command->setLaravel($app);
$migrator->shouldReceive('paths')->once()->andReturn([]);
$migrator->shouldReceive('setConnection')->once()->with(null);
$migrator->shouldReceive('run')->once()->with([__DIR__.DIRECTORY_SEPARATOR.'migrations'], ['pretend' => false, 'step' => true]);
$migrator->shouldReceive('getNotes')->andReturn([]);
$migrator->shouldReceive('run')->once()->with([__DIR__.DIRECTORY_SEPARATOR.'migrations'], ['pretend' => false, 'step' => true], \Mockery::type('\Symfony\Component\Console\Output\OutputInterface'));
$migrator->shouldReceive('repositoryExists')->once()->andReturn(true);

$this->runCommand($command, ['--step' => true]);
Expand Down