8000 Merge branch '4.4' into 5.3 · symfony/symfony@ca839f2 · GitHub
[go: up one dir, main page]

Skip to content

Commit < 8000 span class="text-mono bgColor-muted rounded p-1">ca839f2

Browse files
Merge branch '4.4' into 5.3
* 4.4: Add no-interaction for SYMFONY_PHPUNIT_REMOVE Remove indices in messenger table on MySQL to prevent deadlocks while removing messages when running multiple consumers
2 parents 85d3708 + d1eda26 commit ca839f2

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

src/Symfony/Component/Messenger/Bridge/Doctrine/Tests/Transport/ConnectionTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Messenger\Bridge\Doctrine\Tests\Transport;
1313

1414
use Doctrine\DBAL\Abstraction\Result as AbstractionResult;
15+
use Doctrine\DBAL\Configuration;
1516
use Doctrine\DBAL\Connection as DBALConnection;
1617
use Doctrine\DBAL\DBALException;
1718
use Doctrine\DBAL\Driver\Result as DriverResult;
@@ -25,7 +26,9 @@
2526
use Doctrine\DBAL\Schema\AbstractSchemaManager;
2627
use Doctrine\DBAL\Schema\Schema;
2728
use Doctrine\DBAL\Schema\SchemaConfig;
29+
use Doctrine\DBAL\Schema\TableDiff;
2830
use Doctrine\DBAL\Statement;
31+
use Doctrine\DBAL\Types\Types;
2932
use PHPUnit\Framework\TestCase;
3033
use Symfony\Component\Messenger\Bridge\Doctrine\Tests\Fixtures\DummyMessage;
3134
use Symfony\Component\Messenger\Bridge\Doctrine\Transport\Connection;
@@ -406,6 +409,56 @@ public function providePlatformSql(): iterable
406409
];
407410
}
408411

412+
/**
413+
* @dataProvider setupIndicesProvider
414+
*/
415+
public function testSetupIndices(string $platformClass, array $expectedIndices)
416+
{
417+
$driverConnection = $this->createMock(DBALConnection::class);
418+
$driverConnection->method('getConfiguration')->willReturn(new Configuration());
419+
420+
$schemaManager = $this->createMock(AbstractSchemaManager::class);
421+
$schema = new Schema();
422+
$expectedTable = $schema->createTable('messenger_messages');
423+
$expectedTable->addColumn('id', Types::BIGINT);
424+
$expectedTable->setPrimaryKey(['id']);
425+
// Make sure columns for indices exists so addIndex() will not throw
426+
foreach (array_unique(array_merge(...$expectedIndices)) as $columnName) {
427+
$expectedTable->addColumn($columnName, Types::STRING);
428+
}
429+
foreach ($expectedIndices as $indexColumns) {
430+
$expectedTable->addIndex($indexColumns);
431+
}
432+
$schemaManager->method('createSchema')->willReturn($schema);
433+
$driverConnection->method('getSchemaManager')->willReturn($schemaManager);
434+
435+
$platformMock = $this->createMock($platformClass);
436+
$platformMock
437+
->expects(self::once())
438+
->method('getAlterTableSQL')
439+
->with(self::callback(static function (TableDiff $tableDiff): bool {
440+
return 0 === \count($tableDiff->addedIndexes) && 0 === \count($tableDiff->changedIndexes) && 0 === \count($tableDiff->removedIndexes);
441+
}))
442+
->willReturn([]);
443+
$driverConnection->method('getDatabasePlatform')->willReturn($platformMock);
444+
445+
$connection = new Connection([], $driverConnection);
446+
$connection->setup();
447+
}
448+
449+
public function setupIndicesProvider(): iterable
450+
{
451+
yield 'MySQL' => [
452+
MySQL57Platform::class,
453+
[['delivered_at']],
454+
];
455+
456+
yield 'Other platforms' => [
457+
AbstractPlatform::class,
458+
[['queue_name'], ['available_at'], ['delivered_at']],
459+
];
460+
}
461+
409462
public function testConfigureSchema()
410463
{
411464
$driverConnection = $this->getDBALConnectionMock();

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Doctrine\DBAL\Exception;
1818
use Doctrine\DBAL\Exception\TableNotFoundException;
1919
use Doctrine\DBAL\LockMode;
20+
use Doctrine\DBAL\Platforms\MySqlPlatform;
2021
use Doctrine\DBAL\Query\QueryBuilder;
2122
use Doctrine\DBAL\Result;
2223
use Doctrine\DBAL\Schema\Comparator;
@@ -404,7 +405,6 @@ private function addTableToSchema(Schema $schema): void
404405
$table->addColumn('headers', Types::TEXT)
405406
->setNotnull(true);
406407
$table->addColumn('queue_name', Types::STRING)
407-
->setLength(190) // MySQL 5.6 only supports 191 characters on an indexed column in utf8mb4 mode
408408
->setNotnull(true);
409409
$table->addColumn('created_at', Types::DATETIME_MUTABLE)
410410
->setNotnull(true);
@@ -413,8 +413,11 @@ private function addTableToSchema(Schema $schema): void
413413
$table->addColumn('delivered_at', Types::DATETIME_MUTABLE)
414414
->setNotnull(false);
415415
$table->setPrimaryKey(['id']);
416-
$table->addIndex(['queue_name']);
417-
$table->addIndex(['available_at']);
416+
// No indices on queue_name and available_at on MySQL to prevent deadlock issues when running multiple consumers.
417+
if (!$this->driverCo 5A17 nnection->getDatabasePlatform() instanceof MySqlPlatform) {
418+
$table->addIndex(['queue_name']);
419+
$table->addIndex(['available_at']);
420+
}
418421
$table->addIndex(['delivered_at']);
419422
}
420423

0 commit comments

Comments
 (0)
0