Database/src/Cli/Migrations/MigrateOneCommand.php

44 lines
1.4 KiB
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 Nest\Database\Exceptions\Query\MissingConditionValueException;
use Nest\Database\Exceptions\UnknownDatabaseException;
use Nest\Model\Exceptions\MissingRequiredFieldException;
use Nest\Types\Exceptions\IncompatibleTypeException;
use Throwable;
class MigrateOneCommand extends CommandHandler
{
/**
* @throws Throwable
* @throws MigrationNotFoundException
* @throws IncompatibleTypeException
* @throws UnknownDatabaseException
* @throws MissingConditionValueException
* @throws MissingRequiredFieldException
*/
public function __invoke(string $migrationId): void
{
Application::get()->migrations()->onMigrationStart = function (Migration $migration) {
Out::print("Executing migration V{$migration->getVersion()} ".Out::BOLD_ATTRIBUTE."{$migration->getName()}".Out::RESET_ATTRIBUTES);
echo " ".Out::ITALIC_ATTRIBUTE.$migration->getDescription().Out::RESET_ATTRIBUTES."\n\n";
};
Application::get()->migrations()->onMigrationEnd = function (Migration $migration) {
echo "\n";
Out::print("V{$migration->getVersion()}_{$migration->getName()} finished.");
echo "\n";
};
// Try to execute given migration.
Application::get()->migrations()->load()->migrateOne($migrationId);
Out::success("Success.");
}
}