Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entity migration improvement #297

Merged
merged 5 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@
"symfony/validator": "^6.4.0",
"symfony/workflow": "^6.4.0",
"symfony/yaml": "^6.4.0",
"twig/twig": "^3.3"
"twig/twig": "^3.3",
"yokai/sonata-workflow": "^0.7.0"
},
"require-dev": {
"aws/aws-sdk-php-symfony": "^2.2",
Expand Down
66 changes: 59 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 0 additions & 15 deletions packages/entity-migrator/BatchPrepareMigrationInterface.php

This file was deleted.

51 changes: 0 additions & 51 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($input->getArgument('migration-name'));

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

$count = $migration->countAllThatNeedMigration();
$workflow = $this->workflowRegistry->get($migrationEntity, MigrationWorkflow::NAME);

if (0 === $count) {
$io->warning('No entity need migration');
$blockerList = $workflow->buildTransitionBlockerList($migrationEntity, MigrationWorkflow::TRANSITION_PROCESS);

return Command::SUCCESS;
}
if (!$blockerList->isEmpty()) {
foreach ($blockerList as $blocker) {
$io->warning('Process is blocked: '.$blocker->getMessage());
}

$manager = $this->managerRegistry->getManagerForClass(Migration::class);

\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())
);

$this->migrator->migrate($entityMigration);

++$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
Loading