Skip to content

Commit

Permalink
[DoctrineExtra] fix mysql import file by using mysql directly
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoiriert committed Jan 18, 2024
1 parent 0f6da77 commit 364b7ae
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,13 @@
"test:database:setup": [
"bin/console doctrine:database:drop --if-exists --no-interaction --force --env=test",
"bin/console doctrine:database:create --no-interaction --env=test",
"bin/console draw:doctrine:import-file ./data/sql/dump.sql --no-interaction --env=test",
"bin/console draw:doctrine:mysql-import-file ./data/sql/dump.sql --no-interaction --env=test",
"bin/console messenger:setup-transports --no-interaction --env=test",
"bin/console doctrine:migrations:migrate --no-interaction --allow-no-migration --quiet --env=test"
],
"test:database:setup-dump": [
"@test:database:setup",
"bin/console draw:doctrine:import-file ./data/sql/truncate.sql --no-interaction --env=test -vvv",
"bin/console draw:doctrine:mysql-import-file ./data/sql/truncate.sql --no-interaction --env=test -vvv",
"bin/console draw:doctrine:mysql-dump ./data/sql/dump.sql --no-interaction --env=test"
],
"linter": [
Expand Down
12 changes: 6 additions & 6 deletions packages/doctrine-extra/ORM/Command/MysqlDumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
$result = $this->ormManagerRegistry->getConnection($input->getOption('connection'))
$connectionParameter = $this->ormManagerRegistry->getConnection($input->getOption('connection'))
->getParams()['primary'];

Process::fromShellCommandline(sprintf(
'mysqldump -h %s -P %s -u %s %s %s > %s',
$result['host'],
$result['port'],
$result['user'],
null === $result['password'] ? '' : '-p'.$result['password'],
$result['dbname'],
$connectionParameter['host'],
$connectionParameter['port'],
$connectionParameter['user'],
null === $connectionParameter['password'] ? '' : '-p'.$connectionParameter['password'],
$connectionParameter['dbname'],
$input->getArgument('file')
))->mustRun();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;

class ImportFileCommand extends Command
class MysqlImportFileCommand extends Command
{
public function __construct(private ManagerRegistry $ormManagerRegistry)
{
parent::__construct('draw:doctrine:import-file');
parent::__construct('draw:doctrine:mysql-import-file');
}

protected function configure(): void
Expand All @@ -26,10 +27,20 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
$connection = $this->ormManagerRegistry->getConnection($input->getOption('connection'));
$connectionParameter = $this->ormManagerRegistry
->getConnection($input->getOption('connection'))
->getParams()['primary'];

foreach ($input->getArgument('files') as $file) {
$connection->executeQuery(file_get_contents($file));
Process::fromShellCommandline(sprintf(
'mysql -h %s -P %s -u %s %s %s < %s',
$connectionParameter['host'],
$connectionParameter['port'],
$connectionParameter['user'],
null === $connectionParameter['password'] ? '' : '-p'.$connectionParameter['password'],
$connectionParameter['dbname'],
$file
))->mustRun();
}

return Command::SUCCESS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use Doctrine\ORM\EntityManagerInterface;
use Draw\Bundle\FrameworkExtraBundle\DependencyInjection\Integration\DoctrineExtraIntegration;
use Draw\Bundle\FrameworkExtraBundle\DependencyInjection\Integration\IntegrationInterface;
use Draw\DoctrineExtra\ORM\Command\ImportFileCommand;
use Draw\DoctrineExtra\ORM\Command\MysqlDumpCommand;
use Draw\DoctrineExtra\ORM\Command\MysqlImportFileCommand;
use Draw\DoctrineExtra\ORM\EntityHandler;
use PHPUnit\Framework\Attributes\CoversClass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -61,9 +61,9 @@ public static function provideTestLoad(): iterable
]
),
new ServiceConfiguration(
'draw.doctrine_extra.orm.command.import_file_command',
'draw.doctrine_extra.orm.command.mysql_import_file_command',
[
ImportFileCommand::class,
MysqlImportFileCommand::class,
]
),
new ServiceConfiguration(
Expand Down

0 comments on commit 364b7ae

Please sign in to comment.