Skip to content

Commit c49b512

Browse files
committed
Proof of concept for automatically updating Doctrine Schema from Messenger
1 parent c30d6f9 commit c49b512

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

src/Symfony/Component/Messenger/Bridge/Doctrine/Transport/Connection.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Doctrine\DBAL\Schema\Schema;
2020
use Doctrine\DBAL\Schema\Synchronizer\SchemaSynchronizer;
2121
use Doctrine\DBAL\Schema\Synchronizer\SingleDatabaseSynchronizer;
22+
use Doctrine\DBAL\Schema\Table;
2223
use Doctrine\DBAL\Types\Type;
2324
use Doctrine\DBAL\Types\Types;
2425
use Symfony\Component\Messenger\Exception\InvalidArgumentException;
@@ -290,6 +291,23 @@ public function find($id): ?array
290291
return false === $data ? null : $this->decodeEnvelopeHeaders($data);
291292
}
292293

294+
public function configureSchema(Schema $schema): void
295+
{
296+
if ($schema->hasTable($this->configuration['table_name'])) {
297+
return;
298+
}
299+
300+
$this->addTableToSchema($schema);
301+
}
302+
303+
/**
304+
* @internal
305+
*/
306+
public function getDbalConnection(): DBALConnection
307+
{
308+
return $this->driverConnection;
309+
}
310+
293311
private function createAvailableMessagesQueryBuilder(): QueryBuilder
294312
{
295313
$now = new \DateTime();
@@ -341,6 +359,13 @@ private function executeQuery(string $sql, array $parameters = [], array $types
341359
private function getSchema(): Schema
342360
{
343361
$schema = new Schema([], [], $this->driverConnection->getSchemaManager()->createSchemaConfig());
362+
$this->addTableToSchema($schema);
363+
364+
return $schema;
365+
}
366+
367+
private function addTableToSchema(Schema $schema): void
368+
{
344369
$table = $schema->createTable($this->configuration['table_name']);
345370
$table->addColumn('id', self::$useDeprecatedConstants ? Type::BIGINT : Types::BIGINT)
346371
->setAutoincrement(true)
@@ -361,8 +386,6 @@ private function getSchema(): Schema
361386
$table->addIndex(['queue_name']);
362387
$table->addIndex(['available_at']);
363388
$table->addIndex(['delivered_at']);
364-
365-
return $schema;
366389
}
367390

368391
private function decodeEnvelopeHeaders(array $doctrineEnvelope): array

src/Symfony/Component/Messenger/Bridge/Doctrine/Transport/DoctrineTransport.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Messenger\Bridge\Doctrine\Transport;
1313

14+
use Doctrine\DBAL\Connection as DbalConnection;
15+
use Doctrine\DBAL\Schema\Schema;
1416
use Symfony\Component\Messenger\Envelope;
1517
use Symfony\Component\Messenger\Transport\Receiver\ListableReceiverInterface;
1618
use Symfony\Component\Messenger\Transport\Receiver\MessageCountAwareInterface;
@@ -98,6 +100,16 @@ public function setup(): void
98100
$this->connection->setup();
99101
}
100102

103+
public function configureSchema(Schema $schema, DbalConnection $dbalConnection): void
104+
{
105+
// only update the schema for the correct connection
106+
if ($dbalConnection !== $this->connection->getDbalConnection()) {
107+
return;
108+
}
109+
110+
$this->connection->configureSchema($schema);
111+
}
112+
101113
private function getReceiver(): DoctrineReceiver
102114
{
103115
return $this->receiver = new DoctrineReceiver($this->connection, $this->serializer);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Bridge\Doctrine\Transport;
13+
14+
use Doctrine\Common\EventSubscriber;
15+
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;
16+
use Doctrine\ORM\Tools\ToolEvents;
17+
use Symfony\Component\Messenger\Transport\TransportInterface;
18+
19+
class MessengerTransportDoctrineSchemaSubscriber implements EventSubscriber
20+
{
21+
private $transports;
22+
23+
/**
24+
* @param iterable|TransportInterface[] $transports
25+
*/
26+
public function __construct(iterable $transports)
27+
{
28+
$this->transports = $transports;
29+
}
30+
31+
public function postGenerateSchema(GenerateSchemaEventArgs $event)
32+
{
33+
$dbalConnection = $event->getEntityManager()->getConnection();
34+
foreach ($this->transports as $transport) {
35+
if (!$transport instanceof DoctrineTransport) {
36+
continue;
37+
}
38+
39+
$transport->configureSchema($event->getSchema(), $dbalConnection);
40+
}
41+
}
42+
43+
public function getSubscribedEvents()
44+
{
45+
return [ToolEvents::postGenerateSchema];
46+
}
47+
}

0 commit comments

Comments
 (0)