Skip to content

Commit

Permalink
Merge pull request #5758 from morozov/schema-diff-public-properties-i…
Browse files Browse the repository at this point in the history
…nternal

Mark SchemaDiff public properties internal
  • Loading branch information
morozov authored Oct 15, 2022
2 parents acb5e77 + b272ec9 commit de5042f
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 36 deletions.
21 changes: 21 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ Relying on the availability of the `LOCATE()` on SQLite deprecated. SQLite does
but the function `INSTR()` can be a drop-in replacement in most situations. Use
`AbstractPlatform::getLocateExpression()` if you need a portable solution.

## Deprecated `SchemaDiff::$orphanedForeignKeys`

Relying on the schema diff tracking foreign keys referencing the tables that have been dropped is deprecated.
Before dropping a table referenced by foreign keys, drop the foreign keys first.

## Deprecated the `userDefinedFunctions` driver option for `pdo_sqlite`

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

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

## Marked `SchemaDiff` public properties as internal.

The public properties of the `SchemaDiff` class have been marked as internal. Use the following corresponding methods
instead:

| Property | Method |
|----------------------|-------------------------|
| `$newNamespaces` | `getCreatedSchemas()` |
| `$removedNamespaces` | `getDroppedSchemas()` |
| `$newTables` | `getCreatedTables()` |
| `$changedTables` | `getAlteredTables()` |
| `$removedTables` | `getDroppedTables()` |
| `$newSequences` | `getCreatedSequences()` |
| `$changedSequences` | `getAlteredSequence()` |
| `$removedSequences` | `getDroppedSequences()` |

## Marked `TableDiff` public properties as internal.

The public properties of the `TableDiff` class have been marked as internal. Use the following corresponding methods
Expand Down
4 changes: 4 additions & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,10 @@
TODO: remove in 4.0.0
-->
<referencedProperty name="Doctrine\DBAL\Schema\TableDiff::$name"/>
<!--
TODO: remove in 4.0.0
-->
<referencedProperty name="Doctrine\DBAL\Schema\SchemaDiff::$orphanedForeignKeys"/>
</errorLevel>
</DeprecatedProperty>
<DocblockTypeContradiction>
Expand Down
54 changes: 38 additions & 16 deletions src/Schema/Comparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,16 @@ private function doCompareSchemas(
Schema $fromSchema,
Schema $toSchema
) {
$diff = new SchemaDiff();
$diff->fromSchema = $fromSchema;
$createdSchemas = [];
$droppedSchemas = [];
$createdTables = [];
$alteredTables = [];
$droppedTables = [];
$createdSequences = [];
$alteredSequences = [];
$droppedSequences = [];

$orphanedForeignKeys = [];

$foreignKeysToTable = [];

Expand All @@ -98,29 +106,29 @@ private function doCompareSchemas(
continue;
}

$diff->newNamespaces[$namespace] = $namespace;
$createdSchemas[$namespace] = $namespace;
}

foreach ($fromSchema->getNamespaces() as $namespace) {
if ($toSchema->hasNamespace($namespace)) {
continue;
}

$diff->removedNamespaces[$namespace] = $namespace;
$droppedSchemas[$namespace] = $namespace;
}

foreach ($toSchema->getTables() as $table) {
$tableName = $table->getShortestName($toSchema->getName());
if (! $fromSchema->hasTable($tableName)) {
$diff->newTables[$tableName] = $toSchema->getTable($tableName);
$createdTables[$tableName] = $toSchema->getTable($tableName);
} else {
$tableDifferences = $this->diffTable(
$fromSchema->getTable($tableName),
$toSchema->getTable($tableName),
);

if ($tableDifferences !== false) {
$diff->changedTables[$tableName] = $tableDifferences;
$alteredTables[$tableName] = $tableDifferences;
}
}
}
Expand All @@ -131,7 +139,7 @@ private function doCompareSchemas(

$table = $fromSchema->getTable($tableName);
if (! $toSchema->hasTable($tableName)) {
$diff->removedTables[$tableName] = $table;
$droppedTables[$tableName] = $table;
}

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

foreach ($diff->removedTables as $tableName => $table) {
foreach ($droppedTables as $tableName => $table) {
if (! isset($foreignKeysToTable[$tableName])) {
continue;
}

foreach ($foreignKeysToTable[$tableName] as $foreignKey) {
if (isset($diff->removedTables[strtolower($foreignKey->getLocalTableName())])) {
if (isset($droppedTables[strtolower($foreignKey->getLocalTableName())])) {
continue;
}

$diff->orphanedForeignKeys[] = $foreignKey;
$orphanedForeignKeys[] = $foreignKey;
}

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

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

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

$diff->changedTables[$localTableName]->unsetDroppedForeignKey($droppedForeignKey);
$alteredTables[$localTableName]->unsetDroppedForeignKey($droppedForeignKey);
}
}
}
Expand All @@ -184,11 +192,11 @@ private function doCompareSchemas(
$sequenceName = $sequence->getShortestName($toSchema->getName());
if (! $fromSchema->hasSequence($sequenceName)) {
if (! $this->isAutoIncrementSequenceInSchema($fromSchema, $sequence)) {
$diff->newSequences[] = $sequence;
$createdSequences[] = $sequence;
}
} else {
if ($this->diffSequence($sequence, $fromSchema->getSequence($sequenceName))) {
$diff->changedSequences[] = $toSchema->getSequence($sequenceName);
$alteredSequences[] = $toSchema->getSequence($sequenceName);
}
}
}
Expand All @@ -204,9 +212,23 @@ private function doCompareSchemas(
continue;
}

$diff->removedSequences[] = $sequence;
$droppedSequences[] = $sequence;
}

$diff = new SchemaDiff(
$createdTables,
$alteredTables,
$droppedTables,
$fromSchema,
$createdSchemas,
$droppedSchemas,
$createdSequences,
$alteredSequences,
$droppedSequences,
);

$diff->orphanedForeignKeys = $orphanedForeignKeys;

return $diff;
}

Expand Down
Loading

0 comments on commit de5042f

Please sign in to comment.