Database/src/Cli/Migrations/RollbackCommand.php

34 lines
1,001 B
PHP
Raw Normal View History

2024-11-08 16:33:44 +01:00
<?php
namespace Nest\Cli\Commands\Migrations;
use Nest\Application;
use Nest\Cli\Commands\CommandHandler;
use Nest\Cli\Out;
use Nest\Database\Migrations\Migration;
use Nest\Database\Exceptions\Migrations\MigrationNotFoundException;
use Throwable;
class RollbackCommand extends CommandHandler
{
/**
* @throws MigrationNotFoundException
* @throws Throwable
*/
public function __invoke(): void
{
Application::get()->migrations()->onRollbackStart = function (Migration $migration) {
Out::print("Starting to roll back V{$migration->getVersion()} ".Out::BOLD_ATTRIBUTE."{$migration->getName()}".Out::RESET_ATTRIBUTES);
echo " ".Out::ITALIC_ATTRIBUTE.$migration->getDescription().Out::RESET_ATTRIBUTES."\n\n";
};
// Rollback latest migration.
$migration = Application::get()->migrations()->load()->rollbackLatest();
if (!empty($migration))
Out::success("Rolled back V{$migration->getVersion()} {$migration->getName()}");
else
Out::warning("Nothing to rollback.");
}
}