Skip to content

Commit

Permalink
Interact with migrator rather than command
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljennings committed May 31, 2019
1 parent 629009c commit 6255f96
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
24 changes: 17 additions & 7 deletions src/DatabaseMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Contracts\Console\Kernel;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Database\Migrations\Migrator;
use Orchestra\Testbench\Concerns\CreatesApplication;
use Symfony\Component\Process\Process;

Expand Down Expand Up @@ -119,13 +120,22 @@ protected function runMigrations(
$this->refreshDatabase($this->databasePath);
}

foreach ($paths as $path) {
$app[Kernel::class]->call('migrate', [
'--database' => 'sqlite',
'--path' => $this->join($baseDir, $path),
'--realpath' => true,
]);
/** @var Migrator $migrator */
$migrator = $app->make('migrator');

$migrator->setConnection('sqlite');

if ( ! $migrator->repositoryExists()) {
$repository = $migrator->getRepository();

$repository->createRepository();
}

$paths = array_map(function($path) use ($baseDir) {
return realpath($this->join($baseDir, $path));
}, $paths);

$migrator->run($paths);
}

/**
Expand Down Expand Up @@ -274,4 +284,4 @@ protected function resolveApplicationExceptionHandler($app)
{
$app->singleton('Illuminate\Contracts\Debug\ExceptionHandler', ExceptionHandler::class);
}
}
}
33 changes: 33 additions & 0 deletions src/MigrationServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace MichaelJennings\RefreshDatabase;

use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\ServiceProvider;

class MigrationServiceProvider extends ServiceProvider
{
/**
* The migration paths.
*
* @var array
*/
protected $paths;

public function __construct(Application $app, array $paths)
{
parent::__construct($app);

$this->paths = $paths;
}

/**
* Perform post-registration booting of services.
*
* @return void
*/
public function boot()
{
$this->loadMigrationsFrom($this->paths);
}
}
2 changes: 1 addition & 1 deletion tests/Concerns/CleanUpDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ protected function removeFile($file)
unlink($file);
}
}
}
}
2 changes: 1 addition & 1 deletion tests/DatabaseMigratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,4 @@ protected function makeMigration(string $migration)

$this->writeFile($migration, $contents);
}
}
}

0 comments on commit 6255f96

Please sign in to comment.