Skip to content

Commit bf42190

Browse files
committed
use Table::addPrimaryKeyConstraint() with Doctrine DBAL 4.3+
1 parent bc82b67 commit bf42190

File tree

5 files changed

+45
-5
lines changed

5 files changed

+45
-5
lines changed

src/Symfony/Bridge/Doctrine/SchemaListener/AbstractSchemaListener.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
use Doctrine\DBAL\Connection;
1515
use Doctrine\DBAL\Exception\TableNotFoundException;
16+
use Doctrine\DBAL\Schema\Name\Identifier;
17+
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
18+
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
1619
use Doctrine\DBAL\Schema\Table;
1720
use Doctrine\DBAL\Types\Types;
1821
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;
@@ -30,7 +33,12 @@ protected function getIsSameDatabaseChecker(Connection $connection): \Closure
3033
$table->addColumn('id', Types::INTEGER)
3134
->setAutoincrement(true)
3235
->setNotnull(true);
33-
$table->setPrimaryKey(['id']);
36+
37+
if (class_exists(PrimaryKeyConstraint::class)) {
38+
$table->addPrimaryKeyConstraint(new PrimaryKeyConstraint(null, [new UnqualifiedName(Identifier::unquoted('id'))], true));
39+
} else {
40+
$table->setPrimaryKey(['id']);
41+
}
3442

3543
$schemaManager->createTable($table);
3644

src/Symfony/Bridge/Doctrine/Security/RememberMe/DoctrineTokenProvider.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
use Doctrine\DBAL\Connection;
1515
use Doctrine\DBAL\ParameterType;
16+
use Doctrine\DBAL\Schema\Name\Identifier;
17+
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
18+
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
1619
use Doctrine\DBAL\Schema\Schema;
1720
use Doctrine\DBAL\Types\Types;
1821
use Symfony\Component\Security\Core\Authentication\RememberMe\PersistentToken;
@@ -193,6 +196,11 @@ private function addTableToSchema(Schema $schema): void
193196
$table->addColumn('lastUsed', Types::DATETIME_IMMUTABLE);
194197
$table->addColumn('class', Types::STRING, ['length' => 100]);
195198
$table->addColumn('username', Types::STRING, ['length' => 200]);
196-
$table->setPrimaryKey(['series']);
199+
200+
if (class_exists(PrimaryKeyConstraint::class)) {
201+
$table->addPrimaryKeyConstraint(new PrimaryKeyConstraint(null, [new UnqualifiedName(Identifier::unquoted('series'))], true));
202+
} else {
203+
$table->setPrimaryKey(['series']);
204+
}
197205
}
198206
}

src/Symfony/Component/HttpFoundation/Session/Storage/Handler/PdoSessionHandler.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
1313

14+
use Doctrine\DBAL\Schema\Name\Identifier;
15+
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
16+
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
1417
use Doctrine\DBAL\Schema\Schema;
1518
use Doctrine\DBAL\Types\Types;
1619

@@ -224,7 +227,13 @@ public function configureSchema(Schema $schema, ?\Closure $isSameDatabase = null
224227
default:
225228
throw new \DomainException(\sprintf('Creating the session table is currently not implemented for PDO driver "%s".', $this->driver));
226229
}
227-
$table->setPrimaryKey([$this->idCol]);
230+
231+
if (class_exists(PrimaryKeyConstraint::class)) {
232+
$table->addPrimaryKeyConstraint(new PrimaryKeyConstraint(null, [new UnqualifiedName(Identifier::unquoted($this->idCol))], true));
233+
} else {
234+
$table->setPrimaryKey([$this->idCol]);
235+
}
236+
228237
$table->addIndex([$this->lifetimeCol], $this->lifetimeCol.'_idx');
229238
}
230239

src/Symfony/Component/Lock/Store/DoctrineDbalStore.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
use Doctrine\DBAL\Exception\TableNotFoundException;
1919
use Doctrine\DBAL\ParameterType;
2020
use Doctrine\DBAL\Schema\DefaultSchemaManagerFactory;
21+
use Doctrine\DBAL\Schema\Name\Identifier;
22+
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
23+
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
2124
use Doctrine\DBAL\Schema\Schema;
2225
use Doctrine\DBAL\Tools\DsnParser;
2326
use Symfony\Component\Lock\Exception\InvalidArgumentException;
@@ -214,7 +217,12 @@ public function configureSchema(Schema $schema, \Closure $isSameDatabase): void
214217
$table->addColumn($this->idCol, 'string', ['length' => 64]);
215218
$table->addColumn($this->tokenCol, 'string', ['length' => 44]);
216219
$table->addColumn($this->expirationCol, 'integer', ['unsigned' => true]);
217-
$table->setPrimaryKey([$this->idCol]);
220+
221+
if (class_exists(PrimaryKeyConstraint::class)) {
222+
$table->addPrimaryKeyConstraint(new PrimaryKeyConstraint(null, [new UnqualifiedName(Identifier::unquoted($this->idCol))], true));
223+
} else {
224+
$table->setPrimaryKey([$this->idCol]);
225+
}
218226
}
219227

220228
/**

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
use Doctrine\DBAL\Query\QueryBuilder;
2424
use Doctrine\DBAL\Result;
2525
use Doctrine\DBAL\Schema\AbstractAsset;
26+
use Doctrine\DBAL\Schema\Name\Identifier;
27+
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
28+
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
2629
use Doctrine\DBAL\Schema\Schema;
2730
use Doctrine\DBAL\Schema\Table;
2831
use Doctrine\DBAL\Types\Types;
@@ -519,7 +522,11 @@ private function addTableToSchema(Schema $schema): void
519522
->setNotnull(true);
520523
$table->addColumn('delivered_at', Types::DATETIME_IMMUTABLE)
521524
->setNotnull(false);
522-
$table->setPrimaryKey(['id']);
525+
if (class_exists(PrimaryKeyConstraint::class)) {
526+
$table->addPrimaryKeyConstraint(new PrimaryKeyConstraint(null, [new UnqualifiedName(Identifier::unquoted('id'))], true));
527+
} else {
528+
$table->setPrimaryKey(['id']);
529+
}
523530
$table->addIndex(['queue_name']);
524531
$table->addIndex(['available_at']);
525532
$table->addIndex(['delivered_at']);

0 commit comments

Comments
 (0)