diff --git a/app/migrations/Version20241025145126.php b/app/migrations/Version20241025145126.php new file mode 100644 index 00000000..26f8612c --- /dev/null +++ b/app/migrations/Version20241025145126.php @@ -0,0 +1,29 @@ +addSql('CREATE INDEX draw_migration_sate ON user_migration (migration_id, state)'); + } + + public function down(Schema $schema): void + { + } +} diff --git a/app/src/EntityMigration/UserSetCommentNullMigration.php b/app/src/EntityMigration/UserSetCommentNullMigration.php index 39b82300..8d097262 100644 --- a/app/src/EntityMigration/UserSetCommentNullMigration.php +++ b/app/src/EntityMigration/UserSetCommentNullMigration.php @@ -4,7 +4,6 @@ use App\Entity\User; use Doctrine\ORM\EntityManagerInterface; -use Doctrine\ORM\EntityRepository; use Doctrine\ORM\QueryBuilder; use Doctrine\Persistence\ManagerRegistry; use Draw\Component\EntityMigrator\MigrationInterface; @@ -39,20 +38,6 @@ public function needMigration(MigrationTargetEntityInterface $entity): bool return '' !== $entity->getComment(); } - public function findAllThatNeedMigration(): iterable - { - $manager = $this->managerRegistry->getManagerForClass(User::class); - \assert($manager instanceof EntityManagerInterface); - - $query = $manager - ->createQuery('SELECT user.id FROM '.User::class.' user WHERE user.comment != :comment') - ; - - foreach ($query->toIterable(['comment' => ''], $query::HYDRATE_SCALAR) as $userId) { - yield $manager->getReference(User::class, $userId['id']); - } - } - public function countAllThatNeedMigration(): ?int { $manager = $this->managerRegistry->getManagerForClass(User::class); @@ -65,21 +50,6 @@ public function countAllThatNeedMigration(): ?int ; } - public function migrationIsCompleted(): bool - { - $repository = $this->managerRegistry->getRepository(User::class); - \assert($repository instanceof EntityRepository); - - return null === $repository - ->createQueryBuilder('user') - ->select('user.id') - ->where('user.comment != ""') - ->setMaxResults(1) - ->getQuery() - ->getOneOrNullResult() - ; - } - public function createSelectIdQueryBuilder(): QueryBuilder { $manager = $this->managerRegistry->getManagerForClass(User::class); diff --git a/packages/entity-migrator/EventListener/DoctrineSchemaListener.php b/packages/entity-migrator/EventListener/DoctrineSchemaListener.php new file mode 100644 index 00000000..65174e32 --- /dev/null +++ b/packages/entity-migrator/EventListener/DoctrineSchemaListener.php @@ -0,0 +1,34 @@ + 'loadClassMetadata', + ] +)] +class DoctrineSchemaListener +{ + public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void + { + $metadata = $eventArgs->getClassMetadata(); + + if (!$metadata instanceof ClassMetadataInfo) { + return; + } + + if (!\in_array(EntityMigrationInterface::class, class_implements($metadata->getName()), true)) { + return; + } + + $metadata->table['indexes']['draw_migration_sate'] = [ + 'columns' => ['migration_id', 'state'], + ]; + } +} diff --git a/packages/entity-migrator/EventListener/MigrationWorkflowListener.php b/packages/entity-migrator/EventListener/MigrationWorkflowListener.php index 495fcbc4..28a3aa0f 100644 --- a/packages/entity-migrator/EventListener/MigrationWorkflowListener.php +++ b/packages/entity-migrator/EventListener/MigrationWorkflowListener.php @@ -15,10 +15,8 @@ use Symfony\Component\Workflow\Attribute\AsCompletedListener; use Symfony\Component\Workflow\Attribute\AsEnteredListener; use Symfony\Component\Workflow\Attribute\AsGuardListener; -use Symfony\Component\Workflow\Attribute\AsTransitionListener; use Symfony\Component\Workflow\Event\EnteredEvent; use Symfony\Component\Workflow\Event\GuardEvent; -use Symfony\Component\Workflow\Event\TransitionEvent; class MigrationWorkflowListener { @@ -104,22 +102,6 @@ private function gotOneNotInState( ; } - #[AsTransitionListener(MigrationWorkflow::NAME, MigrationWorkflow::TRANSITION_PROCESS)] - public function prepareProcess(TransitionEvent $event): void - { - $subject = $event->getSubject(); - - \assert($subject instanceof Migration); - - $migration = $this->migrator->getMigration($subject->getName()); - - $context = $event->getContext(); - - $context['total'] = $migration->countAllThatNeedMigration(); - - $event->setContext($context); - } - #[AsCompletedListener(MigrationWorkflow::NAME)] public function flush(): void { diff --git a/packages/entity-migrator/MigrationInterface.php b/packages/entity-migrator/MigrationInterface.php index 5a473e71..fe7c66eb 100644 --- a/packages/entity-migrator/MigrationInterface.php +++ b/packages/entity-migrator/MigrationInterface.php @@ -26,22 +26,5 @@ public function migrate(MigrationTargetEntityInterface $entity): void; */ public function needMigration(MigrationTargetEntityInterface $entity): bool; - /** - * Return all entity that need migration. A migrate command will be sent to queue for each of them. - * - * @return iterable - */ - public function findAllThatNeedMigration(): iterable; - - /** - * Return the number of entities that need migration or null if unknown. - */ - public function countAllThatNeedMigration(): ?int; - - /** - * Return a boolean to indicate that no more entities need migration. - */ - public function migrationIsCompleted(): bool; - public function createSelectIdQueryBuilder(): QueryBuilder; } diff --git a/tests/fixtures/AppKernelTest/testEventDispatcherConfiguration/event_dispatcher.xml b/tests/fixtures/AppKernelTest/testEventDispatcherConfiguration/event_dispatcher.xml index 833af276..39ddef51 100644 --- a/tests/fixtures/AppKernelTest/testEventDispatcherConfiguration/event_dispatcher.xml +++ b/tests/fixtures/AppKernelTest/testEventDispatcherConfiguration/event_dispatcher.xml @@ -287,9 +287,6 @@ - - -