diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..e7e9d11 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml diff --git a/src/Console/BaseCommand.php b/src/Console/BaseCommand.php index 510ddc4..743a187 100755 --- a/src/Console/BaseCommand.php +++ b/src/Console/BaseCommand.php @@ -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() { diff --git a/src/Console/FreshCommand.php b/src/Console/FreshCommand.php index 04ca003..6e5f2eb 100644 --- a/src/Console/FreshCommand.php +++ b/src/Console/FreshCommand.php @@ -46,6 +46,7 @@ public function handle() $options = [ '--database' => $database, '--force' => true, + '--path' => $this->input->getOption('path'), ]; if ($this->needsSeeding()) { $options['--seed'] = true; @@ -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'], ]; } } diff --git a/src/Console/MigrateCommand.php b/src/Console/MigrateCommand.php index 2cae3a6..12a118d 100755 --- a/src/Console/MigrateCommand.php +++ b/src/Console/MigrateCommand.php @@ -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. @@ -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'), ]); diff --git a/src/Migrator.php b/src/Migrator.php index 48bc75c..dddb1b9 100755 --- a/src/Migrator.php +++ b/src/Migrator.php @@ -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 { @@ -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 @@ -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) @@ -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) @@ -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) @@ -361,7 +367,7 @@ protected function getQueries($migration, $method) /** * Create a instance of a registered migration * - * @param string $class + * @param string $class * @return object */ public function resolve($class) @@ -369,6 +375,55 @@ 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. *