This commit is contained in:
2023-04-11 18:02:44 +08:00
parent c64e05a81e
commit a489d465a7
2 changed files with 85 additions and 1 deletions
+78
View File
@@ -0,0 +1,78 @@
<?php
namespace Database;
use Exception;
use Kiri\Di\LocalService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
*
*/
class BackupCommand extends Command
{
public string $command = 'db:backup';
public string $description = './snowflake sw:gii make=model|controller|task|interceptor|limits|middleware name=xxxx';
private LocalService $service;
/**
*
*/
protected function configure()
{
$this->service = \Kiri::getDi()->get(LocalService::class);
$this->setName('db:backup')
->addOption('struct', 's', InputArgument::OPTIONAL)
->addOption('data', 'd', InputArgument::OPTIONAL)
->addOption('path', 'p', InputArgument::REQUIRED)
->addOption('table', 't', InputArgument::OPTIONAL)
->addOption('database', 'db', InputArgument::OPTIONAL)
->setDescription('php kiri.php sw:backup --struct 1 --database users --data 1');
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
* @throws Exception
*/
public function execute(InputInterface $input, OutputInterface $output): int
{
try {
/** @var Connection $data */
$data = $this->service->get($input->getOption('database'));
$table = $input->getOption('table');
if ($table !== null) {
$table = explode(',', $table);
} else {
$tmp = $data->createCommand('show tables from `users`')->all();
$table = [];
foreach ($tmp as $value) {
$table[] = current($value);
}
}
foreach ($table as $value) {
$tableInfo = $data->createCommand('show create table `' . $data->database . '`.`' . $value . '`')->one();
file_put_contents($input->getOption('path'), $tableInfo[$value], FILE_APPEND);
}
} catch (\Throwable $throwable) {
$output->writeln($throwable->getMessage());
} finally {
return 1;
}
}
}
+7 -1
View File
@@ -14,6 +14,8 @@ use Kiri\Events\EventProvider;
use Kiri\Annotation\Inject;
use Kiri\Server\Events\OnWorkerStart;
use Kiri\Server\Events\OnTaskerStart;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Log\LoggerInterface;
use Kiri\Server\Events\OnWorkerExit;
use Swoole\Timer;
@@ -38,11 +40,15 @@ class DatabasesProviders extends Providers
/**
* @param LocalService $application
* @return void
* @throws ConfigException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception
*/
public function onImport(LocalService $application): void
{
$main = Kiri::getDi()->get(Kiri\Main::class);
$main->command(BackupCommand::class);
$databases = Config::get('databases.connections', []);
if (empty($databases)) {
return;