Skip to content

Commit

Permalink
Add migrate:point route
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksagona committed Jun 26, 2023
1 parent f4aea4f commit f1d5db6
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ by using the `db` and `migrate` commands. If you don't pass anything in the opti
./kettle migrate:create <class> [<database>] Create new database migration class
./kettle migrate:run [<steps>] [<database>] Perform forward database migration
./kettle migrate:rollback [<steps>] [<database>] Perform backward database migration
./kettle migrate:point [<id>] [<database>] Point current to a specific migration, without running
./kettle migrate:reset [<database>] Perform complete rollback of the database
```

Expand Down
5 changes: 5 additions & 0 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@
'action' => 'rollback',
'help' => 'Perform backward database migration'
],
'migrate:point [<id>] [<database>]' => [
'controller' => 'Pop\Kettle\Controller\MigrationController',
'action' => 'point',
'help' => 'Point current to a specific migration, without running'
],
'migrate:reset [<database>]' => [
'controller' => 'Pop\Kettle\Controller\MigrationController',
'action' => 'reset',
Expand Down
60 changes: 60 additions & 0 deletions src/Controller/MigrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down

0 comments on commit f1d5db6

Please sign in to comment.