Skip to content

Commit 45c3203

Browse files
atgitwkfabpot
authored andcommitted
[Messenger] Fix Oracle errors 'ORA-00955: Name is already used by an existing object' with Doctrine transport
1 parent 7c54399 commit 45c3203

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,12 @@ public function testConfigureSchemaOracleSequenceNameSuffixed()
868868
{
869869
$driverConnection = $this->createMock(DBALConnection::class);
870870
$driverConnection->method('getDatabasePlatform')->willReturn(new OraclePlatform());
871+
872+
// Mock the result returned by executeQuery to be an Oracle version 12.1.0 or higher.
873+
$result = $this->createMock(Result::class);
874+
$result->method('fetchOne')->willReturn('12.1.0');
875+
$driverConnection->method('executeQuery')->willReturn($result);
876+
871877
$schema = new Schema();
872878

873879
$connection = new Connection(['table_name' => 'messenger_messages'], $driverConnection);

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,9 @@ public function setup(): void
338338
throw new \TypeError(\sprintf('The table name must be an instance of "%s" or a string ("%s" given).', AbstractAsset::class, get_debug_type($tableName)));
339339
}
340340

341-
return $tableName === $this->configuration['table_name'];
341+
// SchemaAssetsFilter needs to match the messenger table name and also the messenger sequence name to make $schemaDiff work correctly in updateSchema()
342+
// This may also work for other databases if their sequence name is suffixed with '_seq', '_id_seq' or similar.
343+
return str_starts_with($tableName, $this->configuration['table_name']); // MESSENGER_MESSAGES*
342344
});
343345
$this->updateSchema();
344346
$configuration->setSchemaAssetsFilter($assetFilter);
@@ -569,9 +571,13 @@ private function addTableToSchema(Schema $schema): void
569571

570572
// We need to create a sequence for Oracle and set the id column to get the correct nextval
571573
if ($this->driverConnection->getDatabasePlatform() instanceof OraclePlatform) {
572-
$idColumn->setDefault($this->configuration['table_name'].self::ORACLE_SEQUENCES_SUFFIX.'.nextval');
574+
$serverVersion = $this->driverConnection->executeQuery("SELECT version FROM product_component_version WHERE product LIKE 'Oracle Database%'")->fetchOne();
575+
if (version_compare($serverVersion, '12.1.0', '>=')) {
576+
$idColumn->setAutoincrement(false); // disable the creation of SEQUENCE and TRIGGER
577+
$idColumn->setDefault($this->configuration['table_name'].self::ORACLE_SEQUENCES_SUFFIX.'.nextval');
573578

574-
$schema->createSequence($this->configuration['table_name'].self::ORACLE_SEQUENCES_SUFFIX);
579+
$schema->createSequence($this->configuration['table_name'].self::ORACLE_SEQUENCES_SUFFIX);
580+
}
575581
}
576582
}
577583

0 commit comments

Comments
 (0)