Skip to content

Commit de5042f

Browse files
authored
Merge pull request #5758 from morozov/schema-diff-public-properties-internal
Mark SchemaDiff public properties internal
2 parents acb5e77 + b272ec9 commit de5042f

File tree

4 files changed

+176
-36
lines changed

4 files changed

+176
-36
lines changed

UPGRADE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ Relying on the availability of the `LOCATE()` on SQLite deprecated. SQLite does
1414
but the function `INSTR()` can be a drop-in replacement in most situations. Use
1515
`AbstractPlatform::getLocateExpression()` if you need a portable solution.
1616

17+
## Deprecated `SchemaDiff::$orphanedForeignKeys`
18+
19+
Relying on the schema diff tracking foreign keys referencing the tables that have been dropped is deprecated.
20+
Before dropping a table referenced by foreign keys, drop the foreign keys first.
21+
1722
## Deprecated the `userDefinedFunctions` driver option for `pdo_sqlite`
1823

1924
Instead of funneling custom functions through the `userDefinedFunctions` option, use `getNativeConnection()`
@@ -134,6 +139,22 @@ The following `Comparator` methods have been marked as internal:
134139

135140
The `diffColumn()` method has been deprecated. Use `diffTable()` instead.
136141

142+
## Marked `SchemaDiff` public properties as internal.
143+
144+
The public properties of the `SchemaDiff` class have been marked as internal. Use the following corresponding methods
145+
instead:
146+
147+
| Property | Method |
148+
|----------------------|-------------------------|
149+
| `$newNamespaces` | `getCreatedSchemas()` |
150+
| `$removedNamespaces` | `getDroppedSchemas()` |
151+
| `$newTables` | `getCreatedTables()` |
152+
| `$changedTables` | `getAlteredTables()` |
153+
| `$removedTables` | `getDroppedTables()` |
154+
| `$newSequences` | `getCreatedSequences()` |
155+
| `$changedSequences` | `getAlteredSequence()` |
156+
| `$removedSequences` | `getDroppedSequences()` |
157+
137158
## Marked `TableDiff` public properties as internal.
138159

139160
The public properties of the `TableDiff` class have been marked as internal. Use the following corresponding methods

psalm.xml.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,10 @@
491491
TODO: remove in 4.0.0
492492
-->
493493
<referencedProperty name="Doctrine\DBAL\Schema\TableDiff::$name"/>
494+
<!--
495+
TODO: remove in 4.0.0
496+
-->
497+
<referencedProperty name="Doctrine\DBAL\Schema\SchemaDiff::$orphanedForeignKeys"/>
494498
</errorLevel>
495499
</DeprecatedProperty>
496500
<DocblockTypeContradiction>

src/Schema/Comparator.php

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,16 @@ private function doCompareSchemas(
8888
Schema $fromSchema,
8989
Schema $toSchema
9090
) {
91-
$diff = new SchemaDiff();
92-
$diff->fromSchema = $fromSchema;
91+
$createdSchemas = [];
92+
$droppedSchemas = [];
93+
$createdTables = [];
94+
$alteredTables = [];
95+
$droppedTables = [];
96+
$createdSequences = [];
97+
$alteredSequences = [];
98+
$droppedSequences = [];
99+
100+
$orphanedForeignKeys = [];
93101

94102
$foreignKeysToTable = [];
95103

@@ -98,29 +106,29 @@ private function doCompareSchemas(
98106
continue;
99107
}
100108

101-
$diff->newNamespaces[$namespace] = $namespace;
109+
$createdSchemas[$namespace] = $namespace;
102110
}
103111

104112
foreach ($fromSchema->getNamespaces() as $namespace) {
105113
if ($toSchema->hasNamespace($namespace)) {
106114
continue;
107115
}
108116

109-
$diff->removedNamespaces[$namespace] = $namespace;
117+
$droppedSchemas[$namespace] = $namespace;
110118
}
111119

112120
foreach ($toSchema->getTables() as $table) {
113121
$tableName = $table->getShortestName($toSchema->getName());
114122
if (! $fromSchema->hasTable($tableName)) {
115-
$diff->newTables[$tableName] = $toSchema->getTable($tableName);
123+
$createdTables[$tableName] = $toSchema->getTable($tableName);
116124
} else {
117125
$tableDifferences = $this->diffTable(
118126
$fromSchema->getTable($tableName),
119127
$toSchema->getTable($tableName),
120128
);
121129

122130
if ($tableDifferences !== false) {
123-
$diff->changedTables[$tableName] = $tableDifferences;
131+
$alteredTables[$tableName] = $tableDifferences;
124132
}
125133
}
126134
}
@@ -131,7 +139,7 @@ private function doCompareSchemas(
131139

132140
$table = $fromSchema->getTable($tableName);
133141
if (! $toSchema->hasTable($tableName)) {
134-
$diff->removedTables[$tableName] = $table;
142+
$droppedTables[$tableName] = $table;
135143
}
136144

137145
// also remember all foreign keys that point to a specific table
@@ -145,37 +153,37 @@ private function doCompareSchemas(
145153
}
146154
}
147155

148-
foreach ($diff->removedTables as $tableName => $table) {
156+
foreach ($droppedTables as $tableName => $table) {
149157
if (! isset($foreignKeysToTable[$tableName])) {
150158
continue;
151159
}
152160

153161
foreach ($foreignKeysToTable[$tableName] as $foreignKey) {
154-
if (isset($diff->removedTables[strtolower($foreignKey->getLocalTableName())])) {
162+
if (isset($droppedTables[strtolower($foreignKey->getLocalTableName())])) {
155163
continue;
156164
}
157165

158-
$diff->orphanedForeignKeys[] = $foreignKey;
166+
$orphanedForeignKeys[] = $foreignKey;
159167
}
160168

161169
// deleting duplicated foreign keys present on both on the orphanedForeignKey
162170
// and the removedForeignKeys from changedTables
163171
foreach ($foreignKeysToTable[$tableName] as $foreignKey) {
164172
// strtolower the table name to make if compatible with getShortestName
165173
$localTableName = strtolower($foreignKey->getLocalTableName());
166-
if (! isset($diff->changedTables[$localTableName])) {
174+
if (! isset($alteredTables[$localTableName])) {
167175
continue;
168176
}
169177

170-
foreach ($diff->changedTables[$localTableName]->getDroppedForeignKeys() as $droppedForeignKey) {
178+
foreach ($alteredTables[$localTableName]->getDroppedForeignKeys() as $droppedForeignKey) {
171179
assert($droppedForeignKey instanceof ForeignKeyConstraint);
172180

173181
// We check if the key is from the removed table if not we skip.
174182
if ($tableName !== strtolower($droppedForeignKey->getForeignTableName())) {
175183
continue;
176184
}
177185

178-
$diff->changedTables[$localTableName]->unsetDroppedForeignKey($droppedForeignKey);
186+
$alteredTables[$localTableName]->unsetDroppedForeignKey($droppedForeignKey);
179187
}
180188
}
181189
}
@@ -184,11 +192,11 @@ private function doCompareSchemas(
184192
$sequenceName = $sequence->getShortestName($toSchema->getName());
185193
if (! $fromSchema->hasSequence($sequenceName)) {
186194
if (! $this->isAutoIncrementSequenceInSchema($fromSchema, $sequence)) {
187-
$diff->newSequences[] = $sequence;
195+
$createdSequences[] = $sequence;
188196
}
189197
} else {
190198
if ($this->diffSequence($sequence, $fromSchema->getSequence($sequenceName))) {
191-
$diff->changedSequences[] = $toSchema->getSequence($sequenceName);
199+
$alteredSequences[] = $toSchema->getSequence($sequenceName);
192200
}
193201
}
194202
}
@@ -204,9 +212,23 @@ private function doCompareSchemas(
204212
continue;
205213
}
206214

207-
$diff->removedSequences[] = $sequence;
215+
$droppedSequences[] = $sequence;
208216
}
209217

218+
$diff = new SchemaDiff(
219+
$createdTables,
220+
$alteredTables,
221+
$droppedTables,
222+
$fromSchema,
223+
$createdSchemas,
224+
$droppedSchemas,
225+
$createdSequences,
226+
$alteredSequences,
227+
$droppedSequences,
228+
);
229+
230+
$diff->orphanedForeignKeys = $orphanedForeignKeys;
231+
210232
return $diff;
211233
}
212234

0 commit comments

Comments
 (0)