diff options
author | msquare <msquare@notrademark.de> | 2019-07-21 21:24:41 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-21 21:24:41 +0200 |
commit | 2ef5a7621a57607c5e7310a42ff09293c4dfd4b1 (patch) | |
tree | f8a40e38e7b9254cf3d5287267382de5ba19c8cb /src | |
parent | 0e6de03ac1d9c57558dc7d3a8b93116b1d8bea07 (diff) | |
parent | 64e4d168e01387d5650aaebecdadc4c815071cc3 (diff) |
Merge pull request #626 from MyIgel/migration
migration: Order by migrated and append not migrated, add more help parameters
Diffstat (limited to 'src')
-rw-r--r-- | src/Database/Migration/Migrate.php | 77 |
1 files changed, 61 insertions, 16 deletions
diff --git a/src/Database/Migration/Migrate.php b/src/Database/Migration/Migrate.php index c8ee9456..214903e4 100644 --- a/src/Database/Migration/Migrate.php +++ b/src/Database/Migration/Migrate.php @@ -50,26 +50,33 @@ class Migrate public function run($path, $type = self::UP, $oneStep = false) { $this->initMigration(); - $migrations = $this->getMigrations($path); - $migrated = $this->getMigrated(); + $migrations = $this->mergeMigrations( + $this->getMigrations($path), + $this->getMigrated() + ); if ($type == self::DOWN) { - $migrations = array_reverse($migrations, true); + $migrations = $migrations->reverse(); } - foreach ($migrations as $file => $migration) { + foreach ($migrations as $migration) { + /** @var array $migration */ + $name = $migration['migration']; + if ( - ($type == self::UP && $migrated->contains('migration', $migration)) - || ($type == self::DOWN && !$migrated->contains('migration', $migration)) + ($type == self::UP && isset($migration['id'])) + || ($type == self::DOWN && !isset($migration['id'])) ) { - call_user_func($this->output, 'Skipping ' . $migration); + ($this->output)('Skipping ' . $name); continue; } - call_user_func($this->output, 'Migrating ' . $migration . ' (' . $type . ')'); + ($this->output)('Migrating ' . $name . ' (' . $type . ')'); - $this->migrate($file, $migration, $type); - $this->setMigrated($migration, $type); + if (isset($migration['path'])) { + $this->migrate($migration['path'], $name, $type); + } + $this->setMigrated($name, $type); if ($oneStep) { return; @@ -93,13 +100,47 @@ class Migrate } /** + * Merge file migrations with already migrated tables + * + * @param Collection $migrations + * @param Collection $migrated + * @return Collection + */ + protected function mergeMigrations(Collection $migrations, Collection $migrated) + { + $return = $migrated; + $return->transform(function ($migration) use ($migrations) { + $migration = (array)$migration; + if ($migrations->contains('migration', $migration['migration'])) { + $migration += $migrations + ->where('migration', $migration['migration']) + ->first(); + } + + return $migration; + }); + + $migrations->each(function ($migration) use ($return) { + if ($return->contains('migration', $migration['migration'])) { + return; + } + + $return->add($migration); + }); + + return $return; + } + + /** * Get all migrated migrations * * @return Collection */ protected function getMigrated() { - return $this->getTableQuery()->get(); + return $this->getTableQuery() + ->orderBy('id') + ->get(); } /** @@ -144,20 +185,24 @@ class Migrate * Get a list of migration files * * @param string $dir - * @return array + * @return Collection */ protected function getMigrations($dir) { $files = $this->getMigrationFiles($dir); - $migrations = []; + $migrations = new Collection(); foreach ($files as $dir) { $name = str_replace('.php', '', basename($dir)); - $migrations[$dir] = $name; + $migrations[] = [ + 'migration' => $name, + 'path' => $dir, + ]; } - asort($migrations); - return $migrations; + return $migrations->sortBy(function ($value) { + return $value['migration']; + }); } /** |