@@ -9,118 +9,118 @@ |
||
| 9 | 9 | |
| 10 | 10 | class MigrateDisorganise extends BaseCommand |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * The console command name. |
|
| 14 | - * |
|
| 15 | - * @var string |
|
| 16 | - */ |
|
| 17 | - protected $name = 'migrate:disorganise'; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * The console command description. |
|
| 21 | - * |
|
| 22 | - * @var string |
|
| 23 | - */ |
|
| 24 | - protected $description = 'Move migrations from a yyyy/mm folder structure back to the base migrations folder'; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * The migrator instance. |
|
| 28 | - * |
|
| 29 | - * @var \Jaybizzle\MigrationsOrganiser\Migrator |
|
| 30 | - */ |
|
| 31 | - protected $migrator; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * The filesystem instance. |
|
| 35 | - * |
|
| 36 | - * @var \Illuminate\Filesystem\Filesystem |
|
| 37 | - */ |
|
| 38 | - protected $files; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * The basePath for the migrations. |
|
| 42 | - */ |
|
| 43 | - protected $basePath; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Create a new migrator instance. |
|
| 47 | - * |
|
| 48 | - * @param \Illuminate\Filesystem\Filesystem $files |
|
| 49 | - * @param \Illuminate\Database\Migrations\Migrator $migrator |
|
| 50 | - */ |
|
| 51 | - public function __construct(Filesystem $files, Migrator $migrator) |
|
| 52 | - { |
|
| 53 | - parent::__construct(); |
|
| 54 | - $this->migrator = $migrator; |
|
| 55 | - $this->files = $files; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Create date folder structure and move migrations into. |
|
| 60 | - * |
|
| 61 | - * @return void |
|
| 62 | - */ |
|
| 63 | - public function fire() |
|
| 64 | - { |
|
| 65 | - $this->basePath = $this->getMigrationPath(); |
|
| 66 | - $migrations = $this->migrator->getMigrationFiles($this->basePath); |
|
| 67 | - $count = count($migrations); |
|
| 68 | - |
|
| 69 | - if ($count == 0) { |
|
| 70 | - $this->comment('No migrations to move'); |
|
| 71 | - |
|
| 72 | - return; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - foreach ($migrations as $migration) { |
|
| 76 | - $datePath = $this->migrator->getDateFolderStructure($migration); |
|
| 77 | - // Move the migration into base migration folder |
|
| 78 | - $this->files->move($this->basePath.'/'.$datePath.$migration.'.php', $this->basePath.'/'.$migration.'.php'); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - $this->info('Migrations disorganised successfully ('.$count.' migrations moved)'); |
|
| 82 | - $this->cleanup(); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Decide whether or not to delete directories. |
|
| 87 | - * |
|
| 88 | - * @return void |
|
| 89 | - */ |
|
| 90 | - public function cleanup() |
|
| 91 | - { |
|
| 92 | - if ($this->option('force')) { |
|
| 93 | - $this->deleteDirs(); |
|
| 94 | - } elseif ($this->confirm('Delete all subdirectories in migrations folder? [yes|no]', false)) { |
|
| 95 | - $this->deleteDirs(); |
|
| 96 | - } |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Delete subdirectories in the migrations folder. |
|
| 101 | - * |
|
| 102 | - * @return void |
|
| 103 | - */ |
|
| 104 | - public function deleteDirs() |
|
| 105 | - { |
|
| 106 | - $dirs = $this->files->directories($this->basePath); |
|
| 107 | - |
|
| 108 | - foreach ($dirs as $dir) { |
|
| 109 | - $this->files->deleteDirectory($dir); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - $this->info('Subdirectories deleted'); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Get the console command options. |
|
| 117 | - * |
|
| 118 | - * @return array |
|
| 119 | - */ |
|
| 120 | - protected function getOptions() |
|
| 121 | - { |
|
| 122 | - return [ |
|
| 123 | - ['force', null, InputOption::VALUE_NONE, 'Force the operation to delete migration folder subdirectories without prompt.'], |
|
| 124 | - ]; |
|
| 125 | - } |
|
| 12 | + /** |
|
| 13 | + * The console command name. |
|
| 14 | + * |
|
| 15 | + * @var string |
|
| 16 | + */ |
|
| 17 | + protected $name = 'migrate:disorganise'; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * The console command description. |
|
| 21 | + * |
|
| 22 | + * @var string |
|
| 23 | + */ |
|
| 24 | + protected $description = 'Move migrations from a yyyy/mm folder structure back to the base migrations folder'; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * The migrator instance. |
|
| 28 | + * |
|
| 29 | + * @var \Jaybizzle\MigrationsOrganiser\Migrator |
|
| 30 | + */ |
|
| 31 | + protected $migrator; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * The filesystem instance. |
|
| 35 | + * |
|
| 36 | + * @var \Illuminate\Filesystem\Filesystem |
|
| 37 | + */ |
|
| 38 | + protected $files; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * The basePath for the migrations. |
|
| 42 | + */ |
|
| 43 | + protected $basePath; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Create a new migrator instance. |
|
| 47 | + * |
|
| 48 | + * @param \Illuminate\Filesystem\Filesystem $files |
|
| 49 | + * @param \Illuminate\Database\Migrations\Migrator $migrator |
|
| 50 | + */ |
|
| 51 | + public function __construct(Filesystem $files, Migrator $migrator) |
|
| 52 | + { |
|
| 53 | + parent::__construct(); |
|
| 54 | + $this->migrator = $migrator; |
|
| 55 | + $this->files = $files; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Create date folder structure and move migrations into. |
|
| 60 | + * |
|
| 61 | + * @return void |
|
| 62 | + */ |
|
| 63 | + public function fire() |
|
| 64 | + { |
|
| 65 | + $this->basePath = $this->getMigrationPath(); |
|
| 66 | + $migrations = $this->migrator->getMigrationFiles($this->basePath); |
|
| 67 | + $count = count($migrations); |
|
| 68 | + |
|
| 69 | + if ($count == 0) { |
|
| 70 | + $this->comment('No migrations to move'); |
|
| 71 | + |
|
| 72 | + return; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + foreach ($migrations as $migration) { |
|
| 76 | + $datePath = $this->migrator->getDateFolderStructure($migration); |
|
| 77 | + // Move the migration into base migration folder |
|
| 78 | + $this->files->move($this->basePath.'/'.$datePath.$migration.'.php', $this->basePath.'/'.$migration.'.php'); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + $this->info('Migrations disorganised successfully ('.$count.' migrations moved)'); |
|
| 82 | + $this->cleanup(); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Decide whether or not to delete directories. |
|
| 87 | + * |
|
| 88 | + * @return void |
|
| 89 | + */ |
|
| 90 | + public function cleanup() |
|
| 91 | + { |
|
| 92 | + if ($this->option('force')) { |
|
| 93 | + $this->deleteDirs(); |
|
| 94 | + } elseif ($this->confirm('Delete all subdirectories in migrations folder? [yes|no]', false)) { |
|
| 95 | + $this->deleteDirs(); |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Delete subdirectories in the migrations folder. |
|
| 101 | + * |
|
| 102 | + * @return void |
|
| 103 | + */ |
|
| 104 | + public function deleteDirs() |
|
| 105 | + { |
|
| 106 | + $dirs = $this->files->directories($this->basePath); |
|
| 107 | + |
|
| 108 | + foreach ($dirs as $dir) { |
|
| 109 | + $this->files->deleteDirectory($dir); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + $this->info('Subdirectories deleted'); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Get the console command options. |
|
| 117 | + * |
|
| 118 | + * @return array |
|
| 119 | + */ |
|
| 120 | + protected function getOptions() |
|
| 121 | + { |
|
| 122 | + return [ |
|
| 123 | + ['force', null, InputOption::VALUE_NONE, 'Force the operation to delete migration folder subdirectories without prompt.'], |
|
| 124 | + ]; |
|
| 125 | + } |
|
| 126 | 126 | } |
@@ -8,41 +8,41 @@ |
||
| 8 | 8 | |
| 9 | 9 | class MigrationsOrganiserServiceProvider extends MSP |
| 10 | 10 | { |
| 11 | - public function register() |
|
| 12 | - { |
|
| 13 | - parent::register(); |
|
| 14 | - $this->registerMigrateOrganise(); |
|
| 15 | - $this->registerMigrateDisorganise(); |
|
| 16 | - $this->commands('command.migrate.organise', 'command.migrate.disorganise'); |
|
| 17 | - } |
|
| 18 | - |
|
| 19 | - protected function registerCreator() |
|
| 20 | - { |
|
| 21 | - $this->app->singleton('migration.creator', function ($app) { |
|
| 22 | - return new MigrationCreator($app['files']); |
|
| 23 | - }); |
|
| 24 | - } |
|
| 25 | - |
|
| 26 | - protected function registerMigrator() |
|
| 27 | - { |
|
| 28 | - $this->app->singleton('migrator', function ($app) { |
|
| 29 | - $repository = $app['migration.repository']; |
|
| 30 | - |
|
| 31 | - return new Migrator($repository, $app['db'], $app['files']); |
|
| 32 | - }); |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - protected function registerMigrateOrganise() |
|
| 36 | - { |
|
| 37 | - $this->app->singleton('command.migrate.organise', function ($app) { |
|
| 38 | - return new MigrateOrganise($app['files'], $app['migrator']); |
|
| 39 | - }); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - protected function registerMigrateDisorganise() |
|
| 43 | - { |
|
| 44 | - $this->app->singleton('command.migrate.disorganise', function ($app) { |
|
| 45 | - return new MigrateDisorganise($app['files'], $app['migrator']); |
|
| 46 | - }); |
|
| 47 | - } |
|
| 11 | + public function register() |
|
| 12 | + { |
|
| 13 | + parent::register(); |
|
| 14 | + $this->registerMigrateOrganise(); |
|
| 15 | + $this->registerMigrateDisorganise(); |
|
| 16 | + $this->commands('command.migrate.organise', 'command.migrate.disorganise'); |
|
| 17 | + } |
|
| 18 | + |
|
| 19 | + protected function registerCreator() |
|
| 20 | + { |
|
| 21 | + $this->app->singleton('migration.creator', function ($app) { |
|
| 22 | + return new MigrationCreator($app['files']); |
|
| 23 | + }); |
|
| 24 | + } |
|
| 25 | + |
|
| 26 | + protected function registerMigrator() |
|
| 27 | + { |
|
| 28 | + $this->app->singleton('migrator', function ($app) { |
|
| 29 | + $repository = $app['migration.repository']; |
|
| 30 | + |
|
| 31 | + return new Migrator($repository, $app['db'], $app['files']); |
|
| 32 | + }); |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + protected function registerMigrateOrganise() |
|
| 36 | + { |
|
| 37 | + $this->app->singleton('command.migrate.organise', function ($app) { |
|
| 38 | + return new MigrateOrganise($app['files'], $app['migrator']); |
|
| 39 | + }); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + protected function registerMigrateDisorganise() |
|
| 43 | + { |
|
| 44 | + $this->app->singleton('command.migrate.disorganise', function ($app) { |
|
| 45 | + return new MigrateDisorganise($app['files'], $app['migrator']); |
|
| 46 | + }); |
|
| 47 | + } |
|
| 48 | 48 | } |
@@ -6,14 +6,14 @@ |
||
| 6 | 6 | |
| 7 | 7 | class MigrationCreator extends MC |
| 8 | 8 | { |
| 9 | - protected function getPath($name, $path) |
|
| 10 | - { |
|
| 11 | - $path = $path.'/'.date('Y').'/'.date('m'); |
|
| 9 | + protected function getPath($name, $path) |
|
| 10 | + { |
|
| 11 | + $path = $path.'/'.date('Y').'/'.date('m'); |
|
| 12 | 12 | |
| 13 | - if (! $this->files->exists($path)) { |
|
| 14 | - $this->files->makeDirectory($path, 0775, true); |
|
| 15 | - } |
|
| 13 | + if (! $this->files->exists($path)) { |
|
| 14 | + $this->files->makeDirectory($path, 0775, true); |
|
| 15 | + } |
|
| 16 | 16 | |
| 17 | - return $path.'/'.$this->getDatePrefix().'_'.$name.'.php'; |
|
| 18 | - } |
|
| 17 | + return $path.'/'.$this->getDatePrefix().'_'.$name.'.php'; |
|
| 18 | + } |
|
| 19 | 19 | } |
@@ -8,76 +8,76 @@ |
||
| 8 | 8 | |
| 9 | 9 | class MigrateOrganise extends BaseCommand |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * The console command name. |
|
| 13 | - * |
|
| 14 | - * @var string |
|
| 15 | - */ |
|
| 16 | - protected $name = 'migrate:organise'; |
|
| 11 | + /** |
|
| 12 | + * The console command name. |
|
| 13 | + * |
|
| 14 | + * @var string |
|
| 15 | + */ |
|
| 16 | + protected $name = 'migrate:organise'; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * The console command description. |
|
| 20 | - * |
|
| 21 | - * @var string |
|
| 22 | - */ |
|
| 23 | - protected $description = 'Move migrations into a yyyy/mm folder structure'; |
|
| 18 | + /** |
|
| 19 | + * The console command description. |
|
| 20 | + * |
|
| 21 | + * @var string |
|
| 22 | + */ |
|
| 23 | + protected $description = 'Move migrations into a yyyy/mm folder structure'; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * The migrator instance. |
|
| 27 | - * |
|
| 28 | - * @var \Jaybizzle\MigrationsOrganiser\Migrator |
|
| 29 | - */ |
|
| 30 | - protected $migrator; |
|
| 25 | + /** |
|
| 26 | + * The migrator instance. |
|
| 27 | + * |
|
| 28 | + * @var \Jaybizzle\MigrationsOrganiser\Migrator |
|
| 29 | + */ |
|
| 30 | + protected $migrator; |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * The filesystem instance. |
|
| 34 | - * |
|
| 35 | - * @var \Illuminate\Filesystem\Filesystem |
|
| 36 | - */ |
|
| 37 | - protected $files; |
|
| 32 | + /** |
|
| 33 | + * The filesystem instance. |
|
| 34 | + * |
|
| 35 | + * @var \Illuminate\Filesystem\Filesystem |
|
| 36 | + */ |
|
| 37 | + protected $files; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Create a new migrator instance. |
|
| 41 | - * |
|
| 42 | - * @param \Illuminate\Filesystem\Filesystem $files |
|
| 43 | - * @param \Illuminate\Database\Migrations\Migrator $migrator |
|
| 44 | - */ |
|
| 45 | - public function __construct(Filesystem $files, Migrator $migrator) |
|
| 46 | - { |
|
| 47 | - parent::__construct(); |
|
| 48 | - $this->migrator = $migrator; |
|
| 49 | - $this->files = $files; |
|
| 50 | - } |
|
| 39 | + /** |
|
| 40 | + * Create a new migrator instance. |
|
| 41 | + * |
|
| 42 | + * @param \Illuminate\Filesystem\Filesystem $files |
|
| 43 | + * @param \Illuminate\Database\Migrations\Migrator $migrator |
|
| 44 | + */ |
|
| 45 | + public function __construct(Filesystem $files, Migrator $migrator) |
|
| 46 | + { |
|
| 47 | + parent::__construct(); |
|
| 48 | + $this->migrator = $migrator; |
|
| 49 | + $this->files = $files; |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Create date folder structure and move migrations into. |
|
| 54 | - * |
|
| 55 | - * @return void |
|
| 56 | - */ |
|
| 57 | - public function fire() |
|
| 58 | - { |
|
| 59 | - $basePath = $this->getMigrationPath(); |
|
| 60 | - $migrations = $this->migrator->getMigrationFiles($basePath, false); |
|
| 61 | - $count = count($migrations); |
|
| 52 | + /** |
|
| 53 | + * Create date folder structure and move migrations into. |
|
| 54 | + * |
|
| 55 | + * @return void |
|
| 56 | + */ |
|
| 57 | + public function fire() |
|
| 58 | + { |
|
| 59 | + $basePath = $this->getMigrationPath(); |
|
| 60 | + $migrations = $this->migrator->getMigrationFiles($basePath, false); |
|
| 61 | + $count = count($migrations); |
|
| 62 | 62 | |
| 63 | - if ($count == 0) { |
|
| 64 | - $this->comment('No migrations to move'); |
|
| 63 | + if ($count == 0) { |
|
| 64 | + $this->comment('No migrations to move'); |
|
| 65 | 65 | |
| 66 | - return; |
|
| 67 | - } |
|
| 66 | + return; |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - foreach ($migrations as $migration) { |
|
| 70 | - $datePath = $this->migrator->getDateFolderStructure($migration); |
|
| 69 | + foreach ($migrations as $migration) { |
|
| 70 | + $datePath = $this->migrator->getDateFolderStructure($migration); |
|
| 71 | 71 | |
| 72 | - // Create folder if it does not already exist |
|
| 73 | - if (! $this->files->exists($basePath.'/'.$datePath)) { |
|
| 74 | - $this->files->makeDirectory($basePath.'/'.$datePath, 0775, true); |
|
| 75 | - } |
|
| 72 | + // Create folder if it does not already exist |
|
| 73 | + if (! $this->files->exists($basePath.'/'.$datePath)) { |
|
| 74 | + $this->files->makeDirectory($basePath.'/'.$datePath, 0775, true); |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - // Move the migration into its new folder |
|
| 78 | - $this->files->move($basePath.'/'.$migration.'.php', $basePath.'/'.$datePath.$migration.'.php'); |
|
| 79 | - } |
|
| 77 | + // Move the migration into its new folder |
|
| 78 | + $this->files->move($basePath.'/'.$migration.'.php', $basePath.'/'.$datePath.$migration.'.php'); |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - $this->info('Migrations organised successfully ('.$count.' migrations moved)'); |
|
| 82 | - } |
|
| 81 | + $this->info('Migrations organised successfully ('.$count.' migrations moved)'); |
|
| 82 | + } |
|
| 83 | 83 | } |
@@ -44,7 +44,6 @@ |
||
| 44 | 44 | /** |
| 45 | 45 | * Require in all the migration files in a given path. |
| 46 | 46 | * |
| 47 | - * @param string $path |
|
| 48 | 47 | * @param array $files |
| 49 | 48 | * |
| 50 | 49 | * @return void |
@@ -6,148 +6,148 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Migrator extends M |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Get all of the migration files in a given path. |
|
| 11 | - * |
|
| 12 | - * @param string $path |
|
| 13 | - * |
|
| 14 | - * @return array |
|
| 15 | - */ |
|
| 16 | - public function getMigrationFiles($path, $recursive = true) |
|
| 17 | - { |
|
| 18 | - if ($recursive === true) { |
|
| 19 | - $files = $this->rglob($path.'/*_*.php', 0, true); |
|
| 20 | - } else { |
|
| 21 | - $files = $this->files->glob($path.'/*_*.php'); |
|
| 22 | - } |
|
| 23 | - |
|
| 24 | - // Once we have the array of files in the directory we will just remove the |
|
| 25 | - // extension and take the basename of the file which is all we need when |
|
| 26 | - // finding the migrations that haven't been run against the databases. |
|
| 27 | - if ($files === false) { |
|
| 28 | - return []; |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - $files = array_map(function ($file) { |
|
| 32 | - return str_replace('.php', '', basename($file)); |
|
| 33 | - |
|
| 34 | - }, $files); |
|
| 35 | - |
|
| 36 | - // Once we have all of the formatted file names we will sort them and since |
|
| 37 | - // they all start with a timestamp this should give us the migrations in |
|
| 38 | - // the order they were actually created by the application developers. |
|
| 39 | - sort($files); |
|
| 40 | - |
|
| 41 | - return $files; |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Require in all the migration files in a given path. |
|
| 46 | - * |
|
| 47 | - * @param string $path |
|
| 48 | - * @param array $files |
|
| 49 | - * |
|
| 50 | - * @return void |
|
| 51 | - */ |
|
| 52 | - public function requireFiles(array $files) |
|
| 53 | - { |
|
| 54 | - foreach ($files as $file) { |
|
| 55 | - $newPath = $this->getFilePathWithFolders($file).'.php'; |
|
| 56 | - $this->files->requireOnce($newPath); |
|
| 57 | - } |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Run "up" a migration instance. |
|
| 62 | - * |
|
| 63 | - * @param string $file |
|
| 64 | - * @param int $batch |
|
| 65 | - * @param bool $pretend |
|
| 66 | - * |
|
| 67 | - * @return void |
|
| 68 | - */ |
|
| 69 | - protected function runUp($file, $batch, $pretend) |
|
| 70 | - { |
|
| 71 | - // First we will resolve a "real" instance of the migration class from this |
|
| 72 | - // migration file name. Once we have the instances we can run the actual |
|
| 73 | - // command such as "up" or "down", or we can just simulate the action. |
|
| 74 | - $migration = $this->resolve($file); |
|
| 75 | - |
|
| 76 | - if ($pretend) { |
|
| 77 | - return $this->pretendToRun($migration, 'up'); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - $migration->up(); |
|
| 81 | - |
|
| 82 | - // Once we have run a migrations class, we will log that it was run in this |
|
| 83 | - // repository so that we don't try to run it next time we do a migration |
|
| 84 | - // in the application. A migration repository keeps the migrate order. |
|
| 85 | - $this->repository->log($this->getFilePathWithoutFolders($file), $batch); |
|
| 86 | - |
|
| 87 | - $this->note("<info>Migrated:</info> $file"); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Recursive glob. |
|
| 92 | - * |
|
| 93 | - * @param string $pattern |
|
| 94 | - * @param int $flags |
|
| 95 | - * @param bool $ignore |
|
| 96 | - * |
|
| 97 | - * @return array |
|
| 98 | - */ |
|
| 99 | - public function rglob($pattern, $flags = 0, $ignore = false) |
|
| 100 | - { |
|
| 101 | - if ($ignore === false) { |
|
| 102 | - $files = glob($pattern, $flags); |
|
| 103 | - } else { |
|
| 104 | - $files = []; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) { |
|
| 108 | - $files = array_merge($files, $this->rglob($dir.'/'.basename($pattern), $flags)); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - return $files; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Get the migration file path with our injected date folder. |
|
| 116 | - * |
|
| 117 | - * @param string $file |
|
| 118 | - * |
|
| 119 | - * @return string |
|
| 120 | - */ |
|
| 121 | - public function getFilePathWithFolders($file) |
|
| 122 | - { |
|
| 123 | - $datePath = $this->getDateFolderStructure($file); |
|
| 124 | - |
|
| 125 | - return '/'.$datePath.$file; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Remove folders from file path. |
|
| 130 | - * |
|
| 131 | - * @param string $file |
|
| 132 | - * |
|
| 133 | - * @return string |
|
| 134 | - */ |
|
| 135 | - public function getFilePathWithoutFolders($file) |
|
| 136 | - { |
|
| 137 | - return basename($file); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Add date folders to migrations path. |
|
| 142 | - * |
|
| 143 | - * @param string $file |
|
| 144 | - * |
|
| 145 | - * @return string |
|
| 146 | - */ |
|
| 147 | - public function getDateFolderStructure($file) |
|
| 148 | - { |
|
| 149 | - $parts = explode('_', $file); |
|
| 150 | - |
|
| 151 | - return $parts[0].'/'.$parts[1].'/'; |
|
| 152 | - } |
|
| 9 | + /** |
|
| 10 | + * Get all of the migration files in a given path. |
|
| 11 | + * |
|
| 12 | + * @param string $path |
|
| 13 | + * |
|
| 14 | + * @return array |
|
| 15 | + */ |
|
| 16 | + public function getMigrationFiles($path, $recursive = true) |
|
| 17 | + { |
|
| 18 | + if ($recursive === true) { |
|
| 19 | + $files = $this->rglob($path.'/*_*.php', 0, true); |
|
| 20 | + } else { |
|
| 21 | + $files = $this->files->glob($path.'/*_*.php'); |
|
| 22 | + } |
|
| 23 | + |
|
| 24 | + // Once we have the array of files in the directory we will just remove the |
|
| 25 | + // extension and take the basename of the file which is all we need when |
|
| 26 | + // finding the migrations that haven't been run against the databases. |
|
| 27 | + if ($files === false) { |
|
| 28 | + return []; |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + $files = array_map(function ($file) { |
|
| 32 | + return str_replace('.php', '', basename($file)); |
|
| 33 | + |
|
| 34 | + }, $files); |
|
| 35 | + |
|
| 36 | + // Once we have all of the formatted file names we will sort them and since |
|
| 37 | + // they all start with a timestamp this should give us the migrations in |
|
| 38 | + // the order they were actually created by the application developers. |
|
| 39 | + sort($files); |
|
| 40 | + |
|
| 41 | + return $files; |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Require in all the migration files in a given path. |
|
| 46 | + * |
|
| 47 | + * @param string $path |
|
| 48 | + * @param array $files |
|
| 49 | + * |
|
| 50 | + * @return void |
|
| 51 | + */ |
|
| 52 | + public function requireFiles(array $files) |
|
| 53 | + { |
|
| 54 | + foreach ($files as $file) { |
|
| 55 | + $newPath = $this->getFilePathWithFolders($file).'.php'; |
|
| 56 | + $this->files->requireOnce($newPath); |
|
| 57 | + } |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Run "up" a migration instance. |
|
| 62 | + * |
|
| 63 | + * @param string $file |
|
| 64 | + * @param int $batch |
|
| 65 | + * @param bool $pretend |
|
| 66 | + * |
|
| 67 | + * @return void |
|
| 68 | + */ |
|
| 69 | + protected function runUp($file, $batch, $pretend) |
|
| 70 | + { |
|
| 71 | + // First we will resolve a "real" instance of the migration class from this |
|
| 72 | + // migration file name. Once we have the instances we can run the actual |
|
| 73 | + // command such as "up" or "down", or we can just simulate the action. |
|
| 74 | + $migration = $this->resolve($file); |
|
| 75 | + |
|
| 76 | + if ($pretend) { |
|
| 77 | + return $this->pretendToRun($migration, 'up'); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + $migration->up(); |
|
| 81 | + |
|
| 82 | + // Once we have run a migrations class, we will log that it was run in this |
|
| 83 | + // repository so that we don't try to run it next time we do a migration |
|
| 84 | + // in the application. A migration repository keeps the migrate order. |
|
| 85 | + $this->repository->log($this->getFilePathWithoutFolders($file), $batch); |
|
| 86 | + |
|
| 87 | + $this->note("<info>Migrated:</info> $file"); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Recursive glob. |
|
| 92 | + * |
|
| 93 | + * @param string $pattern |
|
| 94 | + * @param int $flags |
|
| 95 | + * @param bool $ignore |
|
| 96 | + * |
|
| 97 | + * @return array |
|
| 98 | + */ |
|
| 99 | + public function rglob($pattern, $flags = 0, $ignore = false) |
|
| 100 | + { |
|
| 101 | + if ($ignore === false) { |
|
| 102 | + $files = glob($pattern, $flags); |
|
| 103 | + } else { |
|
| 104 | + $files = []; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) { |
|
| 108 | + $files = array_merge($files, $this->rglob($dir.'/'.basename($pattern), $flags)); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + return $files; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Get the migration file path with our injected date folder. |
|
| 116 | + * |
|
| 117 | + * @param string $file |
|
| 118 | + * |
|
| 119 | + * @return string |
|
| 120 | + */ |
|
| 121 | + public function getFilePathWithFolders($file) |
|
| 122 | + { |
|
| 123 | + $datePath = $this->getDateFolderStructure($file); |
|
| 124 | + |
|
| 125 | + return '/'.$datePath.$file; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Remove folders from file path. |
|
| 130 | + * |
|
| 131 | + * @param string $file |
|
| 132 | + * |
|
| 133 | + * @return string |
|
| 134 | + */ |
|
| 135 | + public function getFilePathWithoutFolders($file) |
|
| 136 | + { |
|
| 137 | + return basename($file); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Add date folders to migrations path. |
|
| 142 | + * |
|
| 143 | + * @param string $file |
|
| 144 | + * |
|
| 145 | + * @return string |
|
| 146 | + */ |
|
| 147 | + public function getDateFolderStructure($file) |
|
| 148 | + { |
|
| 149 | + $parts = explode('_', $file); |
|
| 150 | + |
|
| 151 | + return $parts[0].'/'.$parts[1].'/'; |
|
| 152 | + } |
|
| 153 | 153 | } |