Skip to content

Commit

Permalink
Merge pull request #1 from MarceloHoffmeister/feature/add-option-fres…
Browse files Browse the repository at this point in the history
…h-command

Add support for --path option on fresh command
  • Loading branch information
MarceloHoffmeister authored Apr 12, 2020
2 parents 98ace9c + 3076c4d commit 4237989
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions src/Console/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@

class BaseCommand extends Command
{
/**
* Get all of the migration paths.
*
* @return array
*/
protected function getMigrationPaths()
{
// Here, 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 the installation folder so our database
// migrations may be run for any customized path from within the application.
if ($this->input->hasOption('path') && $this->option('path')) {
return collect($this->option('path'))->map(function ($path) {

return $path;
})->all();
}

// Empty Base Command.
public function handle()
{
Expand Down
2 changes: 2 additions & 0 deletions src/Console/FreshCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function handle()
$options = [
'--database' => $database,
'--force' => true,
'--path' => $this->input->getOption('path'),
];
if ($this->needsSeeding()) {
$options['--seed'] = true;
Expand Down Expand Up @@ -102,6 +103,7 @@ protected function getOptions()
['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'],
['seed', null, InputOption::VALUE_NONE, 'Indicates if the seed task should be re-run.'],
['seeder', null, InputOption::VALUE_OPTIONAL, 'The class name of the root seeder.'],
['path', null, InputOption::VALUE_OPTIONAL, 'The path to the migrations files to be executed'],
];
}
}
12 changes: 9 additions & 3 deletions src/Console/MigrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ class MigrateCommand extends BaseCommand
use ConfirmableTrait;

/**
* The console command name.
* The name and signature of the console command.
*
* @var string
*/
protected $name = 'migrator';
protected $signature = 'migrator
{--database= : The database connection to use}
{--force : Force the operation to run when in production}
{--path=* : The path(s) to the migrations files to be executed}
{--pretend : Dump the SQL queries that would be run}
{--seed : Indicates if the seed task should be re-run}
{--step : Force the migrations to be run so they can be rolled back individually}';

/**
* The console command description.
Expand Down Expand Up @@ -61,7 +67,7 @@ public function fire()
// a database for real, which is helpful for double checking migrations.
$pretend = $this->input->getOption('pretend');

$this->migrator->run([
$this->migrator->run($this->getMigrationPaths(), [
'pretend' => $pretend,
'step' => $this->input->getOption('step'),
]);
Expand Down
71 changes: 63 additions & 8 deletions src/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Illuminate\Support\Arr;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Database\ConnectionResolverInterface as Resolver;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

class Migrator
{
Expand Down Expand Up @@ -103,7 +105,11 @@ public function run(array $options = [])
{
$this->notes = [];

$allMigrations = $this->getMigrations();
if ($paths[0] === "") {
$allMigrations = $this->getMigrations();
} else {
$allMigrations = $this->getMigrationFiles($paths);
}

// Once we grab all of the migration files for the path, we will compare them
// against the migrations that have already been run for this package then
Expand Down Expand Up @@ -284,8 +290,8 @@ public function reset($pretend = false)
/**
* Run "down" a migration instance.
*
* @param object $migration
* @param bool $pretend
* @param object $migration
* @param bool $pretend
* @return void
*/
protected function runDown($migration, $pretend)
Expand Down Expand Up @@ -324,8 +330,8 @@ public function getMigrations()
/**
* Pretend to run the migrations.
*
* @param object $migration
* @param string $method
* @param object $migration
* @param string $method
* @return void
*/
protected function pretendToRun($migration, $method)
Expand All @@ -340,8 +346,8 @@ protected function pretendToRun($migration, $method)
/**
* Get all of the queries that would be run for a migration.
*
* @param object $migration
* @param string $method
* @param object $migration
* @param string $method
* @return array
*/
protected function getQueries($migration, $method)
Expand All @@ -361,14 +367,63 @@ protected function getQueries($migration, $method)
/**
* Create a instance of a registered migration
*
* @param string $class
* @param string $class
* @return object
*/
public function resolve($class)
{
return new $class;
}

/**
* Get all of the migration files in a given path.
*
* @param string|array $paths
* @return array|string
*/
public function getMigrationFiles($paths)
{
$allMigrations = Collection::make($paths)->flatMap(function ($path) {
return Str::endsWith($path, '.php') ? [$path] : $this->files->glob($path.'/*.php');
})->filter()->values()->keyBy(function ($file) {
return $this->getMigrationName($file);
})->sortBy(function ($file, $key) {
return $key;
})->all();

return $this->getNameSpace($allMigrations);
}

/**
* Get the namespace of the migration.
*
* @param array|string $path
* @return array|string
*/
public function getNameSpace($paths)
{
$allNamespaces = [];
foreach ($paths as $filename => $basepath) {
$basepath = str_replace('app', 'Bitis\Core', $basepath);
$basepath = str_replace('/', '\\', $basepath);
$basepath = str_replace('.php', '', $basepath);
array_push($allNamespaces, $basepath);
}

return $allNamespaces;
}

/**
* Get the name of the migration.
*
* @param string $path
* @return string
*/
public function getMigrationName($path)
{
return str_replace('.php', '', basename($path));
}

/**
* Raise a note event for the migrator.
*
Expand Down

0 comments on commit 4237989

Please sign in to comment.