Skip to content

Commit 907b503

Browse files
committed
Add more TableEditor methods
1 parent eeff40d commit 907b503

File tree

4 files changed

+1164
-36
lines changed

4 files changed

+1164
-36
lines changed
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Schema\Exception;
6+
7+
use Doctrine\DBAL\Schema\Collections\Exception\ObjectAlreadyExists;
8+
use Doctrine\DBAL\Schema\Collections\Exception\ObjectDoesNotExist;
9+
use Doctrine\DBAL\Schema\Name\OptionallyQualifiedName;
10+
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
11+
use Doctrine\DBAL\Schema\SchemaException;
12+
use LogicException;
13+
14+
use function sprintf;
15+
16+
/** @internal */
17+
final class InvalidTableModification extends LogicException implements SchemaException
18+
{
19+
public static function columnAlreadyExists(
20+
?OptionallyQualifiedName $tableName,
21+
ObjectAlreadyExists $previous,
22+
): self {
23+
return new self(sprintf(
24+
'Column %s already exists on table %s.',
25+
$previous->getObjectName()->toString(),
26+
self::formatTableName($tableName),
27+
), previous: $previous);
28+
}
29+
30+
public static function columnDoesNotExist(
31+
?OptionallyQualifiedName $tableName,
32+
ObjectDoesNotExist $previous,
33+
): self {
34+
return new self(sprintf(
35+
'Column %s does not exist on table %s.',
36+
$previous->getObjectName()->toString(),
37+
self::formatTableName($tableName),
38+
), previous: $previous);
39+
}
40+
41+
public static function indexAlreadyExists(
42+
?OptionallyQualifiedName $tableName,
43+
ObjectAlreadyExists $previous,
44+
): self {
45+
return new self(sprintf(
46+
'Index %s already exists on table %s.',
47+
$previous->getObjectName()->toString(),
48+
self::formatTableName($tableName),
49+
), previous: $previous);
50+
}
51+
52+
public static function indexDoesNotExist(
53+
?OptionallyQualifiedName $tableName,
54+
ObjectDoesNotExist $previous,
55+
): self {
56+
return new self(sprintf(
57+
'Index %s does not exist on table %s.',
58+
$previous->getObjectName()->toString(),
59+
self::formatTableName($tableName),
60+
), previous: $previous);
61+
}
62+
63+
public static function primaryKeyConstraintAlreadyExists(?OptionallyQualifiedName $tableName): self
64+
{
65+
return new self(sprintf(
66+
'Primary key constraint already exists on table %s.',
67+
self::formatTableName($tableName),
68+
));
69+
}
70+
71+
public static function primaryKeyConstraintDoesNotExist(?OptionallyQualifiedName $tableName): self
72+
{
73+
return new self(sprintf(
74+
'Primary key constraint does not exist on table %s.',
75+
self::formatTableName($tableName),
76+
));
77+
}
78+
79+
public static function uniqueConstraintAlreadyExists(
80+
?OptionallyQualifiedName $tableName,
81+
ObjectAlreadyExists $previous,
82+
): self {
83+
return new self(sprintf(
84+
'Unique constraint %s already exists on table %s.',
85+
$previous->getObjectName()->toString(),
86+
self::formatTableName($tableName),
87+
), previous: $previous);
88+
}
89+
90+
public static function uniqueConstraintDoesNotExist(
91+
?OptionallyQualifiedName $tableName,
92+
ObjectDoesNotExist $previous,
93+
): self {
94+
return new self(sprintf(
95+
'Unique constraint %s does not exist on table %s.',
96+
$previous->getObjectName()->toString(),
97+
self::formatTableName($tableName),
98+
), previous: $previous);
99+
}
100+
101+
public static function foreignKeyConstraintAlreadyExists(
102+
?OptionallyQualifiedName $tableName,
103+
ObjectAlreadyExists $previous,
104+
): self {
105+
return new self(sprintf(
106+
'Foreign key constraint %s already exists on table %s.',
107+
$previous->getObjectName()->toString(),
108+
self::formatTableName($tableName),
109+
), previous: $previous);
110+
}
111+
112+
public static function foreignKeyConstraintDoesNotExist(
113+
?OptionallyQualifiedName $tableName,
114+
ObjectDoesNotExist $previous,
115+
): self {
116+
return new self(sprintf(
117+
'Foreign key constraint %s does not exist on table %s.',
118+
$previous->getObjectName()->toString(),
119+
self::formatTableName($tableName),
120+
), previous: $previous);
121+
}
122+
123+
public static function indexedColumnDoesNotExist(
124+
?OptionallyQualifiedName $tableName,
125+
UnqualifiedName $indexName,
126+
UnqualifiedName $columnName,
127+
): self {
128+
return new self(sprintf(
129+
'Column %s referenced by index %s does not exist on table %s.',
130+
$columnName->toString(),
131+
$indexName->toString(),
132+
self::formatTableName($tableName),
133+
));
134+
}
135+
136+
public static function primaryKeyConstraintColumnDoesNotExist(
137+
?OptionallyQualifiedName $tableName,
138+
?UnqualifiedName $constraintName,
139+
UnqualifiedName $columnName,
140+
): self {
141+
return new self(sprintf(
142+
'Column %s referenced by primary key constraint %s does not exist on table %s.',
143+
$columnName->toString(),
144+
self::formatConstraintName($constraintName),
145+
self::formatTableName($tableName),
146+
));
147+
}
148+
149+
public static function uniqueConstraintColumnDoesNotExist(
150+
?OptionallyQualifiedName $tableName,
151+
?UnqualifiedName $constraintName,
152+
UnqualifiedName $columnName,
153+
): self {
154+
return new self(sprintf(
155+
'Column %s referenced by unique constraint %s does not exist on table %s.',
156+
$columnName->toString(),
157+
self::formatConstraintName($constraintName),
158+
self::formatTableName($tableName),
159+
));
160+
}
161+
162+
public static function foreignKeyConstraintReferencingColumnDoesNotExist(
163+
?OptionallyQualifiedName $tableName,
164+
?UnqualifiedName $constraintName,
165+
UnqualifiedName $columnName,
166+
): self {
167+
return new self(sprintf(
168+
'Referencing column %s of foreign key constraint %s does not exist on table %s.',
169+
$columnName->toString(),
170+
self::formatConstraintName($constraintName),
171+
self::formatTableName($tableName),
172+
));
173+
}
174+
175+
private static function formatTableName(?OptionallyQualifiedName $tableName): string
176+
{
177+
return $tableName === null ? '<unnamed>' : $tableName->toString();
178+
}
179+
180+
private static function formatConstraintName(?UnqualifiedName $constraintName): string
181+
{
182+
return $constraintName === null ? '<unnamed>' : $constraintName->toString();
183+
}
184+
}

src/Schema/Table.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -999,13 +999,23 @@ public static function editor(): TableEditor
999999
*/
10001000
public function edit(): TableEditor
10011001
{
1002-
return self::editor()
1002+
$editor = self::editor()
10031003
->setName($this->getObjectName())
10041004
->setColumns(...array_values($this->_columns))
10051005
->setIndexes(...array_values($this->_indexes))
1006+
->setPrimaryKeyConstraint($this->primaryKeyConstraint)
10061007
->setUniqueConstraints(...array_values($this->uniqueConstraints))
1007-
->setForeignKeyConstraints(...array_values($this->_fkConstraints))
1008-
->setOptions($this->_options)
1008+
->setForeignKeyConstraints(...array_values($this->_fkConstraints));
1009+
1010+
$options = $this->_options;
1011+
1012+
if (isset($options['comment'])) {
1013+
$editor->setComment($options['comment']);
1014+
unset($options['comment']);
1015+
}
1016+
1017+
return $editor
1018+
->setOptions($options)
10091019
->setConfiguration(
10101020
new TableConfiguration($this->maxIdentifierLength),
10111021
);

0 commit comments

Comments
 (0)