diff --git a/src/Platforms/MySQL/Comparator.php b/src/Platforms/MySQL/Comparator.php index f5c87b6d741..2d70416ba4c 100644 --- a/src/Platforms/MySQL/Comparator.php +++ b/src/Platforms/MySQL/Comparator.php @@ -30,11 +30,11 @@ public function __construct( parent::__construct($platform); } - public function diffTable(Table $fromTable, Table $toTable): ?TableDiff + public function diffTable(Table $oldTable, Table $newTable): ?TableDiff { return parent::diffTable( - $this->normalizeTable($fromTable), - $this->normalizeTable($toTable), + $this->normalizeTable($oldTable), + $this->normalizeTable($newTable), ); } diff --git a/src/Platforms/SQLServer/Comparator.php b/src/Platforms/SQLServer/Comparator.php index 3165b1e8be7..47eb7313e96 100644 --- a/src/Platforms/SQLServer/Comparator.php +++ b/src/Platforms/SQLServer/Comparator.php @@ -22,15 +22,15 @@ public function __construct(SQLServerPlatform $platform, private readonly string parent::__construct($platform); } - public function diffTable(Table $fromTable, Table $toTable): ?TableDiff + public function diffTable(Table $oldTable, Table $newTable): ?TableDiff { - $fromTable = clone $fromTable; - $toTable = clone $toTable; + $oldTable = clone $oldTable; + $newTable = clone $newTable; - $this->normalizeColumns($fromTable); - $this->normalizeColumns($toTable); + $this->normalizeColumns($oldTable); + $this->normalizeColumns($newTable); - return parent::diffTable($fromTable, $toTable); + return parent::diffTable($oldTable, $newTable); } private function normalizeColumns(Table $table): void diff --git a/src/Platforms/SQLite/Comparator.php b/src/Platforms/SQLite/Comparator.php index 2d2a940397e..df48231e782 100644 --- a/src/Platforms/SQLite/Comparator.php +++ b/src/Platforms/SQLite/Comparator.php @@ -24,15 +24,15 @@ public function __construct(SQLitePlatform $platform) parent::__construct($platform); } - public function diffTable(Table $fromTable, Table $toTable): ?TableDiff + public function diffTable(Table $oldTable, Table $newTable): ?TableDiff { - $fromTable = clone $fromTable; - $toTable = clone $toTable; + $oldTable = clone $oldTable; + $newTable = clone $newTable; - $this->normalizeColumns($fromTable); - $this->normalizeColumns($toTable); + $this->normalizeColumns($oldTable); + $this->normalizeColumns($newTable); - return parent::diffTable($fromTable, $toTable); + return parent::diffTable($oldTable, $newTable); } private function normalizeColumns(Table $table): void diff --git a/src/Platforms/SQLitePlatform.php b/src/Platforms/SQLitePlatform.php index 10472a95d30..ff2b19f0927 100644 --- a/src/Platforms/SQLitePlatform.php +++ b/src/Platforms/SQLitePlatform.php @@ -785,11 +785,11 @@ private function getSimpleAlterTableSQL(TableDiff $diff): array|false } /** @return string[] */ - private function getColumnNamesInAlteredTable(TableDiff $diff, Table $fromTable): array + private function getColumnNamesInAlteredTable(TableDiff $diff, Table $oldTable): array { $columns = []; - foreach ($fromTable->getColumns() as $column) { + foreach ($oldTable->getColumns() as $column) { $columnName = $column->getName(); $columns[strtolower($columnName)] = $columnName; } @@ -825,10 +825,10 @@ private function getColumnNamesInAlteredTable(TableDiff $diff, Table $fromTable) } /** @return Index[] */ - private function getIndexesInAlteredTable(TableDiff $diff, Table $fromTable): array + private function getIndexesInAlteredTable(TableDiff $diff, Table $oldTable): array { - $indexes = $fromTable->getIndexes(); - $columnNames = $this->getColumnNamesInAlteredTable($diff, $fromTable); + $indexes = $oldTable->getIndexes(); + $columnNames = $this->getColumnNamesInAlteredTable($diff, $oldTable); foreach ($indexes as $key => $index) { foreach ($diff->getRenamedIndexes() as $oldIndexName => $renamedIndex) { @@ -899,10 +899,10 @@ private function getIndexesInAlteredTable(TableDiff $diff, Table $fromTable): ar } /** @return ForeignKeyConstraint[] */ - private function getForeignKeysInAlteredTable(TableDiff $diff, Table $fromTable): array + private function getForeignKeysInAlteredTable(TableDiff $diff, Table $oldTable): array { - $foreignKeys = $fromTable->getForeignKeys(); - $columnNames = $this->getColumnNamesInAlteredTable($diff, $fromTable); + $foreignKeys = $oldTable->getForeignKeys(); + $columnNames = $this->getColumnNamesInAlteredTable($diff, $oldTable); foreach ($foreignKeys as $key => $constraint) { $changed = false; @@ -959,11 +959,11 @@ private function getForeignKeysInAlteredTable(TableDiff $diff, Table $fromTable) } /** @return Index[] */ - private function getPrimaryIndexInAlteredTable(TableDiff $diff, Table $fromTable): array + private function getPrimaryIndexInAlteredTable(TableDiff $diff, Table $oldTable): array { $primaryIndex = []; - foreach ($this->getIndexesInAlteredTable($diff, $fromTable) as $index) { + foreach ($this->getIndexesInAlteredTable($diff, $oldTable) as $index) { if (! $index->isPrimary()) { continue; } diff --git a/src/Schema/AbstractSchemaManager.php b/src/Schema/AbstractSchemaManager.php index 2ad2563745b..a5175476e11 100644 --- a/src/Schema/AbstractSchemaManager.php +++ b/src/Schema/AbstractSchemaManager.php @@ -606,10 +606,10 @@ public function alterSchema(SchemaDiff $schemaDiff): void * * @throws Exception */ - public function migrateSchema(Schema $toSchema): void + public function migrateSchema(Schema $newSchema): void { $schemaDiff = $this->createComparator() - ->compareSchemas($this->introspectSchema(), $toSchema); + ->compareSchemas($this->introspectSchema(), $newSchema); $this->alterSchema($schemaDiff); } diff --git a/src/Schema/Comparator.php b/src/Schema/Comparator.php index 0404560f8f7..a1c62b54639 100644 --- a/src/Schema/Comparator.php +++ b/src/Schema/Comparator.php @@ -23,9 +23,9 @@ public function __construct(private readonly AbstractPlatform $platform) } /** - * Returns a SchemaDiff object containing the differences between the schemas $fromSchema and $toSchema. + * Returns the differences between the schemas. */ - public function compareSchemas(Schema $fromSchema, Schema $toSchema): SchemaDiff + public function compareSchemas(Schema $oldSchema, Schema $newSchema): SchemaDiff { $createdSchemas = []; $droppedSchemas = []; @@ -36,30 +36,30 @@ public function compareSchemas(Schema $fromSchema, Schema $toSchema): SchemaDiff $alteredSequences = []; $droppedSequences = []; - foreach ($toSchema->getNamespaces() as $namespace) { - if ($fromSchema->hasNamespace($namespace)) { + foreach ($newSchema->getNamespaces() as $newNamespace) { + if ($oldSchema->hasNamespace($newNamespace)) { continue; } - $createdSchemas[] = $namespace; + $createdSchemas[] = $newNamespace; } - foreach ($fromSchema->getNamespaces() as $namespace) { - if ($toSchema->hasNamespace($namespace)) { + foreach ($oldSchema->getNamespaces() as $oldNamespace) { + if ($newSchema->hasNamespace($oldNamespace)) { continue; } - $droppedSchemas[] = $namespace; + $droppedSchemas[] = $oldNamespace; } - foreach ($toSchema->getTables() as $table) { - $tableName = $table->getShortestName($toSchema->getName()); - if (! $fromSchema->hasTable($tableName)) { - $createdTables[] = $toSchema->getTable($tableName); + foreach ($newSchema->getTables() as $newTable) { + $newTableName = $newTable->getShortestName($newSchema->getName()); + if (! $oldSchema->hasTable($newTableName)) { + $createdTables[] = $newSchema->getTable($newTableName); } else { $tableDifferences = $this->diffTable( - $fromSchema->getTable($tableName), - $toSchema->getTable($tableName), + $oldSchema->getTable($newTableName), + $newSchema->getTable($newTableName), ); if ($tableDifferences !== null) { @@ -68,43 +68,43 @@ public function compareSchemas(Schema $fromSchema, Schema $toSchema): SchemaDiff } } - /* Check if there are tables removed */ - foreach ($fromSchema->getTables() as $table) { - $tableName = $table->getShortestName($fromSchema->getName()); + // Check if there are tables removed + foreach ($oldSchema->getTables() as $oldTable) { + $oldTableName = $oldTable->getShortestName($oldSchema->getName()); - $table = $fromSchema->getTable($tableName); - if ($toSchema->hasTable($tableName)) { + $oldTable = $oldSchema->getTable($oldTableName); + if ($newSchema->hasTable($oldTableName)) { continue; } - $droppedTables[] = $table; + $droppedTables[] = $oldTable; } - foreach ($toSchema->getSequences() as $sequence) { - $sequenceName = $sequence->getShortestName($toSchema->getName()); - if (! $fromSchema->hasSequence($sequenceName)) { - if (! $this->isAutoIncrementSequenceInSchema($fromSchema, $sequence)) { - $createdSequences[] = $sequence; + foreach ($newSchema->getSequences() as $newSequence) { + $newSequenceName = $newSequence->getShortestName($newSchema->getName()); + if (! $oldSchema->hasSequence($newSequenceName)) { + if (! $this->isAutoIncrementSequenceInSchema($oldSchema, $newSequence)) { + $createdSequences[] = $newSequence; } } else { - if ($this->diffSequence($sequence, $fromSchema->getSequence($sequenceName))) { - $alteredSequences[] = $toSchema->getSequence($sequenceName); + if ($this->diffSequence($newSequence, $oldSchema->getSequence($newSequenceName))) { + $alteredSequences[] = $newSchema->getSequence($newSequenceName); } } } - foreach ($fromSchema->getSequences() as $sequence) { - if ($this->isAutoIncrementSequenceInSchema($toSchema, $sequence)) { + foreach ($oldSchema->getSequences() as $oldSequence) { + if ($this->isAutoIncrementSequenceInSchema($newSchema, $oldSequence)) { continue; } - $sequenceName = $sequence->getShortestName($fromSchema->getName()); + $oldSequenceName = $oldSequence->getShortestName($oldSchema->getName()); - if ($toSchema->hasSequence($sequenceName)) { + if ($newSchema->hasSequence($oldSequenceName)) { continue; } - $droppedSequences[] = $sequence; + $droppedSequences[] = $oldSequence; } return new SchemaDiff( @@ -140,13 +140,13 @@ public function diffSequence(Sequence $sequence1, Sequence $sequence2): bool } /** - * Returns the difference between the tables $fromTable and $toTable. + * Returns the difference between the tables. * * If there are no differences this method returns null. * * @throws Exception */ - public function diffTable(Table $fromTable, Table $toTable): ?TableDiff + public function diffTable(Table $oldTable, Table $newTable): ?TableDiff { $hasChanges = false; @@ -160,115 +160,115 @@ public function diffTable(Table $fromTable, Table $toTable): ?TableDiff $modifiedForeignKeys = []; $droppedForeignKeys = []; - $fromTableColumns = $fromTable->getColumns(); - $toTableColumns = $toTable->getColumns(); + $oldColumns = $oldTable->getColumns(); + $newColumns = $newTable->getColumns(); - /* See if all the columns in "from" table exist in "to" table */ - foreach ($toTableColumns as $column) { - $columnName = strtolower($column->getName()); + // See if all the columns in the old table exist in the new table + foreach ($newColumns as $newColumn) { + $newColumnName = strtolower($newColumn->getName()); - if ($fromTable->hasColumn($columnName)) { + if ($oldTable->hasColumn($newColumnName)) { continue; } - $addedColumns[$columnName] = $column; + $addedColumns[$newColumnName] = $newColumn; $hasChanges = true; } - /* See if there are any removed columns in "to" table */ - foreach ($fromTableColumns as $column) { - $columnName = strtolower($column->getName()); + // See if there are any removed columns in the new table + foreach ($oldColumns as $oldColumn) { + $oldColumnName = strtolower($oldColumn->getName()); - // See if column is removed in "to" table. - if (! $toTable->hasColumn($columnName)) { - $droppedColumns[$columnName] = $column; + // See if column is removed in the new table. + if (! $newTable->hasColumn($oldColumnName)) { + $droppedColumns[$oldColumnName] = $oldColumn; $hasChanges = true; continue; } - $toColumn = $toTable->getColumn($columnName); + $newColumn = $newTable->getColumn($oldColumnName); - if ($this->columnsEqual($column, $toColumn)) { + if ($this->columnsEqual($oldColumn, $newColumn)) { continue; } - $modifiedColumns[] = new ColumnDiff($column, $toColumn); + $modifiedColumns[] = new ColumnDiff($oldColumn, $newColumn); $hasChanges = true; } $renamedColumns = $this->detectRenamedColumns($addedColumns, $droppedColumns); - $fromTableIndexes = $fromTable->getIndexes(); - $toTableIndexes = $toTable->getIndexes(); + $oldIndexes = $oldTable->getIndexes(); + $newIndexes = $newTable->getIndexes(); - /* See if all the indexes in "from" table exist in "to" table */ - foreach ($toTableIndexes as $indexName => $index) { - if (($index->isPrimary() && $fromTable->getPrimaryKey() !== null) || $fromTable->hasIndex($indexName)) { + // See if all the indexes from the old table exist in the new one + foreach ($newIndexes as $newIndexName => $newIndex) { + if (($newIndex->isPrimary() && $oldTable->getPrimaryKey() !== null) || $oldTable->hasIndex($newIndexName)) { continue; } - $addedIndexes[$indexName] = $index; + $addedIndexes[$newIndexName] = $newIndex; $hasChanges = true; } - /* See if there are any removed indexes in "to" table */ - foreach ($fromTableIndexes as $indexName => $index) { - // See if index is removed in "to" table. + // See if there are any removed indexes in the new table + foreach ($oldIndexes as $oldIndexName => $oldIndex) { + // See if the index is removed in the new table. if ( - ($index->isPrimary() && $toTable->getPrimaryKey() === null) || - ! $index->isPrimary() && ! $toTable->hasIndex($indexName) + ($oldIndex->isPrimary() && $newTable->getPrimaryKey() === null) || + ! $oldIndex->isPrimary() && ! $newTable->hasIndex($oldIndexName) ) { - $droppedIndexes[$indexName] = $index; + $droppedIndexes[$oldIndexName] = $oldIndex; $hasChanges = true; continue; } - // See if index has changed in "to" table. - $toTableIndex = $index->isPrimary() ? $toTable->getPrimaryKey() : $toTable->getIndex($indexName); - assert($toTableIndex instanceof Index); + // See if index has changed in the new table. + $newIndex = $oldIndex->isPrimary() ? $newTable->getPrimaryKey() : $newTable->getIndex($oldIndexName); + assert($newIndex instanceof Index); - if (! $this->diffIndex($index, $toTableIndex)) { + if (! $this->diffIndex($oldIndex, $newIndex)) { continue; } - $modifiedIndexes[] = $toTableIndex; + $modifiedIndexes[] = $newIndex; $hasChanges = true; } $renamedIndexes = $this->detectRenamedIndexes($addedIndexes, $droppedIndexes); - $fromForeignKeys = $fromTable->getForeignKeys(); - $toForeignKeys = $toTable->getForeignKeys(); + $oldForeignKeys = $oldTable->getForeignKeys(); + $newForeignKeys = $newTable->getForeignKeys(); - foreach ($fromForeignKeys as $fromKey => $fromConstraint) { - foreach ($toForeignKeys as $toKey => $toConstraint) { - if ($this->diffForeignKey($fromConstraint, $toConstraint) === false) { - unset($fromForeignKeys[$fromKey], $toForeignKeys[$toKey]); + foreach ($oldForeignKeys as $oldKey => $oldForeignKey) { + foreach ($newForeignKeys as $newKey => $newForeignKey) { + if ($this->diffForeignKey($oldForeignKey, $newForeignKey) === false) { + unset($oldForeignKeys[$oldKey], $newForeignKeys[$newKey]); } else { - if (strtolower($fromConstraint->getName()) === strtolower($toConstraint->getName())) { - $modifiedForeignKeys[] = $toConstraint; + if (strtolower($oldForeignKey->getName()) === strtolower($newForeignKey->getName())) { + $modifiedForeignKeys[] = $newForeignKey; $hasChanges = true; - unset($fromForeignKeys[$fromKey], $toForeignKeys[$toKey]); + unset($oldForeignKeys[$oldKey], $newForeignKeys[$newKey]); } } } } - foreach ($fromForeignKeys as $fromConstraint) { - $droppedForeignKeys[] = $fromConstraint; + foreach ($oldForeignKeys as $oldForeignKey) { + $droppedForeignKeys[] = $oldForeignKey; $hasChanges = true; } - foreach ($toForeignKeys as $toConstraint) { - $addedForeignKeys[] = $toConstraint; + foreach ($newForeignKeys as $newForeignKey) { + $addedForeignKeys[] = $newForeignKey; $hasChanges = true; } @@ -278,7 +278,7 @@ public function diffTable(Table $fromTable, Table $toTable): ?TableDiff } return new TableDiff( - $fromTable, + $oldTable, $addedColumns, $modifiedColumns, $droppedColumns, diff --git a/tests/Functional/Schema/OracleSchemaManagerTest.php b/tests/Functional/Schema/OracleSchemaManagerTest.php index e1b733c21a3..6bb401c38ce 100644 --- a/tests/Functional/Schema/OracleSchemaManagerTest.php +++ b/tests/Functional/Schema/OracleSchemaManagerTest.php @@ -259,12 +259,12 @@ public function testQuotedTableNameRemainsQuotedInSchema(): void $schemaManager = $this->connection->createSchemaManager(); - $fromSchema = $schemaManager->introspectSchema(); - $toSchema = clone $fromSchema; + $oldSchema = $schemaManager->introspectSchema(); + $newSchema = clone $oldSchema; - $toSchema->getTable('"tester"')->dropColumn('"name"'); + $newSchema->getTable('"tester"')->dropColumn('"name"'); $diff = $schemaManager->createComparator() - ->compareSchemas($fromSchema, $toSchema); + ->compareSchemas($oldSchema, $newSchema); $schemaManager->alterSchema($diff); diff --git a/tests/Functional/Schema/PostgreSQLSchemaManagerTest.php b/tests/Functional/Schema/PostgreSQLSchemaManagerTest.php index 957822cd56b..d9683223960 100644 --- a/tests/Functional/Schema/PostgreSQLSchemaManagerTest.php +++ b/tests/Functional/Schema/PostgreSQLSchemaManagerTest.php @@ -431,19 +431,20 @@ public function testAutoIncrementCreatesSerialDataTypesWithoutADefaultValueEvenW /** @dataProvider autoIncrementTypeMigrations */ public function testAlterTableAutoIncrementIntToBigInt(string $from, string $to, string $expected): void { - $tableFrom = new Table('autoinc_type_modification'); - $column = $tableFrom->addColumn('id', $from); + $table = new Table('autoinc_type_modification'); + $column = $table->addColumn('id', $from); $column->setAutoincrement(true); - $this->dropAndCreateTable($tableFrom); - $tableFrom = $this->schemaManager->introspectTable('autoinc_type_modification'); - self::assertTrue($tableFrom->getColumn('id')->getAutoincrement()); + $this->dropAndCreateTable($table); - $tableTo = new Table('autoinc_type_modification'); - $column = $tableTo->addColumn('id', $to); + $oldTable = $this->schemaManager->introspectTable('autoinc_type_modification'); + self::assertTrue($oldTable->getColumn('id')->getAutoincrement()); + + $newTable = new Table('autoinc_type_modification'); + $column = $newTable->addColumn('id', $to); $column->setAutoincrement(true); $diff = $this->schemaManager->createComparator() - ->diffTable($tableFrom, $tableTo); + ->diffTable($oldTable, $newTable); self::assertNotNull($diff); self::assertSame( ['ALTER TABLE autoinc_type_modification ALTER id TYPE ' . $expected], diff --git a/tests/Schema/ComparatorTest.php b/tests/Schema/ComparatorTest.php index 8aa3f9ada68..81859a8046a 100644 --- a/tests/Schema/ComparatorTest.php +++ b/tests/Schema/ComparatorTest.php @@ -634,30 +634,30 @@ public function testAutoIncrementNoSequences(): void public function testComparesNamespaces(): void { - $fromSchema = $this->getMockBuilder(Schema::class) + $oldSchema = $this->getMockBuilder(Schema::class) ->onlyMethods(['getNamespaces', 'hasNamespace']) ->getMock(); - $toSchema = $this->getMockBuilder(Schema::class) + $newSchema = $this->getMockBuilder(Schema::class) ->onlyMethods(['getNamespaces', 'hasNamespace']) ->getMock(); - $fromSchema->expects(self::once()) + $oldSchema->expects(self::once()) ->method('getNamespaces') ->willReturn(['foo', 'bar']); - $fromSchema->method('hasNamespace') + $oldSchema->method('hasNamespace') ->withConsecutive(['bar'], ['baz']) ->willReturnOnConsecutiveCalls(true, false); - $toSchema->expects(self::once()) + $newSchema->expects(self::once()) ->method('getNamespaces') ->willReturn(['bar', 'baz']); - $toSchema->method('hasNamespace') + $newSchema->method('hasNamespace') ->withConsecutive(['foo'], ['bar']) ->willReturnOnConsecutiveCalls(false, true); - $diff = $this->comparator->compareSchemas($fromSchema, $toSchema); + $diff = $this->comparator->compareSchemas($oldSchema, $newSchema); self::assertEquals(['baz'], $diff->getCreatedSchemas()); self::assertEquals(['foo'], $diff->getDroppedSchemas()); @@ -698,7 +698,7 @@ public static function getCompareColumnComments(): iterable public function testForeignKeyRemovalWithRenamedLocalColumn(): void { - $fromSchema = new Schema([ + $oldSchema = new Schema([ 'table1' => new Table( 'table1', [ @@ -718,7 +718,7 @@ public function testForeignKeyRemovalWithRenamedLocalColumn(): void ], ), ]); - $toSchema = new Schema([ + $newSchema = new Schema([ 'table2' => new Table( 'table2', [ @@ -739,7 +739,7 @@ public function testForeignKeyRemovalWithRenamedLocalColumn(): void ), ]); - $schemaDiff = $this->comparator->compareSchemas($fromSchema, $toSchema); + $schemaDiff = $this->comparator->compareSchemas($oldSchema, $newSchema); $alteredTables = $schemaDiff->getAlteredTables(); self::assertCount(1, $alteredTables); @@ -751,7 +751,7 @@ public function testForeignKeyRemovalWithRenamedLocalColumn(): void public function testWillNotProduceSchemaDiffOnTableWithAddedCustomSchemaDefinition(): void { - $fromSchema = new Schema( + $oldSchema = new Schema( [ new Table( 'a_table', @@ -765,7 +765,7 @@ public function testWillNotProduceSchemaDiffOnTableWithAddedCustomSchemaDefiniti ), ], ); - $toSchema = new Schema( + $newSchema = new Schema( [ new Table( 'a_table', @@ -780,7 +780,7 @@ public function testWillNotProduceSchemaDiffOnTableWithAddedCustomSchemaDefiniti ); self::assertEmpty( - $this->comparator->compareSchemas($fromSchema, $toSchema) + $this->comparator->compareSchemas($oldSchema, $newSchema) ->getAlteredTables(), 'Schema diff is empty, since only `columnDefinition` changed from `null` (not detected) to a defined one', );