Skip to content

Commit

Permalink
Remove TableDiff::$name and getName()
Browse files Browse the repository at this point in the history
  • Loading branch information
morozov committed Sep 20, 2022
1 parent b24280d commit 39e4ff8
Show file tree
Hide file tree
Showing 21 changed files with 91 additions and 229 deletions.
4 changes: 4 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,10 @@ the name of the object as a separate parameter. The name of the passed index or
The `AbstractPlatform::canEmulateSchemas()` method and the schema emulation implemented in the SQLite platform
have been removed.

## BC BREAK: removed `TableDiff::$name` name `TableDiff::getName()`.

The `TableDiff::$name` property and `TableDiff::getName()` method have been removed.

## BC BREAK: removed support for renaming tables via `TableDiff` and `AbstractPlatform::alterTable()`.

The `TableDiff::$newName` property and the `TableDiff::getNewName()` method have been removed.
Expand Down
12 changes: 2 additions & 10 deletions src/Platforms/AbstractMySQLPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ public function getAlterTableSQL(TableDiff $diff): array

if (! $this->onSchemaAlterTable($diff, $tableSql)) {
if (count($queryParts) > 0) {
$sql[] = 'ALTER TABLE ' . ($diff->getOldTable() ?? $diff->getName($this))->getQuotedName($this) . ' '
$sql[] = 'ALTER TABLE ' . $diff->getOldTable()->getQuotedName($this) . ' '
. implode(', ', $queryParts);
}

Expand All @@ -408,7 +408,7 @@ protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff): array
{
$sql = [];

$tableNameSQL = ($diff->getOldTable() ?? $diff->getName($this))->getQuotedName($this);
$tableNameSQL = $diff->getOldTable()->getQuotedName($this);

foreach ($diff->changedIndexes as $changedIndex) {
$sql = array_merge($sql, $this->getPreAlterTableAlterPrimaryKeySQL($diff, $changedIndex));
Expand Down Expand Up @@ -463,10 +463,6 @@ private function getPreAlterTableAlterPrimaryKeySQL(TableDiff $diff, Index $inde

$table = $diff->getOldTable();

if ($table === null) {
return [];
}

$sql = [];

$tableNameSQL = $table->getQuotedName($this);
Expand Down Expand Up @@ -506,10 +502,6 @@ private function getPreAlterTableAlterIndexForeignKeySQL(TableDiff $diff): array
{
$table = $diff->getOldTable();

if ($table === null) {
return [];
}

$sql = [];

$tableNameSQL = $table->getQuotedName($this);
Expand Down
4 changes: 2 additions & 2 deletions src/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -1393,7 +1393,7 @@ protected function onSchemaAlterTable(TableDiff $diff, array &$sql): bool
/** @return string[] */
protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff): array
{
$tableNameSQL = ($diff->getOldTable() ?? $diff->getName($this))->getQuotedName($this);
$tableNameSQL = $diff->getOldTable()->getQuotedName($this);

$sql = [];

Expand Down Expand Up @@ -1421,7 +1421,7 @@ protected function getPostAlterTableIndexForeignKeySQL(TableDiff $diff): array
{
$sql = [];

$tableNameSQL = ($diff->getOldTable() ?? $diff->getName($this))->getQuotedName($this);
$tableNameSQL = $diff->getOldTable()->getQuotedName($this);

foreach ($diff->addedForeignKeys as $foreignKey) {
$sql[] = $this->getCreateForeignKeySQL($foreignKey, $tableNameSQL);
Expand Down
4 changes: 2 additions & 2 deletions src/Platforms/DB2Platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public function getAlterTableSQL(TableDiff $diff): array
$columnSql = [];
$commentsSQL = [];

$tableNameSQL = ($diff->getOldTable() ?? $diff->getName($this))->getQuotedName($this);
$tableNameSQL = $diff->getOldTable()->getQuotedName($this);

$queryParts = [];
foreach ($diff->addedColumns as $column) {
Expand Down Expand Up @@ -457,7 +457,7 @@ protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff): array
{
$sql = [];

$tableNameSQL = ($diff->getOldTable() ?? $diff->getName($this))->getQuotedName($this);
$tableNameSQL = $diff->getOldTable()->getQuotedName($this);

foreach ($diff->removedIndexes as $remKey => $remIndex) {
foreach ($diff->addedIndexes as $addKey => $addIndex) {
Expand Down
8 changes: 4 additions & 4 deletions src/Platforms/MariaDBPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function getJsonTypeDeclarationSQL(array $column): string
protected function getPreAlterTableRenameIndexForeignKeySQL(TableDiff $diff): array
{
$sql = [];
$tableName = $diff->getName($this)->getQuotedName($this);
$tableName = $diff->getOldTable()->getQuotedName($this);

foreach ($this->getRemainingForeignKeyConstraintsRequiringRenamedIndexes($diff) as $foreignKey) {
if (in_array($foreignKey, $diff->changedForeignKeys, true)) {
Expand Down Expand Up @@ -64,7 +64,7 @@ private function getPostAlterTableRenameIndexForeignKeySQL(TableDiff $diff): arr
{
$sql = [];

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

foreach ($this->getRemainingForeignKeyConstraintsRequiringRenamedIndexes($diff) as $foreignKey) {
if (in_array($foreignKey, $diff->changedForeignKeys, true)) {
Expand All @@ -89,14 +89,14 @@ private function getPostAlterTableRenameIndexForeignKeySQL(TableDiff $diff): arr
*/
private function getRemainingForeignKeyConstraintsRequiringRenamedIndexes(TableDiff $diff): array
{
if ($diff->fromTable === null || count($diff->renamedIndexes) === 0) {
if (count($diff->renamedIndexes) === 0) {
return [];
}

$foreignKeys = [];

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

Expand Down
2 changes: 1 addition & 1 deletion src/Platforms/OraclePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ public function getAlterTableSQL(TableDiff $diff): array

$addColumnSQL = [];

$tableNameSQL = ($diff->getOldTable() ?? $diff->getName($this))->getQuotedName($this);
$tableNameSQL = $diff->getOldTable()->getQuotedName($this);

foreach ($diff->addedColumns as $column) {
if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Platforms/PostgreSQLPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public function getAlterTableSQL(TableDiff $diff): array
$commentsSQL = [];
$columnSql = [];

$table = $diff->getOldTable() ?? $diff->getName($this);
$table = $diff->getOldTable();

$tableNameSQL = $table->getQuotedName($this);

Expand Down
2 changes: 1 addition & 1 deletion src/Platforms/SQLServerPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ public function getAlterTableSQL(TableDiff $diff): array
$columnSql = [];
$commentsSql = [];

$table = $diff->getOldTable() ?? $diff->getName($this);
$table = $diff->getOldTable();

$tableName = $table->getName();

Expand Down
18 changes: 2 additions & 16 deletions src/Platforms/SQLitePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,22 +478,14 @@ protected function getPostAlterTableIndexForeignKeySQL(TableDiff $diff): array
{
$table = $diff->getOldTable();

if ($table === null) {
throw new Exception(
'Sqlite platform requires for alter table the table diff with reference to original table schema.',
);
}

$sql = [];

$tableName = $diff->getName($this);

foreach ($this->getIndexesInAlteredTable($diff, $table) as $index) {
if ($index->isPrimary()) {
continue;
}

$sql[] = $this->getCreateIndexSQL($index, $tableName->getQuotedName($this));
$sql[] = $this->getCreateIndexSQL($index, $table->getQuotedName($this));
}

return $sql;
Expand Down Expand Up @@ -576,12 +568,6 @@ public function getAlterTableSQL(TableDiff $diff): array

$table = $diff->getOldTable();

if ($table === null) {
throw new Exception(
'Sqlite platform requires for alter table the table diff with reference to original table schema.',
);
}

$columns = [];
$oldColumnNames = [];
$newColumnNames = [];
Expand Down Expand Up @@ -776,7 +762,7 @@ private function getSimpleAlterTableSQL(TableDiff $diff): array|false
return false;
}

$table = $diff->getOldTable() ?? $diff->getName($this);
$table = $diff->getOldTable();

$sql = [];
$tableSql = [];
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/Comparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public function diffSequence(Sequence $sequence1, Sequence $sequence2): bool
public function diffTable(Table $fromTable, Table $toTable): ?TableDiff
{
$changes = 0;
$tableDifferences = new TableDiff($fromTable->getName(), [], [], [], [], [], [], $fromTable);
$tableDifferences = new TableDiff($fromTable, [], [], [], [], [], []);

$fromTableColumns = $fromTable->getColumns();
$toTableColumns = $toTable->getColumns();
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/SQLiteSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ protected function _getPortableTableForeignKeyDefinition(array $tableForeignKey)

private function getTableDiffForAlterForeignKey(Table $table): TableDiff
{
return new TableDiff($table->getName(), [], [], [], [], [], [], $table);
return new TableDiff($table, [], [], [], [], [], []);
}

private function parseColumnCollationFromSQL(string $column, string $sql): ?string
Expand Down
38 changes: 3 additions & 35 deletions src/Schema/TableDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

namespace Doctrine\DBAL\Schema;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\Deprecations\Deprecation;

/**
* Table Diff.
*/
Expand Down Expand Up @@ -60,47 +57,18 @@ class TableDiff
* @param array<string, Index> $removedIndexes
*/
public function __construct(
/**
* @deprecated Use {@see getOldTable()} instead.
*/
public string $name,
private Table $oldTable,
public array $addedColumns = [],
public array $changedColumns = [],
public array $removedColumns = [],
public array $addedIndexes = [],
public array $changedIndexes = [],
public array $removedIndexes = [],
/**
* @internal Use {@see getOldTable()} instead.
*/
public ?Table $fromTable = null,
) {
if ($fromTable === null) {
return;
}

Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/5678',
'Not passing the $fromColumn to %s is deprecated.',
__METHOD__,
);
}

/**
* @deprecated Use {@see getOldTable()} instead.
*
* @param AbstractPlatform $platform The platform to use for retrieving this table diff's name.
*/
public function getName(AbstractPlatform $platform): Identifier
{
return new Identifier(
$this->fromTable instanceof Table ? $this->fromTable->getQuotedName($platform) : $this->name,
);
}

public function getOldTable(): ?Table
public function getOldTable(): Table
{
return $this->fromTable;
return $this->oldTable;
}
}
3 changes: 1 addition & 2 deletions tests/Functional/Platform/AddColumnWithDefaultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ public function testAddColumnWithDefault(): void

$this->connection->executeStatement("INSERT INTO add_default_test (original_field) VALUES ('one')");

$tableDiff = new TableDiff('add_default_test');
$tableDiff->fromTable = $table;
$tableDiff = new TableDiff($table);
$tableDiff->addedColumns['foo'] = new Column('new_field', Type::getType('string'), [
'length' => 8,
'default' => 'DEFAULT',
Expand Down
15 changes: 5 additions & 10 deletions tests/Functional/Schema/SchemaManagerFunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,7 @@ public function testAlterTableScenario(): void
self::assertCount(0, $table->getForeignKeys());
self::assertCount(1, $table->getIndexes());

$tableDiff = new TableDiff('alter_table');
$tableDiff->fromTable = $alterTable;
$tableDiff = new TableDiff($alterTable);
$tableDiff->addedColumns['foo'] = new Column('foo', Type::getType('integer'));
$tableDiff->removedColumns['test'] = $table->getColumn('test');

Expand All @@ -601,8 +600,7 @@ public function testAlterTableScenario(): void
self::assertFalse($table->hasColumn('test'));
self::assertTrue($table->hasColumn('foo'));

$tableDiff = new TableDiff('alter_table');
$tableDiff->fromTable = $table;
$tableDiff = new TableDiff($table);
$tableDiff->addedIndexes['foo_idx'] = new Index('foo_idx', ['foo']);

$this->schemaManager->alterTable($tableDiff);
Expand All @@ -614,8 +612,7 @@ public function testAlterTableScenario(): void
self::assertFalse($table->getIndex('foo_idx')->isPrimary());
self::assertFalse($table->getIndex('foo_idx')->isUnique());

$tableDiff = new TableDiff('alter_table');
$tableDiff->fromTable = $table;
$tableDiff = new TableDiff($table);
$tableDiff->changedIndexes['foo_idx'] = new Index('foo_idx', ['foo', 'foreign_key_test']);

$this->schemaManager->alterTable($tableDiff);
Expand All @@ -628,8 +625,7 @@ public function testAlterTableScenario(): void
array_map('strtolower', $table->getIndex('foo_idx')->getColumns()),
);

$tableDiff = new TableDiff('alter_table');
$tableDiff->fromTable = $table;
$tableDiff = new TableDiff($table);
$tableDiff->renamedIndexes['foo_idx'] = new Index('bar_idx', ['foo', 'foreign_key_test']);

$this->schemaManager->alterTable($tableDiff);
Expand All @@ -645,8 +641,7 @@ public function testAlterTableScenario(): void
self::assertFalse($table->getIndex('bar_idx')->isPrimary());
self::assertFalse($table->getIndex('bar_idx')->isUnique());

$tableDiff = new TableDiff('alter_table');
$tableDiff->fromTable = $table;
$tableDiff = new TableDiff($table);
$tableDiff->removedIndexes['bar_idx'] = new Index('bar_idx', ['foo', 'foreign_key_test']);

$tableDiff->addedForeignKeys[] = new ForeignKeyConstraint(
Expand Down
5 changes: 3 additions & 2 deletions tests/Platforms/AbstractMySQLPlatformTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,13 @@ public function testChangeIndexWithForeignKeys(): void
{
$index = new Index('idx', ['col'], false);
$unique = new Index('uniq', ['col'], true);
$table = new Table('test');

$diff = new TableDiff('test', [], [], [], ['uniq' => $unique], [], ['idx' => $index]);
$diff = new TableDiff($table, [], [], [], ['uniq' => $unique], [], ['idx' => $index]);
$sql = $this->platform->getAlterTableSQL($diff);
self::assertEquals(['ALTER TABLE test DROP INDEX idx, ADD UNIQUE INDEX uniq (col)'], $sql);

$diff = new TableDiff('test', [], [], [], ['idx' => $index], [], ['unique' => $unique]);
$diff = new TableDiff($table, [], [], [], ['idx' => $index], [], ['unique' => $unique]);
$sql = $this->platform->getAlterTableSQL($diff);
self::assertEquals(['ALTER TABLE test DROP INDEX uniq, ADD INDEX idx (col)'], $sql);
}
Expand Down
Loading

0 comments on commit 39e4ff8

Please sign in to comment.