Skip to content

Commit

Permalink
[SonataIntegrationBundle] Integrate entity-migrator
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoiriert committed Oct 25, 2023
1 parent 41ce41b commit 45a632c
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 3 deletions.
19 changes: 19 additions & 0 deletions app/src/Sonata/Admin/UserMigrationAdmin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Sonata\Admin;

use App\Entity\UserMigration;
use Draw\Bundle\SonataIntegrationBundle\EntityMigrator\Admin\BaseEntityMigrationAdmin;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;

#[AutoconfigureTag(
name: 'sonata.admin',
attributes: [
...BaseEntityMigrationAdmin::ADMIN,
'model_class' => UserMigration::class,
'label' => 'User Migration',
]
)]
class UserMigrationAdmin extends BaseEntityMigrationAdmin
{
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@
"php bin/console messenger:setup-transports --no-interaction",
"php bin/console doctrine:migrations:migrate --no-interaction --allow-no-migration --quiet",
"php bin/console doctrine:fixtures:load --no-interaction",
"php bin/console draw:entity-migrator:setup --no-interaction",
"php bin/console draw:application:update-deployed-version --no-interaction"
],
"linter": [
Expand Down
2 changes: 2 additions & 0 deletions config/packages/draw_sonata_integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ draw_sonata_integration:
commandName: 'app:null'
label: 'Null'

entity_migrator: ~

messenger:
queue_names: ['default', 'failed']

Expand Down
12 changes: 11 additions & 1 deletion packages/entity-migrator/Entity/BaseEntityMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#[
ORM\UniqueConstraint(name: 'entity_migration', fields: ['entity', 'migration'])
]
abstract class BaseEntityMigration implements EntityMigrationInterface
abstract class BaseEntityMigration implements EntityMigrationInterface, \Stringable
{
#[
ORM\Id,
Expand Down Expand Up @@ -48,6 +48,11 @@ public function __construct(MigrationTargetEntityInterface $entity, Migration $m
$this->createdAt = new \DateTimeImmutable();
}

public function getId(): ?int
{
return $this->id;
}

public function getEntity(): MigrationTargetEntityInterface
{
return $this->entity;
Expand Down Expand Up @@ -100,4 +105,9 @@ public function getTransitionLogs(): array
{
return $this->transitionLogs;
}

public function __toString(): string
{
return $this->migration.' --> '.$this->entity;
}
}
7 changes: 6 additions & 1 deletion packages/entity-migrator/Entity/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
ORM\Table(name: 'draw_entity_migrator__migration'),
ORM\UniqueConstraint(name: 'name', columns: ['name'])
]
class Migration
class Migration implements \Stringable
{
#[
ORM\Id,
Expand Down Expand Up @@ -68,4 +68,9 @@ public function isPaused(): bool
{
return 'paused' === $this->state;
}

public function __toString(): string
{
return $this->name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Draw\Component\EntityMigrator\Entity\BaseEntityMigration;

interface MigrationTargetEntityInterface
interface MigrationTargetEntityInterface extends \Stringable
{
/**
* @return class-string<BaseEntityMigration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Draw\Bundle\UserBundle\Entity\UserLock;
use Draw\Component\Application\Configuration\Entity\Config;
use Draw\Component\Console\Entity\Execution;
use Draw\Component\EntityMigrator\Entity\Migration;
use Draw\Component\Messenger\Broker\Broker;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
Expand All @@ -26,6 +27,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->children()
->append($this->createConfigurationNode())
->append($this->createConsoleNode())
->append($this->createEntityMigratorNode())
->append($this->createMessengerNode())
->append($this->createUserNode())
->end();
Expand Down Expand Up @@ -81,6 +83,18 @@ private function createConsoleNode(): ArrayNodeDefinition
->end();
}

private function createEntityMigratorNode(): ArrayNodeDefinition
{
return (new ArrayNodeDefinition('entity_migrator'))
->canBeEnabled()
->append(
(new SonataAdminNodeConfiguration(Migration::class, 'Entity Migrator', 'admin'))
->addDefaultsIfNotSet()
->labelDefaultValue('Migration')
->iconDefaultValue('fa fa-cogs')
);
}

private function createMessengerNode(): ArrayNodeDefinition
{
return $this->canBe(Broker::class, new ArrayNodeDefinition('messenger'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Draw\Bundle\SonataIntegrationBundle\Console\Admin\ExecutionAdmin;
use Draw\Bundle\SonataIntegrationBundle\Console\Command;
use Draw\Bundle\SonataIntegrationBundle\Console\CommandRegistry;
use Draw\Bundle\SonataIntegrationBundle\EntityMigrator\Admin\MigrationAdmin;
use Draw\Bundle\SonataIntegrationBundle\Messenger\Admin\MessengerMessageAdmin;
use Draw\Bundle\SonataIntegrationBundle\Messenger\EventListener\FinalizeContextQueueCountEventListener;
use Draw\Bundle\SonataIntegrationBundle\Messenger\Security\CanShowMessageVoter;
Expand Down Expand Up @@ -43,6 +44,7 @@ public function load(array $configs, ContainerBuilder $container): void

$this->configureConfiguration($config['configuration'], $loader, $container);
$this->configureConsole($config['console'], $loader, $container);
$this->configureEntityMigrator($config['entity_migrator'], $loader, $container);
$this->configureMessenger($config['messenger'], $loader, $container);
$this->configureUser($config['user'], $loader, $container);
}
Expand Down Expand Up @@ -161,6 +163,24 @@ private function configureMessenger(array $config, Loader\FileLoader $loader, Co
}
}

private function configureEntityMigrator(array $config, Loader\FileLoader $loader, ContainerBuilder $container): void
{
if (!$config['enabled']) {
return;
}

$container
->setDefinition(
MigrationAdmin::class,
SonataAdminNodeConfiguration::configureFromConfiguration(
new Definition(MigrationAdmin::class),
$config['admin']
)
)
->setAutowired(true)
->setAutoconfigured(true);
}

private function configureUser(array $config, Loader\FileLoader $loader, ContainerBuilder $container): void
{
if (!$config['enabled']) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Draw\Bundle\SonataIntegrationBundle\EntityMigrator\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Route\RouteCollectionInterface;
use Sonata\AdminBundle\Show\ShowMapper;

abstract class BaseEntityMigrationAdmin extends AbstractAdmin
{
public const ADMIN = [
'manager_type' => 'orm',
'pager_type' => 'simple',
'group' => 'Entity Migrator',
'icon' => 'fas fa-cogs',
];

protected function configureListFields(ListMapper $list): void
{
$list
->addIdentifier('id')
->add('entity')
->add('migration')
->add('state');
}

protected function configureShowFields(ShowMapper $show): void
{
$show
->add('id')
->add('entity')
->add('migration')
->add('state')
->add(
'transitionLogs',
fieldDescriptionOptions: [
'template' => '@DrawSonataIntegration/EntityMigrator/BaseEntityMigration/show_transition_logs.html.twig',
]
)
->add('createdAt');
}

protected function configureRoutes(RouteCollectionInterface $collection): void
{
$collection
->remove('edit');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Draw\Bundle\SonataIntegrationBundle\EntityMigrator\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Route\RouteCollectionInterface;
use Sonata\AdminBundle\Show\ShowMapper;

class MigrationAdmin extends AbstractAdmin
{
protected function configureListFields(ListMapper $list): void
{
$list
->addIdentifier('id')
->add('name')
->add('state');
}

protected function configureShowFields(ShowMapper $show): void
{
$show
->add('id')
->add('name')
->add('state');
}

protected function configureRoutes(RouteCollectionInterface $collection): void
{
$collection
->remove('edit')
->remove('create')
->remove('delete');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{% extends '@SonataAdmin/CRUD/base_show_field.html.twig' %}

{% block field %}
<table class="table">
<thead>
<tr>
<th>Created By</th>
<th>From</th>
<th>Transition</th>
<th>To</th>
<th>Created At</th>
<th>Delay</th>
</tr>
</thead>
<tbody>
{% for entry in value %}
<tr>
<td>{{ entry['createdBy'] }}</td>
<td>{{ entry['from'] }}</td>
<td>{{ entry['transition'] }}</td>
<td>{{ entry['to'] }}</td>
<td>{{ entry['createdAt']|date }}</td>
<td>
{% if previousCreatedAt is defined %}
{{ entry['createdAt']|date('U') - previousCreatedAt|date('U') }} seconds
{% endif %}
</td>
</tr>
{% set previousCreatedAt = entry['createdAt'] %}
{% endfor %}
</tbody>
</table>
{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Draw\Bundle\UserBundle\Entity\UserLock;
use Draw\Component\Application\Configuration\Entity\Config;
use Draw\Component\Console\Entity\Execution;
use Draw\Component\EntityMigrator\Entity\Migration;
use Draw\Component\Tester\DependencyInjection\ConfigurationTestCase;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Symfony\Component\Config\Definition\ConfigurationInterface;
Expand Down Expand Up @@ -49,6 +50,18 @@ public function getDefaultConfiguration(): array
],
'commands' => [],
],
'entity_migrator' => [
'enabled' => false,
'admin' => [
'group' => 'Entity Migrator',
'entity_class' => Migration::class,
'controller_class' => 'sonata.admin.controller.crud',
'icon' => 'fa fa-cogs',
'label' => 'Migration',
'pager_type' => 'default',
'show_in_dashboard' => true,
],
],
'messenger' => [
'enabled' => true,
'queue_names' => [],
Expand Down

0 comments on commit 45a632c

Please sign in to comment.