From e262dcd8b8b4cfdc045993a8f79fd9304997df51 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Mon, 10 Oct 2022 22:05:36 +0200 Subject: [PATCH 1/3] Allow doctrine/event-manager 2 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 647b4a83cdb..2c5a40a2b81 100644 --- a/composer.json +++ b/composer.json @@ -35,7 +35,7 @@ "composer-runtime-api": "^2", "doctrine/cache": "^1.11|^2.0", "doctrine/deprecations": "^0.5.3|^1", - "doctrine/event-manager": "^1.0", + "doctrine/event-manager": "^1|^2", "psr/cache": "^1|^2|^3", "psr/log": "^1|^2|^3" }, From c964ebfc6368975add3168d2568bf9b6f3a97f82 Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Wed, 12 Oct 2022 07:34:53 -0700 Subject: [PATCH 2/3] Deprecate orphaned foreign keys --- UPGRADE.md | 5 +++++ src/Schema/SchemaDiff.php | 6 +++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/UPGRADE.md b/UPGRADE.md index 45bb274ab54..4a8b42558ee 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -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()` diff --git a/src/Schema/SchemaDiff.php b/src/Schema/SchemaDiff.php index 38e3782d3ca..2d69ab365bc 100644 --- a/src/Schema/SchemaDiff.php +++ b/src/Schema/SchemaDiff.php @@ -65,7 +65,11 @@ class SchemaDiff /** @var Sequence[] */ public $removedSequences = []; - /** @var ForeignKeyConstraint[] */ + /** + * @deprecated + * + * @var ForeignKeyConstraint[] + */ public $orphanedForeignKeys = []; /** From b272ec9ce9711eb666049962f47156c14af619d7 Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Wed, 12 Oct 2022 08:14:59 -0700 Subject: [PATCH 3/3] Mark SchemaDiff public properties internal --- UPGRADE.md | 16 +++++ psalm.xml.dist | 4 ++ src/Schema/Comparator.php | 54 +++++++++++----- src/Schema/SchemaDiff.php | 127 ++++++++++++++++++++++++++++++++------ 4 files changed, 166 insertions(+), 35 deletions(-) diff --git a/UPGRADE.md b/UPGRADE.md index 4a8b42558ee..3661c8090c3 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -139,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 diff --git a/psalm.xml.dist b/psalm.xml.dist index 62ffa0a020a..ffcc8e73b74 100644 --- a/psalm.xml.dist +++ b/psalm.xml.dist @@ -491,6 +491,10 @@ TODO: remove in 4.0.0 --> + + diff --git a/src/Schema/Comparator.php b/src/Schema/Comparator.php index 924fce269ff..d0bb6d2e3fd 100644 --- a/src/Schema/Comparator.php +++ b/src/Schema/Comparator.php @@ -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 = []; @@ -98,7 +106,7 @@ private function doCompareSchemas( continue; } - $diff->newNamespaces[$namespace] = $namespace; + $createdSchemas[$namespace] = $namespace; } foreach ($fromSchema->getNamespaces() as $namespace) { @@ -106,13 +114,13 @@ private function doCompareSchemas( 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), @@ -120,7 +128,7 @@ private function doCompareSchemas( ); if ($tableDifferences !== false) { - $diff->changedTables[$tableName] = $tableDifferences; + $alteredTables[$tableName] = $tableDifferences; } } } @@ -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 @@ -145,17 +153,17 @@ 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 @@ -163,11 +171,11 @@ private function doCompareSchemas( 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. @@ -175,7 +183,7 @@ private function doCompareSchemas( continue; } - $diff->changedTables[$localTableName]->unsetDroppedForeignKey($droppedForeignKey); + $alteredTables[$localTableName]->unsetDroppedForeignKey($droppedForeignKey); } } } @@ -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); } } } @@ -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; } diff --git a/src/Schema/SchemaDiff.php b/src/Schema/SchemaDiff.php index 2d69ab365bc..04e6d84bde0 100644 --- a/src/Schema/SchemaDiff.php +++ b/src/Schema/SchemaDiff.php @@ -24,6 +24,8 @@ class SchemaDiff /** * All added namespaces. * + * @internal Use {@link getCreatedSchemas()} instead. + * * @var string[] */ public $newNamespaces = []; @@ -31,6 +33,8 @@ class SchemaDiff /** * All removed namespaces. * + * @internal Use {@link getDroppedSchemas()} instead. + * * @var string[] */ public $removedNamespaces = []; @@ -38,6 +42,8 @@ class SchemaDiff /** * All added tables. * + * @internal Use {@link getCreatedTables()} instead. + * * @var Table[] */ public $newTables = []; @@ -45,6 +51,8 @@ class SchemaDiff /** * All changed tables. * + * @internal Use {@link getAlteredTables()} instead. + * * @var TableDiff[] */ public $changedTables = []; @@ -52,17 +60,31 @@ class SchemaDiff /** * All removed tables. * + * @internal Use {@link getDroppedTables()} instead. + * * @var Table[] */ public $removedTables = []; - /** @var Sequence[] */ + /** + * @internal Use {@link getCreatedSequences()} instead. + * + * @var Sequence[] + */ public $newSequences = []; - /** @var Sequence[] */ + /** + * @internal Use {@link getAlteredSequences()} instead. + * + * @var Sequence[] + */ public $changedSequences = []; - /** @var Sequence[] */ + /** + * @internal Use {@link getDroppedSequences()} instead. + * + * @var Sequence[] + */ public $removedSequences = []; /** @@ -77,16 +99,83 @@ class SchemaDiff * * @internal The diff can be only instantiated by a {@see Comparator}. * - * @param Table[] $newTables - * @param TableDiff[] $changedTables - * @param Table[] $removedTables + * @param Table[] $newTables + * @param TableDiff[] $changedTables + * @param Table[] $removedTables + * @param array $createdSchemas + * @param array $droppedSchemas + * @param array $createdSequences + * @param array $alteredSequences + * @param array $droppedSequences */ - public function __construct($newTables = [], $changedTables = [], $removedTables = [], ?Schema $fromSchema = null) + public function __construct( + $newTables = [], + $changedTables = [], + $removedTables = [], + ?Schema $fromSchema = null, + $createdSchemas = [], + $droppedSchemas = [], + $createdSequences = [], + $alteredSequences = [], + $droppedSequences = [] + ) { + $this->newTables = $newTables; + $this->changedTables = $changedTables; + $this->removedTables = $removedTables; + $this->fromSchema = $fromSchema; + $this->newNamespaces = $createdSchemas; + $this->removedNamespaces = $droppedSchemas; + $this->newSequences = $createdSequences; + $this->changedSequences = $alteredSequences; + $this->removedSequences = $droppedSequences; + } + + /** @return array */ + public function getCreatedSchemas(): array + { + return $this->newNamespaces; + } + + /** @return array */ + public function getDroppedSchemas(): array + { + return $this->removedNamespaces; + } + + /** @return array */ + public function getCreatedTables(): array + { + return $this->newTables; + } + + /** @return array */ + public function getAlteredTables(): array + { + return $this->changedTables; + } + + /** @return array
*/ + public function getDroppedTables(): array + { + return $this->removedTables; + } + + /** @return array */ + public function getCreatedSequences(): array + { + return $this->newSequences; + } + + /** @return array */ + public function getAlteredSequences(): array + { + return $this->changedSequences; + } + + /** @return array */ + public function getDroppedSequences(): array { - $this->newTables = $newTables; - $this->changedTables = $changedTables; - $this->removedTables = $removedTables; - $this->fromSchema = $fromSchema; + return $this->removedSequences; } /** @@ -121,8 +210,8 @@ protected function _toSql(AbstractPlatform $platform, $saveMode = false) $sql = []; if ($platform->supportsSchemas()) { - foreach ($this->newNamespaces as $newNamespace) { - $sql[] = $platform->getCreateSchemaSQL($newNamespace); + foreach ($this->getCreatedSchemas() as $schema) { + $sql[] = $platform->getCreateSchemaSQL($schema); } } @@ -133,28 +222,28 @@ protected function _toSql(AbstractPlatform $platform, $saveMode = false) } if ($platform->supportsSequences() === true) { - foreach ($this->changedSequences as $sequence) { + foreach ($this->getAlteredSequences() as $sequence) { $sql[] = $platform->getAlterSequenceSQL($sequence); } if ($saveMode === false) { - foreach ($this->removedSequences as $sequence) { + foreach ($this->getDroppedSequences() as $sequence) { $sql[] = $platform->getDropSequenceSQL($sequence); } } - foreach ($this->newSequences as $sequence) { + foreach ($this->getCreatedSequences() as $sequence) { $sql[] = $platform->getCreateSequenceSQL($sequence); } } - $sql = array_merge($sql, $platform->getCreateTablesSQL($this->newTables)); + $sql = array_merge($sql, $platform->getCreateTablesSQL($this->getCreatedTables())); if ($saveMode === false) { - $sql = array_merge($sql, $platform->getDropTablesSQL($this->removedTables)); + $sql = array_merge($sql, $platform->getDropTablesSQL($this->getDroppedTables())); } - foreach ($this->changedTables as $tableDiff) { + foreach ($this->getAlteredTables() as $tableDiff) { $sql = array_merge($sql, $platform->getAlterTableSQL($tableDiff)); }