Skip to content

Commit d3fcbcf

Browse files
committed
Merge branch '4.3.x' into 5.0.x
2 parents 90e0f35 + 85765ed commit d3fcbcf

File tree

11 files changed

+153
-34
lines changed

11 files changed

+153
-34
lines changed

.github/workflows/website-schema.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
name: "Website config validation"
3+
4+
on:
5+
pull_request:
6+
branches:
7+
- "*.x"
8+
paths:
9+
- ".doctrine-project.json"
10+
- ".github/workflows/website-schema.yml"
11+
push:
12+
branches:
13+
- "*.x"
14+
paths:
15+
- ".doctrine-project.json"
16+
- ".github/workflows/website-schema.yml"
17+
18+
jobs:
19+
json-validate:
20+
name: "Validate JSON schema"
21+
uses: "doctrine/.github/.github/workflows/[email protected]"

UPGRADE.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ The `Sequence::isAutoIncrementsFor()` method has been deprecated.
112112

113113
## Deprecated using invalid database object names
114114

115-
Using the following objects with an empty name is deprecated: `Column`, `View`, `Sequence`, `Identifier`.
115+
Using the following objects with an empty name is deprecated: `Table`, `Column`, `Index`, `View`, `Sequence`,
116+
`Identifier`.
116117

117118
Using the following objects with a qualified name is deprecated: `Column`, `ForeignKeyConstraint`, `Index`, `Schema`,
118119
`UniqueConstraint`. If the object name contains a dot, the name should be quoted.

src/Platforms/AbstractMySQLPlatform.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,21 +230,21 @@ protected function _getCreateTableSQL(string $name, array $columns, array $optio
230230
{
231231
$queryFields = $this->getColumnDeclarationListSQL($columns);
232232

233-
if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) {
233+
if (! empty($options['uniqueConstraints'])) {
234234
foreach ($options['uniqueConstraints'] as $definition) {
235235
$queryFields .= ', ' . $this->getUniqueConstraintDeclarationSQL($definition);
236236
}
237237
}
238238

239239
// add all indexes
240-
if (isset($options['indexes']) && ! empty($options['indexes'])) {
240+
if (! empty($options['indexes'])) {
241241
foreach ($options['indexes'] as $definition) {
242242
$queryFields .= ', ' . $this->getIndexDeclarationSQL($definition);
243243
}
244244
}
245245

246246
// attach all primary keys
247-
if (isset($options['primary']) && ! empty($options['primary'])) {
247+
if (! empty($options['primary'])) {
248248
$keyColumns = array_unique(array_values($options['primary']));
249249
$queryFields .= ', PRIMARY KEY(' . implode(', ', $keyColumns) . ')';
250250
}

src/Platforms/AbstractPlatform.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ private function buildCreateTableSQL(Table $table, bool $createForeignKeys): arr
809809

810810
foreach ($table->getIndexes() as $index) {
811811
if (! $index->isPrimary()) {
812-
$options['indexes'][$index->getQuotedName($this)] = $index;
812+
$options['indexes'][] = $index;
813813

814814
continue;
815815
}
@@ -819,7 +819,7 @@ private function buildCreateTableSQL(Table $table, bool $createForeignKeys): arr
819819
}
820820

821821
foreach ($table->getUniqueConstraints() as $uniqueConstraint) {
822-
$options['uniqueConstraints'][$uniqueConstraint->getQuotedName($this)] = $uniqueConstraint;
822+
$options['uniqueConstraints'][] = $uniqueConstraint;
823823
}
824824

825825
if ($createForeignKeys) {
@@ -961,17 +961,17 @@ protected function _getCreateTableSQL(string $name, array $columns, array $optio
961961
{
962962
$columnListSql = $this->getColumnDeclarationListSQL($columns);
963963

964-
if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) {
964+
if (! empty($options['uniqueConstraints'])) {
965965
foreach ($options['uniqueConstraints'] as $definition) {
966966
$columnListSql .= ', ' . $this->getUniqueConstraintDeclarationSQL($definition);
967967
}
968968
}
969969

970-
if (isset($options['primary']) && ! empty($options['primary'])) {
970+
if (! empty($options['primary'])) {
971971
$columnListSql .= ', PRIMARY KEY(' . implode(', ', array_unique(array_values($options['primary']))) . ')';
972972
}
973973

974-
if (isset($options['indexes']) && ! empty($options['indexes'])) {
974+
if (! empty($options['indexes'])) {
975975
foreach ($options['indexes'] as $definition) {
976976
$columnListSql .= ', ' . $this->getIndexDeclarationSQL($definition);
977977
}
@@ -1147,8 +1147,13 @@ public function getCreateSchemaSQL(string $schemaName): string
11471147
*/
11481148
public function getCreateUniqueConstraintSQL(UniqueConstraint $constraint, string $tableName): string
11491149
{
1150-
return 'ALTER TABLE ' . $tableName . ' ADD CONSTRAINT ' . $constraint->getQuotedName($this) . ' UNIQUE'
1151-
. ' (' . implode(', ', $constraint->getQuotedColumns($this)) . ')';
1150+
$sql = 'ALTER TABLE ' . $tableName . ' ADD';
1151+
1152+
if ($constraint->getName() !== '') {
1153+
$sql .= ' CONSTRAINT ' . $constraint->getQuotedName($this);
1154+
}
1155+
1156+
return $sql . ' UNIQUE (' . implode(', ', $constraint->getQuotedColumns($this)) . ')';
11521157
}
11531158

11541159
/**
@@ -1491,9 +1496,10 @@ public function getUniqueConstraintDeclarationSQL(UniqueConstraint $constraint):
14911496
throw new InvalidArgumentException('Incomplete definition. "columns" required.');
14921497
}
14931498

1494-
$chunks = ['CONSTRAINT'];
1499+
$chunks = [];
14951500

14961501
if ($constraint->getName() !== '') {
1502+
$chunks[] = 'CONSTRAINT';
14971503
$chunks[] = $constraint->getQuotedName($this);
14981504
}
14991505

src/Platforms/PostgreSQLPlatform.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ protected function _getCreateTableSQL(string $name, array $columns, array $optio
379379
{
380380
$queryFields = $this->getColumnDeclarationListSQL($columns);
381381

382-
if (isset($options['primary']) && ! empty($options['primary'])) {
382+
if (! empty($options['primary'])) {
383383
$keyColumns = array_unique(array_values($options['primary']));
384384
$queryFields .= ', PRIMARY KEY(' . implode(', ', $keyColumns) . ')';
385385
}
@@ -390,7 +390,7 @@ protected function _getCreateTableSQL(string $name, array $columns, array $optio
390390

391391
$sql = [$query];
392392

393-
if (isset($options['indexes']) && ! empty($options['indexes'])) {
393+
if (! empty($options['indexes'])) {
394394
foreach ($options['indexes'] as $index) {
395395
$sql[] = $this->getCreateIndexSQL($index, $name);
396396
}

src/Platforms/SQLServerPlatform.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,13 @@ protected function _getCreateTableSQL(string $name, array $columns, array $optio
204204

205205
$columnListSql = $this->getColumnDeclarationListSQL($columns);
206206

207-
if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) {
207+
if (! empty($options['uniqueConstraints'])) {
208208
foreach ($options['uniqueConstraints'] as $definition) {
209209
$columnListSql .= ', ' . $this->getUniqueConstraintDeclarationSQL($definition);
210210
}
211211
}
212212

213-
if (isset($options['primary']) && ! empty($options['primary'])) {
213+
if (! empty($options['primary'])) {
214214
$flags = '';
215215
if (isset($options['primary_index']) && $options['primary_index']->hasFlag('nonclustered')) {
216216
$flags = ' NONCLUSTERED';
@@ -231,7 +231,7 @@ protected function _getCreateTableSQL(string $name, array $columns, array $optio
231231

232232
$sql = [$query];
233233

234-
if (isset($options['indexes']) && ! empty($options['indexes'])) {
234+
if (! empty($options['indexes'])) {
235235
foreach ($options['indexes'] as $index) {
236236
$sql[] = $this->getCreateIndexSQL($index, $name);
237237
}

src/Platforms/SQLitePlatform.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ protected function _getCreateTableSQL(string $name, array $columns, array $optio
269269
{
270270
$queryFields = $this->getColumnDeclarationListSQL($columns);
271271

272-
if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) {
272+
if (! empty($options['uniqueConstraints'])) {
273273
foreach ($options['uniqueConstraints'] as $definition) {
274274
$queryFields .= ', ' . $this->getUniqueConstraintDeclarationSQL($definition);
275275
}
@@ -296,13 +296,13 @@ protected function _getCreateTableSQL(string $name, array $columns, array $optio
296296
return $query;
297297
}
298298

299-
if (isset($options['indexes']) && ! empty($options['indexes'])) {
299+
if (! empty($options['indexes'])) {
300300
foreach ($options['indexes'] as $indexDef) {
301301
$query[] = $this->getCreateIndexSQL($indexDef, $name);
302302
}
303303
}
304304

305-
if (isset($options['unique']) && ! empty($options['unique'])) {
305+
if (! empty($options['unique'])) {
306306
foreach ($options['unique'] as $indexDef) {
307307
$query[] = $this->getCreateIndexSQL($indexDef, $name);
308308
}

src/Schema/Index.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
use function count;
1818
use function strtolower;
1919

20-
/** @extends AbstractOptionallyNamedObject<UnqualifiedName> */
21-
class Index extends AbstractOptionallyNamedObject
20+
/** @extends AbstractNamedObject<UnqualifiedName> */
21+
class Index extends AbstractNamedObject
2222
{
2323
/**
2424
* Asset identifier instances of the column names the index is associated with.
@@ -51,7 +51,7 @@ public function __construct(
5151
array $flags = [],
5252
private readonly array $options = [],
5353
) {
54-
parent::__construct($name);
54+
parent::__construct($name ?? '');
5555

5656
$this->_isUnique = $isUnique || $isPrimary;
5757
$this->_isPrimary = $isPrimary;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Tests\Functional\Schema;
6+
7+
use Doctrine\DBAL\Schema\Column;
8+
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
9+
use Doctrine\DBAL\Schema\Table;
10+
use Doctrine\DBAL\Tests\FunctionalTestCase;
11+
use Doctrine\DBAL\Types\Type;
12+
use Doctrine\DBAL\Types\Types;
13+
14+
final class ForeignKeyConstraintTest extends FunctionalTestCase
15+
{
16+
public function testUnnamedForeignKeyConstraint(): void
17+
{
18+
$this->dropTableIfExists('users');
19+
$this->dropTableIfExists('roles');
20+
$this->dropTableIfExists('teams');
21+
22+
$roles = new Table('roles');
23+
$roles->addColumn('id', Types::INTEGER);
24+
$roles->setPrimaryKey(['id']);
25+
26+
$teams = new Table('teams');
27+
$teams->addColumn('id', Types::INTEGER);
28+
$teams->setPrimaryKey(['id']);
29+
30+
$users = new Table('users', [
31+
new Column('id', Type::getType(Types::INTEGER)),
32+
new Column('role_id', Type::getType(Types::INTEGER)),
33+
new Column('team_id', Type::getType(Types::INTEGER)),
34+
], [], [], [
35+
new ForeignKeyConstraint(['role_id'], 'roles', ['id']),
36+
new ForeignKeyConstraint(['team_id'], 'teams', ['id']),
37+
]);
38+
$users->setPrimaryKey(['id']);
39+
40+
$sm = $this->connection->createSchemaManager();
41+
$sm->createTable($roles);
42+
$sm->createTable($teams);
43+
$sm->createTable($users);
44+
45+
$table = $sm->introspectTable('users');
46+
47+
self::assertCount(2, $table->getForeignKeys());
48+
}
49+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Tests\Functional\Schema;
6+
7+
use Doctrine\DBAL\Schema\Column;
8+
use Doctrine\DBAL\Schema\Table;
9+
use Doctrine\DBAL\Schema\UniqueConstraint;
10+
use Doctrine\DBAL\Tests\FunctionalTestCase;
11+
use Doctrine\DBAL\Types\Type;
12+
use Doctrine\DBAL\Types\Types;
13+
14+
final class UniqueConstraintTest extends FunctionalTestCase
15+
{
16+
public function testUnnamedUniqueConstraint(): void
17+
{
18+
$this->dropTableIfExists('users');
19+
20+
$users = new Table('users', [
21+
new Column('id', Type::getType(Types::INTEGER)),
22+
new Column('username', Type::getType(Types::STRING), ['length' => 32]),
23+
new Column('email', Type::getType(Types::STRING), ['length' => 255]),
24+
], [], [
25+
new UniqueConstraint('', ['username']),
26+
new UniqueConstraint('', ['email']),
27+
], []);
28+
29+
$sm = $this->connection->createSchemaManager();
30+
$sm->createTable($users);
31+
32+
// we want to assert that the two empty names don't clash, but introspection of unique constraints is currently
33+
// not supported. for now, we just assert that the table can be created without exceptions.
34+
$this->expectNotToPerformAssertions();
35+
}
36+
}

0 commit comments

Comments
 (0)