-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d2aa12a
commit c54caf0
Showing
20 changed files
with
314 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,18 @@ | ||
#!env php | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
use EcomDev\MySQL2JSONL\Command\ExportCommand; | ||
use Symfony\Component\Console\Application; | ||
|
||
require_once __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$application = new Application( | ||
basename(__FILE__), | ||
'1.0.0' | ||
); | ||
|
||
$application->addCommands([ | ||
new ExportCommand() | ||
]); | ||
|
||
$application->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
namespace EcomDev\MySQL2JSONL\Command; | ||
|
||
use Amp\Pipeline\Queue; | ||
use EcomDev\MySQL2JSONL\Configuration; | ||
use EcomDev\MySQL2JSONL\ConfigurationException; | ||
use EcomDev\MySQL2JSONL\ExportTableFactory; | ||
use EcomDev\MySQL2JSONL\Progress\ExportProgressNotifier; | ||
use Revolt\EventLoop; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Helper\FormatterHelper; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use function Amp\async; | ||
use function Amp\delay; | ||
use function Amp\Future\awaitAll; | ||
|
||
#[AsCommand(name: 'export', description: 'Export data from MySQL to a directory with JSONL files')] | ||
class ExportCommand extends Command | ||
{ | ||
public function configure() | ||
{ | ||
$this->addOption( | ||
'config', | ||
'c', InputOption::VALUE_REQUIRED, | ||
'Configuration file', | ||
'config.json' | ||
); | ||
|
||
$this->addArgument( | ||
'directory', | ||
InputArgument::OPTIONAL, | ||
'Directory to export data to', | ||
'./data-dump' | ||
); | ||
} | ||
|
||
public function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$file = $input->getOption('config'); | ||
if (!file_exists($file)) { | ||
/* @var FormatterHelper $formatter*/ | ||
$formatter = $this->getHelper('formatter'); | ||
$output->write($formatter->formatSection('Error', sprintf( | ||
'Configuration file %s does not exist', | ||
$file | ||
), 'error')); | ||
return 1; | ||
} | ||
try { | ||
$config = Configuration::fromJSON(file_get_contents($file)); | ||
} catch (ConfigurationException $error) { | ||
$error->output($output); | ||
return 1; | ||
} | ||
|
||
$notifiers = new ExportProgressNotifier($output); | ||
$futures = []; | ||
$connectionPool = $config->createConnectionPool(); | ||
$factory = new ExportTableFactory($connectionPool, $notifiers); | ||
foreach ($factory->tablesToExport($config) as $table) { | ||
$queue = new Queue(100); | ||
|
||
$futures[] = async(function () use ($factory, $table, $queue) { | ||
$factory->createExport($table)->run($queue); | ||
}); | ||
|
||
$futures[] = async(function () use ($queue, $output) { | ||
foreach ($queue->iterate() as $item) { | ||
} | ||
}); | ||
|
||
if (count($futures) >= 100) { | ||
awaitAll($futures); | ||
$futures = []; | ||
} | ||
} | ||
|
||
EventLoop::run(); | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,4 +34,4 @@ public function isSatisfiedBy(string $tableName, int $rows): bool | |
{ | ||
return $this->result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,4 +62,4 @@ public function isSatisfiedBy(string $tableName, int $rows): bool | |
default => false | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,4 +44,4 @@ public function generate(string $tableName, array $columns, int $rowCount, $onUp | |
|
||
return $sql; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.