@@ -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 | } |