eee
This commit is contained in:
@@ -26,6 +26,7 @@ class DatabasesProviders extends Providers
|
|||||||
{
|
{
|
||||||
$main = Kiri::getDi()->get(Kiri\Application::class);
|
$main = Kiri::getDi()->get(Kiri\Application::class);
|
||||||
$main->command(BackupCommand::class);
|
$main->command(BackupCommand::class);
|
||||||
|
$main->command(ImplodeCommand::class);
|
||||||
|
|
||||||
$databases = \config('databases.connections', []);
|
$databases = \config('databases.connections', []);
|
||||||
if (empty($databases)) {
|
if (empty($databases)) {
|
||||||
|
|||||||
@@ -0,0 +1,97 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Database;
|
||||||
|
|
||||||
|
use Co\Channel;
|
||||||
|
use Exception;
|
||||||
|
use Kiri\Di\LocalService;
|
||||||
|
use Swoole\Coroutine;
|
||||||
|
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 ImplodeCommand extends Command
|
||||||
|
{
|
||||||
|
|
||||||
|
public string $command = 'db:implode';
|
||||||
|
|
||||||
|
|
||||||
|
public string $description = 'php kiri.php db:implode --database users --table u_user /Users/admin/snowflake-bi/test.sql';
|
||||||
|
|
||||||
|
|
||||||
|
private LocalService $service;
|
||||||
|
|
||||||
|
public array $percentStatus = [];
|
||||||
|
|
||||||
|
protected Channel $channel;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
protected function configure()
|
||||||
|
{
|
||||||
|
$this->service = \Kiri::getDi()->get(LocalService::class);
|
||||||
|
$this->setName('db:backup')
|
||||||
|
->addOption('data', 'd', InputArgument::OPTIONAL)
|
||||||
|
->addArgument('path', InputArgument::REQUIRED, "save to path", null)
|
||||||
|
->addOption('database', 'db', InputArgument::OPTIONAL)
|
||||||
|
->setDescription('php kiri.php db:backup --database users --table u_user --data 1 /Users/admin/snowflake-bi/test.sql');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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'));
|
||||||
|
|
||||||
|
$path = $input->getArgument('path');
|
||||||
|
if (!str_starts_with($path, '/')) {
|
||||||
|
$path = APP_PATH . $path;
|
||||||
|
}
|
||||||
|
|
||||||
|
$waite = new Coroutine\WaitGroup();
|
||||||
|
|
||||||
|
|
||||||
|
$stream = fopen($path, 'r');
|
||||||
|
if (!$stream) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
while (($line = fgets($stream)) !== false) {
|
||||||
|
$insert = html_entity_decode($line);
|
||||||
|
if (!str_starts_with(strtoupper($insert), 'INSERT INTO')) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Coroutine::create(function () use ($waite, $insert, $data) {
|
||||||
|
Coroutine\defer(fn() => $waite->done());
|
||||||
|
$exec = $data->createCommand($insert)->exec();
|
||||||
|
exit('exec sql result: ' . $exec);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
fclose($stream);
|
||||||
|
|
||||||
|
|
||||||
|
$waite->wait();
|
||||||
|
|
||||||
|
$output->write('dump data success');
|
||||||
|
} catch (\Throwable $throwable) {
|
||||||
|
$output->writeln(throwable($throwable));
|
||||||
|
} finally {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user