Skip to content

Commit 13e73f1

Browse files
authored
Merge pull request #4106 from karakayasemi/fix-oracle-autoincrement
respect to platform restrictions when creating autoincrement identifier name
2 parents 9d69275 + e48d65d commit 13e73f1

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

lib/Doctrine/DBAL/Platforms/OraclePlatform.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,22 @@ private function normalizeIdentifier($name)
586586
return $identifier->isQuoted() ? $identifier : new Identifier(strtoupper($name));
587587
}
588588

589+
/**
590+
* Adds suffix to identifier,
591+
*
592+
* if the new string exceeds max identifier length,
593+
* keeps $suffix, cuts from $identifier as much as the part exceeding.
594+
*/
595+
private function addSuffix(string $identifier, string $suffix) : string
596+
{
597+
$maxPossibleLengthWithoutSuffix = $this->getMaxIdentifierLength() - strlen($suffix);
598+
if (strlen($identifier) > $maxPossibleLengthWithoutSuffix) {
599+
$identifier = substr($identifier, 0, $maxPossibleLengthWithoutSuffix);
600+
}
601+
602+
return $identifier . $suffix;
603+
}
604+
589605
/**
590606
* Returns the autoincrement primary key identifier name for the given table identifier.
591607
*
@@ -598,7 +614,7 @@ private function normalizeIdentifier($name)
598614
*/
599615
private function getAutoincrementIdentifierName(Identifier $table)
600616
{
601-
$identifierName = $table->getName() . '_AI_PK';
617+
$identifierName = $this->addSuffix($table->getName(), '_AI_PK');
602618

603619
return $table->isQuoted()
604620
? $this->quoteSingleIdentifier($identifierName)
@@ -966,7 +982,7 @@ public function getIdentitySequenceName($tableName, $columnName)
966982
$table = new Identifier($tableName);
967983

968984
// No usage of column name to preserve BC compatibility with <2.5
969-
$identitySequenceName = $table->getName() . '_SEQ';
985+
$identitySequenceName = $this->addSuffix($table->getName(), '_SEQ');
970986

971987
if ($table->isQuoted()) {
972988
$identitySequenceName = '"' . $identitySequenceName . '"';
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Doctrine\Tests\DBAL\Functional\Platform;
4+
5+
use Doctrine\DBAL\Schema\Table;
6+
use Doctrine\Tests\DbalFunctionalTestCase;
7+
use function str_repeat;
8+
9+
/**
10+
* This class holds tests that make sure generated SQL statements respect to platform restrictions
11+
* like maximum element name length
12+
*/
13+
class PlatformRestrictionsTest extends DbalFunctionalTestCase
14+
{
15+
/**
16+
* Tests element names that are at the boundary of the identifier length limit.
17+
* Ensures generated auto-increment identifier name respects to platform restrictions.
18+
*/
19+
public function testMaxIdentifierLengthLimitWithAutoIncrement() : void
20+
{
21+
$platform = $this->connection->getDatabasePlatform();
22+
$tableName = str_repeat('x', $platform->getMaxIdentifierLength());
23+
$columnName = str_repeat('y', $platform->getMaxIdentifierLength());
24+
$table = new Table($tableName);
25+
$table->addColumn($columnName, 'integer', ['autoincrement' => true]);
26+
$table->setPrimaryKey([$columnName]);
27+
$this->connection->getSchemaManager()->dropAndCreateTable($table);
28+
$createdTable = $this->connection->getSchemaManager()->listTableDetails($tableName);
29+
30+
$this->assertTrue($createdTable->hasColumn($columnName));
31+
$this->assertTrue($createdTable->hasPrimaryKey());
32+
}
33+
}

0 commit comments

Comments
 (0)