diff --git a/src/Schema/Comparator.php b/src/Schema/Comparator.php index 81e632b82eb..0404560f8f7 100644 --- a/src/Schema/Comparator.php +++ b/src/Schema/Comparator.php @@ -41,7 +41,7 @@ public function compareSchemas(Schema $fromSchema, Schema $toSchema): SchemaDiff continue; } - $createdSchemas[$namespace] = $namespace; + $createdSchemas[] = $namespace; } foreach ($fromSchema->getNamespaces() as $namespace) { @@ -49,13 +49,13 @@ public function compareSchemas(Schema $fromSchema, Schema $toSchema): SchemaDiff continue; } - $droppedSchemas[$namespace] = $namespace; + $droppedSchemas[] = $namespace; } foreach ($toSchema->getTables() as $table) { $tableName = $table->getShortestName($toSchema->getName()); if (! $fromSchema->hasTable($tableName)) { - $createdTables[$tableName] = $toSchema->getTable($tableName); + $createdTables[] = $toSchema->getTable($tableName); } else { $tableDifferences = $this->diffTable( $fromSchema->getTable($tableName), @@ -63,7 +63,7 @@ public function compareSchemas(Schema $fromSchema, Schema $toSchema): SchemaDiff ); if ($tableDifferences !== null) { - $alteredTables[$tableName] = $tableDifferences; + $alteredTables[] = $tableDifferences; } } } @@ -77,7 +77,7 @@ public function compareSchemas(Schema $fromSchema, Schema $toSchema): SchemaDiff continue; } - $droppedTables[$tableName] = $table; + $droppedTables[] = $table; } foreach ($toSchema->getSequences() as $sequence) { @@ -108,11 +108,11 @@ public function compareSchemas(Schema $fromSchema, Schema $toSchema): SchemaDiff } return new SchemaDiff( + $createdSchemas, + $droppedSchemas, $createdTables, $alteredTables, $droppedTables, - $createdSchemas, - $droppedSchemas, $createdSequences, $alteredSequences, $droppedSequences, diff --git a/src/Schema/SchemaDiff.php b/src/Schema/SchemaDiff.php index 75fe15b3bbc..9e1f3db7ef5 100644 --- a/src/Schema/SchemaDiff.php +++ b/src/Schema/SchemaDiff.php @@ -14,125 +14,78 @@ */ class SchemaDiff { - /** - * All added namespaces. - * - * @internal Use {@link getCreatedSchemas()} instead. - * - * @var array - */ - public array $newNamespaces = []; - - /** - * All removed namespaces. - * - * @internal Use {@link getDroppedSchemas()} instead. - * - * @var array - */ - public array $removedNamespaces = []; - - /** - * @internal Use {@link getCreatedSequences()} instead. - * - * @var array - */ - public array $newSequences = []; - - /** - * @internal Use {@link getAlteredSequences()} instead. - * - * @var array - */ - public array $changedSequences = []; - - /** - * @internal Use {@link getDroppedSequences()} instead. - * - * @var array - */ - public array $removedSequences = []; - /** * Constructs an SchemaDiff object. * * @internal The diff can be only instantiated by a {@see Comparator}. * - * @param array $newTables - * @param array $changedTables - * @param array $removedTables - * @param array $createdSchemas - * @param array $droppedSchemas - * @param array $createdSequences - * @param array $alteredSequences - * @param array $droppedSequences + * @param array $createdSchemas + * @param array $droppedSchemas + * @param array $createdTables + * @param array $alteredTables + * @param array
$droppedTables + * @param array $createdSequences + * @param array $alteredSequences + * @param array $droppedSequences */ public function __construct( - /** @internal Use {@link getCreatedTables()} instead. */ - public array $newTables = [], - /** @internal Use {@link getAlteredTables()} instead. */ - public array $changedTables = [], - /** @internal Use {@link getDroppedTables()} instead. */ - public array $removedTables = [], - array $createdSchemas = [], - array $droppedSchemas = [], - array $createdSequences = [], - array $alteredSequences = [], - array $droppedSequences = [], + private readonly array $createdSchemas, + private readonly array $droppedSchemas, + private readonly array $createdTables, + private readonly array $alteredTables, + private readonly array $droppedTables, + private readonly array $createdSequences, + private readonly array $alteredSequences, + private readonly array $droppedSequences, ) { - $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 $this->createdSchemas; } /** @return array */ public function getDroppedSchemas(): array { - return $this->removedNamespaces; + return $this->droppedSchemas; } /** @return array
*/ public function getCreatedTables(): array { - return $this->newTables; + return $this->createdTables; } /** @return array */ public function getAlteredTables(): array { - return $this->changedTables; + return $this->alteredTables; } /** @return array
*/ public function getDroppedTables(): array { - return $this->removedTables; + return $this->droppedTables; } /** @return array */ public function getCreatedSequences(): array { - return $this->newSequences; + return $this->createdSequences; } /** @return array */ public function getAlteredSequences(): array { - return $this->changedSequences; + return $this->alteredSequences; } /** @return array */ public function getDroppedSequences(): array { - return $this->removedSequences; + return $this->droppedSequences; } /** diff --git a/tests/Functional/Schema/SchemaManagerFunctionalTestCase.php b/tests/Functional/Schema/SchemaManagerFunctionalTestCase.php index dc2c3e7706c..9f126f92586 100644 --- a/tests/Functional/Schema/SchemaManagerFunctionalTestCase.php +++ b/tests/Functional/Schema/SchemaManagerFunctionalTestCase.php @@ -684,9 +684,7 @@ public function testTableInNamespace(): void self::markTestSkipped('Schema definition is not supported by this platform.'); } - //create schema - $diff = new SchemaDiff(); - $diff->newNamespaces['testschema'] = 'testschema'; + $diff = new SchemaDiff(['testschema'], [], [], [], [], [], [], []); foreach ($diff->toSql($this->connection->getDatabasePlatform()) as $sql) { $this->connection->executeStatement($sql); diff --git a/tests/Schema/ComparatorTest.php b/tests/Schema/ComparatorTest.php index 0d4bdeda39c..8aa3f9ada68 100644 --- a/tests/Schema/ComparatorTest.php +++ b/tests/Schema/ComparatorTest.php @@ -45,7 +45,10 @@ public function testCompareSame1(): void ), ]); - self::assertEquals(new SchemaDiff(), $this->comparator->compareSchemas($schema1, $schema2)); + self::assertEquals( + new SchemaDiff([], [], [], [], [], [], [], []), + $this->comparator->compareSchemas($schema1, $schema2), + ); } public function testCompareSame2(): void @@ -69,7 +72,10 @@ public function testCompareSame2(): void ), ]); - self::assertEquals(new SchemaDiff(), $this->comparator->compareSchemas($schema1, $schema2)); + self::assertEquals( + new SchemaDiff([], [], [], [], [], [], [], []), + $this->comparator->compareSchemas($schema1, $schema2), + ); } public function testCompareMissingTable(): void @@ -82,9 +88,10 @@ public function testCompareMissingTable(): void $schema1 = new Schema([$table], [], $schemaConfig); $schema2 = new Schema([], [], $schemaConfig); - $expected = new SchemaDiff([], [], ['bugdb' => $table]); - - self::assertEquals($expected, $this->comparator->compareSchemas($schema1, $schema2)); + self::assertEquals( + new SchemaDiff([], [], [], [], [$table], [], [], []), + $this->comparator->compareSchemas($schema1, $schema2), + ); } public function testCompareNewTable(): void @@ -97,7 +104,7 @@ public function testCompareNewTable(): void $schema1 = new Schema([], [], $schemaConfig); $schema2 = new Schema([$table], [], $schemaConfig); - $expected = new SchemaDiff(['bugdb' => $table], [], []); + $expected = new SchemaDiff([], [], [$table], [], [], [], [], []); self::assertEquals($expected, $this->comparator->compareSchemas($schema1, $schema2)); } @@ -195,8 +202,7 @@ public function testRemovedSequence(): void $diffSchema = $this->comparator->compareSchemas($schema1, $schema2); - self::assertCount(1, $diffSchema->removedSequences); - self::assertSame($seq, $diffSchema->removedSequences[0]); + self::assertSame([$seq], $diffSchema->getDroppedSequences()); } public function testAddedSequence(): void @@ -208,8 +214,7 @@ public function testAddedSequence(): void $diffSchema = $this->comparator->compareSchemas($schema1, $schema2); - self::assertCount(1, $diffSchema->newSequences); - self::assertSame($seq, $diffSchema->newSequences[0]); + self::assertSame([$seq], $diffSchema->getCreatedSequences()); } public function testTableAddForeignKey(): void @@ -305,7 +310,9 @@ public function testTablesCaseInsensitive(): void $diff = $this->comparator->compareSchemas($schemaA, $schemaB); - $this->assertSchemaTableChangeCount($diff, 1, 0, 1); + self::assertCount(1, $diff->getCreatedTables()); + self::assertCount(0, $diff->getAlteredTables()); + self::assertCount(1, $diff->getDroppedTables()); } public function testSequencesCaseInsensitive(): void @@ -324,7 +331,9 @@ public function testSequencesCaseInsensitive(): void $diff = $this->comparator->compareSchemas($schemaA, $schemaB); - $this->assertSchemaSequenceChangeCount($diff, 1, 0, 1); + self::assertCount(1, $diff->getCreatedSequences()); + self::assertCount(0, $diff->getAlteredSequences()); + self::assertCount(1, $diff->getDroppedSequences()); } public function testCompareColumnCompareCaseInsensitive(): void @@ -501,17 +510,17 @@ public function testDiff(): void self::assertCount(0, $tableDiff->getDroppedColumns()); } - public function testChangedSequence(): void + public function testAlteredSequence(): void { - $schema = new Schema(); - $schema->createSequence('baz'); + $oldSchema = new Schema(); + $oldSchema->createSequence('baz'); - $schemaNew = clone $schema; - $schemaNew->getSequence('baz')->setAllocationSize(20); + $newSchema = clone $oldSchema; + $newSchema->getSequence('baz')->setAllocationSize(20); - $diff = $this->comparator->compareSchemas($schema, $schemaNew); + $diff = $this->comparator->compareSchemas($oldSchema, $newSchema); - self::assertSame($diff->changedSequences[0], $schemaNew->getSequence('baz')); + self::assertSame([$newSchema->getSequence('baz')], $diff->getAlteredSequences()); } public function testFqnSchemaComparison(): void @@ -525,9 +534,10 @@ public function testFqnSchemaComparison(): void $newSchema = new Schema([], [], $config); $newSchema->createTable('foo.bar'); - $expected = new SchemaDiff(); - - self::assertEquals($expected, $this->comparator->compareSchemas($oldSchema, $newSchema)); + self::assertEquals( + new SchemaDiff([], [], [], [], [], [], [], []), + $this->comparator->compareSchemas($oldSchema, $newSchema), + ); } public function testNamespacesComparison(): void @@ -544,13 +554,10 @@ public function testNamespacesComparison(): void $newSchema->createTable('baz.tab'); $newSchema->createTable('war.tab'); - $expected = new SchemaDiff(); - $expected->newNamespaces = ['bar' => 'bar', 'baz' => 'baz']; - $diff = $this->comparator->compareSchemas($oldSchema, $newSchema); - self::assertEquals(['bar' => 'bar', 'baz' => 'baz'], $diff->newNamespaces); - self::assertCount(2, $diff->newTables); + self::assertEquals(['bar', 'baz'], $diff->getCreatedSchemas()); + self::assertCount(2, $diff->getCreatedTables()); } public function testFqnSchemaComparisonDifferentSchemaNameButSameTableNoDiff(): void @@ -564,7 +571,10 @@ public function testFqnSchemaComparisonDifferentSchemaNameButSameTableNoDiff(): $newSchema = new Schema(); $newSchema->createTable('bar'); - self::assertEquals(new SchemaDiff(), $this->comparator->compareSchemas($oldSchema, $newSchema)); + self::assertEquals( + new SchemaDiff([], [], [], [], [], [], [], []), + $this->comparator->compareSchemas($oldSchema, $newSchema), + ); } public function testFqnSchemaComparisonNoSchemaSame(): void @@ -577,7 +587,10 @@ public function testFqnSchemaComparisonNoSchemaSame(): void $newSchema = new Schema(); $newSchema->createTable('bar'); - self::assertEquals(new SchemaDiff(), $this->comparator->compareSchemas($oldSchema, $newSchema)); + self::assertEquals( + new SchemaDiff([], [], [], [], [], [], [], []), + $this->comparator->compareSchemas($oldSchema, $newSchema), + ); } public function testAutoIncrementSequences(): void @@ -595,7 +608,7 @@ public function testAutoIncrementSequences(): void $diff = $this->comparator->compareSchemas($oldSchema, $newSchema); - self::assertCount(0, $diff->removedSequences); + self::assertCount(0, $diff->getDroppedSequences()); } /** @@ -616,29 +629,7 @@ public function testAutoIncrementNoSequences(): void $diff = $this->comparator->compareSchemas($oldSchema, $newSchema); - self::assertCount(0, $diff->newSequences); - } - - public function assertSchemaTableChangeCount( - SchemaDiff $diff, - int $newTableCount = 0, - int $changeTableCount = 0, - int $removeTableCount = 0, - ): void { - self::assertCount($newTableCount, $diff->newTables); - self::assertCount($changeTableCount, $diff->changedTables); - self::assertCount($removeTableCount, $diff->removedTables); - } - - public function assertSchemaSequenceChangeCount( - SchemaDiff $diff, - int $newSequenceCount = 0, - int $changeSequenceCount = 0, - int $removeSequenceCount = 0, - ): void { - self::assertCount($newSequenceCount, $diff->newSequences); - self::assertCount($changeSequenceCount, $diff->changedSequences); - self::assertCount($removeSequenceCount, $diff->removedSequences); + self::assertCount(0, $diff->getCreatedSequences()); } public function testComparesNamespaces(): void @@ -666,11 +657,10 @@ public function testComparesNamespaces(): void ->withConsecutive(['foo'], ['bar']) ->willReturnOnConsecutiveCalls(false, true); - $expected = new SchemaDiff(); - $expected->newNamespaces = ['baz' => 'baz']; - $expected->removedNamespaces = ['foo' => 'foo']; + $diff = $this->comparator->compareSchemas($fromSchema, $toSchema); - self::assertEquals($expected, $this->comparator->compareSchemas($fromSchema, $toSchema)); + self::assertEquals(['baz'], $diff->getCreatedSchemas()); + self::assertEquals(['foo'], $diff->getDroppedSchemas()); } /** @dataProvider getCompareColumnComments */ @@ -751,11 +741,10 @@ public function testForeignKeyRemovalWithRenamedLocalColumn(): void $schemaDiff = $this->comparator->compareSchemas($fromSchema, $toSchema); - self::assertArrayHasKey('table2', $schemaDiff->changedTables); - - $tableDiff = $schemaDiff->changedTables['table2']; + $alteredTables = $schemaDiff->getAlteredTables(); + self::assertCount(1, $alteredTables); - $addedForeignKeys = $tableDiff->getAddedForeignKeys(); + $addedForeignKeys = $alteredTables[0]->getAddedForeignKeys(); self::assertCount(1, $addedForeignKeys, 'FK to table3 should be added.'); self::assertEquals('table3', $addedForeignKeys[0]->getForeignTableName()); } @@ -792,7 +781,7 @@ public function testWillNotProduceSchemaDiffOnTableWithAddedCustomSchemaDefiniti self::assertEmpty( $this->comparator->compareSchemas($fromSchema, $toSchema) - ->changedTables, + ->getAlteredTables(), 'Schema diff is empty, since only `columnDefinition` changed from `null` (not detected) to a defined one', ); }