Skip to content

Commit

Permalink
Merge pull request doctrine#5753 from morozov/table-diff-internal-pro…
Browse files Browse the repository at this point in the history
…perties-private

Declare internal properties of TableDiff as private
  • Loading branch information
morozov committed Oct 12, 2022
2 parents 26c481f + 26ece99 commit d2c74da
Show file tree
Hide file tree
Showing 10 changed files with 363 additions and 644 deletions.
4 changes: 2 additions & 2 deletions src/Platforms/AbstractMySQLPlatform.php
Expand Up @@ -398,14 +398,14 @@ public function getAlterTableSQL(TableDiff $diff): array
$diff->getAddedColumns(),
$diff->getModifiedColumns(),
$diff->getDroppedColumns(),
$diff->getRenamedColumns(),
array_values($addedIndexes),
array_values($modifiedIndexes),
$diff->getDroppedIndexes(),
$diff->getRenamedIndexes(),
$diff->getAddedForeignKeys(),
$diff->getModifiedForeignKeys(),
$diff->getDroppedForeignKeys(),
$diff->getRenamedColumns(),
$diff->getRenamedIndexes(),
);
}

Expand Down
16 changes: 11 additions & 5 deletions src/Platforms/MariaDBPlatform.php
Expand Up @@ -37,8 +37,10 @@ protected function getPreAlterTableRenameIndexForeignKeySQL(TableDiff $diff): ar
$sql = [];
$tableName = $diff->getOldTable()->getQuotedName($this);

$modifiedForeignKeys = $diff->getModifiedForeignKeys();

foreach ($this->getRemainingForeignKeyConstraintsRequiringRenamedIndexes($diff) as $foreignKey) {
if (in_array($foreignKey, $diff->changedForeignKeys, true)) {
if (in_array($foreignKey, $modifiedForeignKeys, true)) {
continue;
}

Expand Down Expand Up @@ -66,8 +68,10 @@ private function getPostAlterTableRenameIndexForeignKeySQL(TableDiff $diff): arr

$tableName = $diff->getOldTable()->getQuotedName($this);

$modifiedForeignKeys = $diff->getModifiedForeignKeys();

foreach ($this->getRemainingForeignKeyConstraintsRequiringRenamedIndexes($diff) as $foreignKey) {
if (in_array($foreignKey, $diff->changedForeignKeys, true)) {
if (in_array($foreignKey, $modifiedForeignKeys, true)) {
continue;
}

Expand All @@ -89,19 +93,21 @@ private function getPostAlterTableRenameIndexForeignKeySQL(TableDiff $diff): arr
*/
private function getRemainingForeignKeyConstraintsRequiringRenamedIndexes(TableDiff $diff): array
{
if (count($diff->renamedIndexes) === 0) {
$renamedIndexes = $diff->getRenamedIndexes();

if (count($renamedIndexes) === 0) {
return [];
}

$foreignKeys = [];

$remainingForeignKeys = array_diff_key(
$diff->getOldTable()->getForeignKeys(),
$diff->removedForeignKeys,
$diff->getDroppedForeignKeys(),
);

foreach ($remainingForeignKeys as $foreignKey) {
foreach ($diff->renamedIndexes as $index) {
foreach ($renamedIndexes as $index) {
if ($foreignKey->intersectsIndexColumns($index)) {
$foreignKeys[] = $foreignKey;

Expand Down
11 changes: 4 additions & 7 deletions src/Schema/Comparator.php
Expand Up @@ -221,10 +221,7 @@ public function diffTable(Table $fromTable, Table $toTable): ?TableDiff
continue;
}

$modifiedColumns[$column->getName()] = new ColumnDiff(
$column,
$toColumn,
);
$modifiedColumns[] = new ColumnDiff($column, $toColumn);

$hasChanges = true;
}
Expand Down Expand Up @@ -266,7 +263,7 @@ public function diffTable(Table $fromTable, Table $toTable): ?TableDiff
continue;
}

$modifiedIndexes[$indexName] = $toTableIndex;
$modifiedIndexes[] = $toTableIndex;

$hasChanges = true;
}
Expand Down Expand Up @@ -312,14 +309,14 @@ public function diffTable(Table $fromTable, Table $toTable): ?TableDiff
$addedColumns,
$modifiedColumns,
$droppedColumns,
$renamedColumns,
$addedIndexes,
$modifiedIndexes,
$droppedIndexes,
$renamedIndexes,
$addedForeignKeys,
$modifiedForeignKeys,
$droppedForeignKeys,
$renamedColumns,
$renamedIndexes,
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Schema/SQLiteSchemaManager.php
Expand Up @@ -60,7 +60,7 @@ public function createForeignKey(ForeignKeyConstraint $foreignKey, string $table
{
$table = $this->introspectTable($table);

$this->alterTable(new TableDiff($table, [], [], [], [], [], [], [$foreignKey]));
$this->alterTable(new TableDiff($table, [], [], [], [], [], [], [], [], [$foreignKey], [], []));
}

public function dropForeignKey(string $name, string $table): void
Expand All @@ -69,7 +69,7 @@ public function dropForeignKey(string $name, string $table): void

$foreignKey = $table->getForeignKey($name);

$this->alterTable(new TableDiff($table, [], [], [], [], [], [], [], [], [$foreignKey]));
$this->alterTable(new TableDiff($table, [], [], [], [], [], [], [], [], [], [], [$foreignKey]));
}

/**
Expand Down
110 changes: 48 additions & 62 deletions src/Schema/TableDiff.php
Expand Up @@ -5,7 +5,6 @@
namespace Doctrine\DBAL\Schema;

use function array_filter;
use function array_values;

/**
* Table Diff.
Expand All @@ -17,42 +16,31 @@ class TableDiff
*
* @internal The diff can be only instantiated by a {@see Comparator}.
*
* @param array<Column> $addedColumns
* @param array<ColumnDiff> $changedColumns
* @param array<Column> $removedColumns
* @param array<Index> $addedIndexes
* @param array<Index> $changedIndexes
* @param array<Index> $removedIndexes
* @param list<ForeignKeyConstraint> $addedForeignKeys
* @param list<ForeignKeyConstraint> $changedForeignKeys
* @param list<ForeignKeyConstraint> $removedForeignKeys
* @param array<string, Column> $renamedColumns
* @param array<string, Index> $renamedIndexes
* @param array<ForeignKeyConstraint> $droppedForeignKeys
* @param array<Column> $addedColumns
* @param array<ColumnDiff> $modifiedColumns
* @param array<Column> $droppedColumns
* @param array<string, Column> $renamedColumns
* @param array<Index> $addedIndexes
* @param array<Index> $modifiedIndexes
* @param array<Index> $droppedIndexes
* @param array<string, Index> $renamedIndexes
* @param array<ForeignKeyConstraint> $addedForeignKeys
* @param array<ForeignKeyConstraint> $modifiedForeignKeys
*/
public function __construct(
private Table $oldTable,
/** @internal Use {@see getAddedColumns()} instead. */
public array $addedColumns = [],
/** @internal Use {@see getModifiedColumns()} instead. */
public array $changedColumns = [],
/** @internal Use {@see getDroppedColumns()} instead. */
public array $removedColumns = [],
/** @internal Use {@see getAddedIndexes()} instead. */
public array $addedIndexes = [],
/** @internal Use {@see getModifiedIndexes()} instead. */
public array $changedIndexes = [],
/** @internal Use {@see getDroppedIndexes()} instead. */
public array $removedIndexes = [],
/** @internal Use {@see getAddedForeignKeys()} instead. */
public array $addedForeignKeys = [],
/** @internal Use {@see getModifiedForeignKeys()} instead. */
public array $changedForeignKeys = [],
/** @internal Use {@see getDroppedForeignKeys()} instead. */
public array $removedForeignKeys = [],
/** @internal Use {@see getRenamedColumns()} instead. */
public array $renamedColumns = [],
/** @internal Use {@see getRenamedIndexes()} instead. */
public array $renamedIndexes = [],
private readonly Table $oldTable,
private readonly array $addedColumns,
private readonly array $modifiedColumns,
private readonly array $droppedColumns,
private readonly array $renamedColumns,
private array $addedIndexes,
private readonly array $modifiedIndexes,
private array $droppedIndexes,
private readonly array $renamedIndexes,
private readonly array $addedForeignKeys,
private readonly array $modifiedForeignKeys,
private array $droppedForeignKeys,
) {
}

Expand All @@ -61,22 +49,22 @@ public function getOldTable(): Table
return $this->oldTable;
}

/** @return list<Column> */
/** @return array<Column> */
public function getAddedColumns(): array
{
return array_values($this->addedColumns);
return $this->addedColumns;
}

/** @return list<ColumnDiff> */
/** @return array<ColumnDiff> */
public function getModifiedColumns(): array
{
return array_values($this->changedColumns);
return $this->modifiedColumns;
}

/** @return list<Column> */
/** @return array<Column> */
public function getDroppedColumns(): array
{
return array_values($this->removedColumns);
return $this->droppedColumns;
}

/** @return array<string,Column> */
Expand All @@ -85,10 +73,10 @@ public function getRenamedColumns(): array
return $this->renamedColumns;
}

/** @return list<Index> */
/** @return array<Index> */
public function getAddedIndexes(): array
{
return array_values($this->addedIndexes);
return $this->addedIndexes;
}

/**
Expand All @@ -108,13 +96,13 @@ static function (Index $addedIndex) use ($index): bool {
/** @return array<Index> */
public function getModifiedIndexes(): array
{
return array_values($this->changedIndexes);
return $this->modifiedIndexes;
}

/** @return list<Index> */
/** @return array<Index> */
public function getDroppedIndexes(): array
{
return array_values($this->removedIndexes);
return $this->droppedIndexes;
}

/**
Expand All @@ -123,10 +111,10 @@ public function getDroppedIndexes(): array
*/
public function unsetDroppedIndex(Index $index): void
{
$this->removedIndexes = array_filter(
$this->removedIndexes,
static function (Index $removedIndex) use ($index): bool {
return $removedIndex !== $index;
$this->droppedIndexes = array_filter(
$this->droppedIndexes,
static function (Index $droppedIndex) use ($index): bool {
return $droppedIndex !== $index;
},
);
}
Expand All @@ -137,34 +125,32 @@ public function getRenamedIndexes(): array
return $this->renamedIndexes;
}

/** @return list<ForeignKeyConstraint> */
/** @return array<ForeignKeyConstraint> */
public function getAddedForeignKeys(): array
{
return $this->addedForeignKeys;
}

/** @return list<ForeignKeyConstraint> */
/** @return array<ForeignKeyConstraint> */
public function getModifiedForeignKeys(): array
{
return $this->changedForeignKeys;
return $this->modifiedForeignKeys;
}

/** @return list<ForeignKeyConstraint> */
/** @return array<ForeignKeyConstraint> */
public function getDroppedForeignKeys(): array
{
return $this->removedForeignKeys;
return $this->droppedForeignKeys;
}

/** @internal This method exists only for compatibility with the current implementation of the schema comparator. */
public function unsetDroppedForeignKey(ForeignKeyConstraint $foreignKey): void
{
$this->removedForeignKeys = array_values(
array_filter(
$this->removedForeignKeys,
static function (ForeignKeyConstraint $removedForeignKey) use ($foreignKey): bool {
return $removedForeignKey !== $foreignKey;
},
),
$this->droppedForeignKeys = array_filter(
$this->droppedForeignKeys,
static function (ForeignKeyConstraint $droppedForeignKey) use ($foreignKey): bool {
return $droppedForeignKey !== $foreignKey;
},
);
}
}

0 comments on commit d2c74da

Please sign in to comment.