diff --git a/README.md b/README.md index 4a33139..1a3d291 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ by using the `db` and `migrate` commands. If you don't pass anything in the opti ./kettle migrate:create [] Create new database migration class ./kettle migrate:run [] [] Perform forward database migration ./kettle migrate:rollback [] [] Perform backward database migration +./kettle migrate:point [] [] Point current to a specific migration, without running ./kettle migrate:reset [] Perform complete rollback of the database ``` diff --git a/config/routes.php b/config/routes.php index 1d6c332..65e7710 100644 --- a/config/routes.php +++ b/config/routes.php @@ -56,6 +56,11 @@ 'action' => 'rollback', 'help' => 'Perform backward database migration' ], + 'migrate:point [] []' => [ + 'controller' => 'Pop\Kettle\Controller\MigrationController', + 'action' => 'point', + 'help' => 'Point current to a specific migration, without running' + ], 'migrate:reset []' => [ 'controller' => 'Pop\Kettle\Controller\MigrationController', 'action' => 'reset', diff --git a/src/Controller/MigrationController.php b/src/Controller/MigrationController.php index f1bbf50..e917725 100644 --- a/src/Controller/MigrationController.php +++ b/src/Controller/MigrationController.php @@ -174,6 +174,66 @@ public function rollback($steps = 1, $database = 'default') } } + /** + * Point command + * + * @param mixed $id + * @param string $database + * @return void + */ + public function point($id = 'latest', $database = 'default') + { + $location = getcwd(); + + if (null === $id) { + $id = 'latest'; + } + if (null === $database) { + $database = 'default'; + } + + if (!file_exists($location . '/database/migrations/' . $database)) { + $this->console->write($this->console->colorize( + "The database '" . $database . "' does not exist in the migration folder.", Console::BOLD_RED + )); + } else { + $ids = array_map(function($value) { + return substr($value, 0, strpos($value, '_')); + }, + array_values(array_filter(scandir($location . '/database/migrations/' . $database), function ($value){ + if (($value != '.') && ($value != '..') && ($value != '.current') && ($value != '.empty') && (stripos($value, '_') !== false)) { + return $value; + } + }) + )); + + if (!empty($ids) && is_array($ids)) { + sort($ids, SORT_NUMERIC); + } + + if (empty($ids)) { + $this->console->write($this->console->colorize( + "No migrations for the database '" . $database . "' were found.", Console::BOLD_RED + )); + } else if (is_numeric($id)) { + if (!in_array($id, $ids)) { + $this->console->write($this->console->colorize( + "The migration '" . $id . "' for the database '" . $database . "' does not exist.", Console::BOLD_RED + )); + } else { + file_put_contents($location . '/database/migrations/' . $database . '/.current', $id); + $this->console->write(); + $this->console->write('Done!'); + } + } else if ($id == 'latest') { + $id = end($ids); + file_put_contents($location . '/database/migrations/' . $database . '/.current', $id); + $this->console->write(); + $this->console->write('Done!'); + } + } + } + /** * Reset command *