Skip to content

Commit

Permalink
[EntityMigrator/SonataIntegrationBundle] Refactor entity migrator
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoiriert committed Sep 30, 2024
1 parent 542e766 commit 82dffeb
Show file tree
Hide file tree
Showing 28 changed files with 656 additions and 500 deletions.
6 changes: 3 additions & 3 deletions app/src/EntityMigration/UserSetCommentNullMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;
use Draw\Component\EntityMigrator\BatchPrepareMigrationInterface;
use Draw\Component\EntityMigrator\MigrationInterface;
use Draw\Component\EntityMigrator\MigrationTargetEntityInterface;

/**
* @template-implements BatchPrepareMigrationInterface<User>
* @template-implements MigrationInterface<User>
*/
class UserSetCommentNullMigration implements BatchPrepareMigrationInterface
class UserSetCommentNullMigration implements MigrationInterface
{
public static function getName(): string
{
Expand Down
15 changes: 0 additions & 15 deletions packages/entity-migrator/BatchPrepareMigrationInterface.php

This file was deleted.

97 changes: 0 additions & 97 deletions packages/entity-migrator/Command/BaseCommand.php

This file was deleted.

90 changes: 50 additions & 40 deletions packages/entity-migrator/Command/MigrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,84 +2,94 @@

namespace Draw\Component\EntityMigrator\Command;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use Draw\Component\EntityMigrator\Entity\Migration;
use Draw\Component\EntityMigrator\Workflow\MigrationWorkflow;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Workflow\Registry;

#[AsCommand(
name: 'draw:entity-migrator:migrate',
description: 'Migrate all entities',
)]
class MigrateCommand extends BaseCommand
class MigrateCommand extends Command
{
public function __construct(
private Registry $workflowRegistry,
private ManagerRegistry $managerRegistry,
) {
parent::__construct();
}

protected function configure(): void
{
$this
->addArgument('migration-name', null, 'The migration name to migrate')
;
}

protected function interact(InputInterface $input, OutputInterface $output): void
{
$io = new SymfonyStyle($input, $output);

if (!$input->getArgument('migration-name')) {
$io->block(
'Which migration ?',
null,
'fg=white;bg=blue',
' ',
true
);

$question = new ChoiceQuestion(
'Select which migration',
array_map(
static fn (Migration $migration) => $migration->getName(),
$this->managerRegistry->getRepository(Migration::class)->findAll()
)
);

$input->setArgument('migration-name', $io->askQuestion($question));
}
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle(
$input,
$output
);

$migration = $this->migrator->getMigration($this->getMigrationName($input, $output));

$count = $migration->countAllThatNeedMigration();

if (0 === $count) {
$io->warning('No entity need migration');

return Command::SUCCESS;
}

$migrationEntity = $this->managerRegistry
->getRepository(Migration::class)
->findOneBy(['name' => $migration::getName()])
->findOneBy(['name' => $input->getArgument('migration-name')])
;

$manager = $this->managerRegistry->getManagerForClass(Migration::class);
$workflow = $this->workflowRegistry->get($migrationEntity, MigrationWorkflow::NAME);

\assert($manager instanceof EntityManagerInterface);

$realCount = 0;

$progress = $io->createProgressBar($count ?? 0);
$progress->setFormat(ProgressBar::FORMAT_DEBUG);
foreach ($migration->findAllThatNeedMigration() as $entity) {
$entityMigration = $this->entityMigrationRepository->load(
$entity,
$manager->getReference(Migration::class, $migrationEntity->getId())
);
$blockerList = $workflow->buildTransitionBlockerList($migrationEntity, MigrationWorkflow::TRANSITION_PROCESS);

$this->migrator->migrate($entityMigration);
if (!$blockerList->isEmpty()) {
foreach ($blockerList as $blocker) {
$io->warning('Process is blocked: '.$blocker->getMessage());
}

++$realCount;

$progress->advance();

$manager->clear();

$this->servicesResetter?->reset();
return Command::FAILURE;
}

$progress->finish();
$progressBar = $io->createProgressBar();
$progressBar->setFormat(ProgressBar::FORMAT_DEBUG);

$io->newLine();
$workflow->apply($migrationEntity, MigrationWorkflow::TRANSITION_PROCESS, ['progressBar' => $progressBar]);

$io->success(\sprintf(
'Migration %s processed for %d entities',
$migration::getName(),
$realCount
));
$io->newLine();
$io->success('Migration started');

return Command::SUCCESS;
}
Expand Down
Loading

0 comments on commit 82dffeb

Please sign in to comment.