Skip to content

Commit

Permalink
[EntityMigrator] remove not needed Migration method, automatically cr…
Browse files Browse the repository at this point in the history
…eate index
  • Loading branch information
mpoiriert committed Oct 25, 2024
1 parent 88d4633 commit a2ec230
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 68 deletions.
29 changes: 29 additions & 0 deletions app/migrations/Version20241025145126.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20241025145126 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE INDEX draw_migration_sate ON user_migration (migration_id, state)');
}

public function down(Schema $schema): void
{
}
}
30 changes: 0 additions & 30 deletions app/src/EntityMigration/UserSetCommentNullMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
34 changes: 34 additions & 0 deletions packages/entity-migrator/EventListener/DoctrineSchemaListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Draw\Component\EntityMigrator\EventListener;

use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\Persistence\Event\LoadClassMetadataEventArgs;
use Draw\Component\EntityMigrator\Entity\EntityMigrationInterface;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;

#[AutoconfigureTag(
'doctrine.event_listener',
[
'event' => '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'],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down
17 changes: 0 additions & 17 deletions packages/entity-migrator/MigrationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>
*/
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,6 @@
<event name="workflow.entity_migrator_migration.guard.error">
<callable type="function" name="canError" class="Draw\Component\EntityMigrator\EventListener\MigrationWorkflowListener" priority="0"/>
</event>
<event name="workflow.entity_migrator_migration.transition.process">
<callable type="function" name="prepareProcess" class="Draw\Component\EntityMigrator\EventListener\MigrationWorkflowListener" priority="0"/>
</event>
<event name="workflow.transition">
<callable type="function" name="addTransitionToContext" class="Draw\Component\Workflow\EventListener\AddTransitionNameToContextListener" priority="0"/>
<callable type="function" name="addUserToContext" class="Draw\Component\Workflow\EventListener\AddUserToContextListener" priority="0"/>
Expand Down

0 comments on commit a2ec230

Please sign in to comment.