From 6381aca16dceaf611b44f92ccac1ee82f750d510 Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Mon, 19 Sep 2022 17:43:23 -0700 Subject: [PATCH] Remove TableDiff::$name and getName() --- UPGRADE.md | 4 ++ src/Platforms/AbstractMySQLPlatform.php | 12 +---- src/Platforms/AbstractPlatform.php | 4 +- src/Platforms/DB2Platform.php | 4 +- src/Platforms/MariaDBPlatform.php | 8 +-- src/Platforms/OraclePlatform.php | 2 +- src/Platforms/PostgreSQLPlatform.php | 2 +- src/Platforms/SQLServerPlatform.php | 2 +- src/Platforms/SQLitePlatform.php | 18 +------ src/Schema/Comparator.php | 2 +- src/Schema/SQLiteSchemaManager.php | 2 +- src/Schema/TableDiff.php | 38 ++----------- .../Platform/AddColumnWithDefaultTest.php | 3 +- .../SchemaManagerFunctionalTestCase.php | 15 ++---- .../AbstractMySQLPlatformTestCase.php | 5 +- tests/Platforms/AbstractPlatformTestCase.php | 54 +++++++++---------- tests/Platforms/DB2PlatformTest.php | 3 +- tests/Platforms/SQLServerPlatformTestCase.php | 30 ++++++----- tests/Platforms/SQLitePlatformTest.php | 32 ++--------- tests/Schema/ComparatorTest.php | 33 ++++-------- tests/Schema/TableDiffTest.php | 47 ---------------- 21 files changed, 91 insertions(+), 229 deletions(-) delete mode 100644 tests/Schema/TableDiffTest.php diff --git a/UPGRADE.md b/UPGRADE.md index 63632c1795a..97df5a10b89 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -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. diff --git a/src/Platforms/AbstractMySQLPlatform.php b/src/Platforms/AbstractMySQLPlatform.php index ee3f840601b..d5d359b17f0 100644 --- a/src/Platforms/AbstractMySQLPlatform.php +++ b/src/Platforms/AbstractMySQLPlatform.php @@ -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); } @@ -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)); @@ -463,10 +463,6 @@ private function getPreAlterTableAlterPrimaryKeySQL(TableDiff $diff, Index $inde $table = $diff->getOldTable(); - if ($table === null) { - return []; - } - $sql = []; $tableNameSQL = $table->getQuotedName($this); @@ -506,10 +502,6 @@ private function getPreAlterTableAlterIndexForeignKeySQL(TableDiff $diff): array { $table = $diff->getOldTable(); - if ($table === null) { - return []; - } - $sql = []; $tableNameSQL = $table->getQuotedName($this); diff --git a/src/Platforms/AbstractPlatform.php b/src/Platforms/AbstractPlatform.php index ee805b53741..c45eddf39c9 100644 --- a/src/Platforms/AbstractPlatform.php +++ b/src/Platforms/AbstractPlatform.php @@ -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 = []; @@ -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); diff --git a/src/Platforms/DB2Platform.php b/src/Platforms/DB2Platform.php index 6261032650a..e79cfd7bfa1 100644 --- a/src/Platforms/DB2Platform.php +++ b/src/Platforms/DB2Platform.php @@ -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) { @@ -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) { diff --git a/src/Platforms/MariaDBPlatform.php b/src/Platforms/MariaDBPlatform.php index bcc697e9b7a..e62dadbeba1 100644 --- a/src/Platforms/MariaDBPlatform.php +++ b/src/Platforms/MariaDBPlatform.php @@ -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)) { @@ -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)) { @@ -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, ); diff --git a/src/Platforms/OraclePlatform.php b/src/Platforms/OraclePlatform.php index cedc3a54d80..9664aed9c95 100644 --- a/src/Platforms/OraclePlatform.php +++ b/src/Platforms/OraclePlatform.php @@ -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)) { diff --git a/src/Platforms/PostgreSQLPlatform.php b/src/Platforms/PostgreSQLPlatform.php index e9708d30844..ac69103f137 100644 --- a/src/Platforms/PostgreSQLPlatform.php +++ b/src/Platforms/PostgreSQLPlatform.php @@ -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); diff --git a/src/Platforms/SQLServerPlatform.php b/src/Platforms/SQLServerPlatform.php index 05ef0528771..e6874c95e5e 100644 --- a/src/Platforms/SQLServerPlatform.php +++ b/src/Platforms/SQLServerPlatform.php @@ -357,7 +357,7 @@ public function getAlterTableSQL(TableDiff $diff): array $columnSql = []; $commentsSql = []; - $table = $diff->getOldTable() ?? $diff->getName($this); + $table = $diff->getOldTable(); $tableName = $table->getName(); diff --git a/src/Platforms/SQLitePlatform.php b/src/Platforms/SQLitePlatform.php index 623e44401ca..e7bc3b1cacb 100644 --- a/src/Platforms/SQLitePlatform.php +++ b/src/Platforms/SQLitePlatform.php @@ -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; @@ -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 = []; @@ -776,7 +762,7 @@ private function getSimpleAlterTableSQL(TableDiff $diff): array|false return false; } - $table = $diff->getOldTable() ?? $diff->getName($this); + $table = $diff->getOldTable(); $sql = []; $tableSql = []; diff --git a/src/Schema/Comparator.php b/src/Schema/Comparator.php index 3b3d3904f67..12895b14cfd 100644 --- a/src/Schema/Comparator.php +++ b/src/Schema/Comparator.php @@ -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(); diff --git a/src/Schema/SQLiteSchemaManager.php b/src/Schema/SQLiteSchemaManager.php index 5379678e8ec..908ad44738b 100644 --- a/src/Schema/SQLiteSchemaManager.php +++ b/src/Schema/SQLiteSchemaManager.php @@ -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 diff --git a/src/Schema/TableDiff.php b/src/Schema/TableDiff.php index 0229a385123..ef2e017c697 100644 --- a/src/Schema/TableDiff.php +++ b/src/Schema/TableDiff.php @@ -4,9 +4,6 @@ namespace Doctrine\DBAL\Schema; -use Doctrine\DBAL\Platforms\AbstractPlatform; -use Doctrine\Deprecations\Deprecation; - /** * Table Diff. */ @@ -60,47 +57,18 @@ class TableDiff * @param array $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; } } diff --git a/tests/Functional/Platform/AddColumnWithDefaultTest.php b/tests/Functional/Platform/AddColumnWithDefaultTest.php index 11292348753..9d313b5f4b5 100644 --- a/tests/Functional/Platform/AddColumnWithDefaultTest.php +++ b/tests/Functional/Platform/AddColumnWithDefaultTest.php @@ -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', diff --git a/tests/Functional/Schema/SchemaManagerFunctionalTestCase.php b/tests/Functional/Schema/SchemaManagerFunctionalTestCase.php index 854162a1115..96286682330 100644 --- a/tests/Functional/Schema/SchemaManagerFunctionalTestCase.php +++ b/tests/Functional/Schema/SchemaManagerFunctionalTestCase.php @@ -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'); @@ -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); @@ -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); @@ -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); @@ -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( diff --git a/tests/Platforms/AbstractMySQLPlatformTestCase.php b/tests/Platforms/AbstractMySQLPlatformTestCase.php index 34095af6e8b..f907385505e 100644 --- a/tests/Platforms/AbstractMySQLPlatformTestCase.php +++ b/tests/Platforms/AbstractMySQLPlatformTestCase.php @@ -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); } diff --git a/tests/Platforms/AbstractPlatformTestCase.php b/tests/Platforms/AbstractPlatformTestCase.php index b1de4244cc9..852c1ae7c93 100644 --- a/tests/Platforms/AbstractPlatformTestCase.php +++ b/tests/Platforms/AbstractPlatformTestCase.php @@ -313,8 +313,7 @@ public function testGetAlterTableSqlDispatchEvent(): void $table->addColumn('changed', 'integer'); $table->addColumn('renamed', 'integer'); - $tableDiff = new TableDiff('mytable'); - $tableDiff->fromTable = $table; + $tableDiff = new TableDiff($table); $tableDiff->addedColumns['added'] = new Column('added', Type::getType('integer'), []); $tableDiff->removedColumns['removed'] = new Column('removed', Type::getType('integer'), []); $tableDiff->changedColumns['changed'] = new ColumnDiff( @@ -561,8 +560,7 @@ public function testAlterTableChangeQuotedColumn(): void $table = new Table('mytable'); $table->addColumn('select', 'integer'); - $tableDiff = new TableDiff('mytable'); - $tableDiff->fromTable = $table; + $tableDiff = new TableDiff($table); $tableDiff->changedColumns['select'] = new ColumnDiff( new Column( 'select', @@ -716,10 +714,11 @@ public function testReturnsJsonTypeDeclarationSQL(): void public function testAlterTableRenameIndex(): void { - $tableDiff = new TableDiff('mytable'); - $tableDiff->fromTable = new Table('mytable'); - $tableDiff->fromTable->addColumn('id', 'integer'); - $tableDiff->fromTable->setPrimaryKey(['id']); + $table = new Table('mytable'); + $table->addColumn('id', 'integer'); + $table->setPrimaryKey(['id']); + + $tableDiff = new TableDiff($table); $tableDiff->renamedIndexes = [ 'idx_foo' => new Index('idx_bar', ['id']), ]; @@ -741,10 +740,11 @@ protected function getAlterTableRenameIndexSQL(): array public function testQuotesAlterTableRenameIndex(): void { - $tableDiff = new TableDiff('table'); - $tableDiff->fromTable = new Table('table'); - $tableDiff->fromTable->addColumn('id', 'integer'); - $tableDiff->fromTable->setPrimaryKey(['id']); + $table = new Table('table'); + $table->addColumn('id', 'integer'); + $table->setPrimaryKey(['id']); + + $tableDiff = new TableDiff($table); $tableDiff->renamedIndexes = [ 'create' => new Index('select', ['id']), '`foo`' => new Index('`bar`', ['id']), @@ -870,10 +870,11 @@ abstract protected function getQuotedAlterTableChangeColumnLengthSQL(): array; public function testAlterTableRenameIndexInSchema(): void { - $tableDiff = new TableDiff('myschema.mytable'); - $tableDiff->fromTable = new Table('myschema.mytable'); - $tableDiff->fromTable->addColumn('id', 'integer'); - $tableDiff->fromTable->setPrimaryKey(['id']); + $table = new Table('myschema.mytable'); + $table->addColumn('id', 'integer'); + $table->setPrimaryKey(['id']); + + $tableDiff = new TableDiff($table); $tableDiff->renamedIndexes = [ 'idx_foo' => new Index('idx_bar', ['id']), ]; @@ -895,10 +896,11 @@ protected function getAlterTableRenameIndexInSchemaSQL(): array public function testQuotesAlterTableRenameIndexInSchema(): void { - $tableDiff = new TableDiff('`schema`.table'); - $tableDiff->fromTable = new Table('`schema`.table'); - $tableDiff->fromTable->addColumn('id', 'integer'); - $tableDiff->fromTable->setPrimaryKey(['id']); + $table = new Table('`schema`.table'); + $table->addColumn('id', 'integer'); + $table->setPrimaryKey(['id']); + + $tableDiff = new TableDiff($table); $tableDiff->renamedIndexes = [ 'create' => new Index('select', ['id']), '`foo`' => new Index('`bar`', ['id']), @@ -1050,8 +1052,7 @@ public function testGeneratesAlterTableRenameColumnSQL(): void ['notnull' => true, 'default' => 666, 'comment' => 'rename test'], ); - $tableDiff = new TableDiff('foo'); - $tableDiff->fromTable = $table; + $tableDiff = new TableDiff($table); $tableDiff->renamedColumns['bar'] = new Column( 'baz', Type::getType('integer'), @@ -1076,8 +1077,7 @@ public function testQuotesTableIdentifiersInAlterTableSQL(): void $table->addForeignKeyConstraint('fk_table', ['fk'], ['id'], [], 'fk1'); $table->addForeignKeyConstraint('fk_table', ['fk2'], ['id'], [], 'fk2'); - $tableDiff = new TableDiff('"foo"'); - $tableDiff->fromTable = $table; + $tableDiff = new TableDiff($table); $tableDiff->addedColumns['bloo'] = new Column('bloo', Type::getType('integer')); $tableDiff->changedColumns['bar'] = new ColumnDiff( new Column('bar', Type::getType('integer'), ['notnull' => false]), @@ -1103,8 +1103,7 @@ public function testAlterStringToFixedString(): void $table = new Table('mytable'); $table->addColumn('name', 'string', ['length' => 2]); - $tableDiff = new TableDiff('mytable'); - $tableDiff->fromTable = $table; + $tableDiff = new TableDiff($table); $tableDiff->changedColumns['name'] = new ColumnDiff( new Column( @@ -1140,8 +1139,7 @@ public function testGeneratesAlterTableRenameIndexUsedByForeignKeySQL(): void $primaryTable->addForeignKeyConstraint($foreignTable->getName(), ['foo'], ['id'], [], 'fk_foo'); $primaryTable->addForeignKeyConstraint($foreignTable->getName(), ['bar'], ['id'], [], 'fk_bar'); - $tableDiff = new TableDiff('mytable'); - $tableDiff->fromTable = $primaryTable; + $tableDiff = new TableDiff($primaryTable); $tableDiff->renamedIndexes['idx_foo'] = new Index('idx_foo_renamed', ['foo']); self::assertSame( diff --git a/tests/Platforms/DB2PlatformTest.php b/tests/Platforms/DB2PlatformTest.php index 650df63e3df..4ce513c4eec 100644 --- a/tests/Platforms/DB2PlatformTest.php +++ b/tests/Platforms/DB2PlatformTest.php @@ -500,8 +500,7 @@ public function testGeneratesAlterColumnSQL( Column $newColumn, ?string $expectedSQLClause, ): void { - $tableDiff = new TableDiff('foo'); - $tableDiff->fromTable = new Table('foo'); + $tableDiff = new TableDiff(new Table('foo')); $tableDiff->changedColumns['bar'] = new ColumnDiff($newColumn, $oldColumn); $expectedSQL = []; diff --git a/tests/Platforms/SQLServerPlatformTestCase.php b/tests/Platforms/SQLServerPlatformTestCase.php index 363354b7928..3bc847a0db3 100644 --- a/tests/Platforms/SQLServerPlatformTestCase.php +++ b/tests/Platforms/SQLServerPlatformTestCase.php @@ -642,7 +642,9 @@ public function testCreateTableWithSchemaColumnComments(): void public function testAlterTableWithSchemaColumnComments(): void { - $tableDiff = new TableDiff('testschema.mytable'); + $table = new Table('testschema.mytable'); + + $tableDiff = new TableDiff($table); $tableDiff->addedColumns['quota'] = new Column('quota', Type::getType('integer'), ['comment' => 'A comment']); $expectedSql = [ @@ -656,7 +658,9 @@ public function testAlterTableWithSchemaColumnComments(): void public function testAlterTableWithSchemaDropColumnComments(): void { - $tableDiff = new TableDiff('testschema.mytable'); + $table = new Table('testschema.mytable'); + + $tableDiff = new TableDiff($table); $tableDiff->changedColumns['quota'] = new ColumnDiff( new Column('quota', Type::getType('integer'), []), new Column('quota', Type::getType('integer'), ['comment' => 'A comment']), @@ -672,7 +676,9 @@ public function testAlterTableWithSchemaDropColumnComments(): void public function testAlterTableWithSchemaUpdateColumnComments(): void { - $tableDiff = new TableDiff('testschema.mytable'); + $table = new Table('testschema.mytable'); + + $tableDiff = new TableDiff($table); $tableDiff->changedColumns['quota'] = new ColumnDiff( new Column('quota', Type::getType('integer'), ['comment' => 'B comment']), new Column('quota', Type::getType('integer'), ['comment' => 'A comment']), @@ -756,8 +762,7 @@ public function testGeneratesAlterTableSQLWithColumnComments(): void ]); $table->setPrimaryKey(['id']); - $tableDiff = new TableDiff('mytable'); - $tableDiff->fromTable = $table; + $tableDiff = new TableDiff($table); $tableDiff->addedColumns['added_comment_none'] = new Column('added_comment_none', Type::getType('integer')); @@ -1026,8 +1031,7 @@ public function testChangeColumnsTypeWithDefaultValue(): void $table->addColumn('col_int', 'smallint', ['default' => 666]); $table->addColumn('col_string', 'string', ['default' => 'foo']); - $tableDiff = new TableDiff($tableName); - $tableDiff->fromTable = $table; + $tableDiff = new TableDiff($table); $tableDiff->changedColumns['col_int'] = new ColumnDiff( new Column('col_int', Type::getType('integer'), ['default' => 666]), new Column('col_int', Type::getType('smallint'), ['default' => 666]), @@ -1186,7 +1190,7 @@ public static function getGeneratesIdentifierNamesInAlterTableSQL(): iterable // Unquoted identifiers non-reserved keywords. [ new TableDiff( - 'mytable', + new Table('mytable'), [ 'addcolumn' => new Column('addcolumn', Type::getType('string'), [ 'length' => 255, @@ -1224,7 +1228,7 @@ public static function getGeneratesIdentifierNamesInAlterTableSQL(): iterable // Quoted identifiers non-reserved keywords. [ new TableDiff( - '`mytable`', + new Table('`mytable`'), [ 'addcolumn' => new Column('`addcolumn`', Type::getType('string'), [ 'length' => 255, @@ -1262,7 +1266,7 @@ public static function getGeneratesIdentifierNamesInAlterTableSQL(): iterable // Unquoted identifiers reserved keywords. [ new TableDiff( - 'table', + new Table('`mytable`'), [ 'add' => new Column('add', Type::getType('string'), [ 'length' => 255, @@ -1300,7 +1304,7 @@ public static function getGeneratesIdentifierNamesInAlterTableSQL(): iterable // Quoted identifiers reserved keywords. [ new TableDiff( - '`table`', + new Table('`table`'), [ 'add' => new Column('`add`', Type::getType('string'), [ 'length' => 255, @@ -1576,7 +1580,9 @@ public function testGeneratesSequenceSqlCommands(): void public function testAlterTableWithSchemaSameColumnComments(): void { - $tableDiff = new TableDiff('testschema.mytable'); + $table = new Table('testschema.mytable'); + + $tableDiff = new TableDiff($table); $tableDiff->changedColumns['quota'] = new ColumnDiff( new Column('quota', Type::getType('integer'), ['comment' => 'A comment', 'notnull' => true]), new Column('quota', Type::getType('integer'), ['comment' => 'A comment', 'notnull' => false]), diff --git a/tests/Platforms/SQLitePlatformTest.php b/tests/Platforms/SQLitePlatformTest.php index 84e83feafd3..08ee8f11b38 100644 --- a/tests/Platforms/SQLitePlatformTest.php +++ b/tests/Platforms/SQLitePlatformTest.php @@ -220,7 +220,8 @@ public function testGenerateTableSqlShouldNotAutoQuotePrimaryKey(): void public function testAlterTableAddColumns(): void { - $diff = new TableDiff('user'); + $table = new Table('user'); + $diff = new TableDiff($table); $diff->addedColumns['foo'] = new Column('foo', Type::getType('string')); $diff->addedColumns['count'] = new Column('count', Type::getType('integer'), [ @@ -236,42 +237,18 @@ public function testAlterTableAddColumns(): void self::assertEquals($expected, $this->platform->getAlterTableSQL($diff)); } - /** @dataProvider complexDiffProvider */ - public function testAlterTableAddComplexColumns(TableDiff $diff): void - { - $this->expectException(Exception::class); - - $this->platform->getAlterTableSQL($diff); - } - public function testRenameNonExistingColumn(): void { $table = new Table('test'); $table->addColumn('id', 'integer'); - $tableDiff = new TableDiff('test'); - $tableDiff->fromTable = $table; + $tableDiff = new TableDiff($table); $tableDiff->renamedColumns['value'] = new Column('data', Type::getType('string')); $this->expectException(Exception::class); $this->platform->getAlterTableSQL($tableDiff); } - /** @return mixed[][] */ - public static function complexDiffProvider(): iterable - { - $date = new TableDiff('user'); - $date->addedColumns['time'] = new Column('time', Type::getType('date'), ['default' => 'CURRENT_DATE']); - - $id = new TableDiff('user'); - $id->addedColumns['id'] = new Column('id', Type::getType('integer'), ['autoincrement' => true]); - - return [ - 'date column with default value' => [$date], - 'id column with auto increment' => [$id], - ]; - } - public function testCreateTableWithDeferredForeignKeys(): void { $table = new Table('user'); @@ -316,8 +293,7 @@ public function testAlterTable(): void $table->addForeignKeyConstraint('user', ['parent'], ['id'], ['deferrable' => true, 'deferred' => true]); $table->addIndex(['article', 'post'], 'index1'); - $diff = new TableDiff('user'); - $diff->fromTable = $table; + $diff = new TableDiff($table); $diff->renamedColumns['id'] = new Column('key', Type::getType('integer'), []); $diff->renamedColumns['post'] = new Column('comment', Type::getType('integer'), []); $diff->removedColumns['parent'] = new Column('comment', Type::getType('integer'), []); diff --git a/tests/Schema/ComparatorTest.php b/tests/Schema/ComparatorTest.php index 80aadd25e9b..4a73d55c5ed 100644 --- a/tests/Schema/ComparatorTest.php +++ b/tests/Schema/ComparatorTest.php @@ -136,7 +136,7 @@ public function testCompareMissingField(): void [], [ 'bugdb' => new TableDiff( - 'bugdb', + $schema1->getTable('bugdb'), [], [], ['integercolumn1' => $missingColumn], @@ -144,8 +144,6 @@ public function testCompareMissingField(): void ], ); - $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb'); - self::assertEquals($expected, $this->comparator->compareSchemas($schema1, $schema2)); } @@ -173,7 +171,7 @@ public function testCompareNewField(): void [], [ 'bugdb' => new TableDiff( - 'bugdb', + $schema1->getTable('bugdb'), [ 'integercolumn2' => new Column('integercolumn2', Type::getType('integer')), ], @@ -181,8 +179,6 @@ public function testCompareNewField(): void ], ); - $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb'); - self::assertEquals($expected, $this->comparator->compareSchemas($schema1, $schema2)); } @@ -281,7 +277,7 @@ public function testCompareRemovedIndex(): void [], [ 'bugdb' => new TableDiff( - 'bugdb', + $schema1->getTable('bugdb'), [], [], [], @@ -298,8 +294,6 @@ public function testCompareRemovedIndex(): void ], ); - $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb'); - self::assertEquals($expected, $this->comparator->compareSchemas($schema1, $schema2)); } @@ -335,7 +329,7 @@ public function testCompareNewIndex(): void [], [ 'bugdb' => new TableDiff( - 'bugdb', + $schema1->getTable('bugdb'), [], [], [], @@ -350,8 +344,6 @@ public function testCompareNewIndex(): void ], ); - $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb'); - self::assertEquals($expected, $this->comparator->compareSchemas($schema1, $schema2)); } @@ -394,7 +386,7 @@ public function testCompareChangedIndex(): void [], [ 'bugdb' => new TableDiff( - 'bugdb', + $schema1->getTable('bugdb'), [], [], [], @@ -413,8 +405,6 @@ public function testCompareChangedIndex(): void ], ); - $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb'); - self::assertEquals($expected, $this->comparator->compareSchemas($schema1, $schema2)); } @@ -449,7 +439,7 @@ public function testCompareChangedIndexFieldPositions(): void [], [ 'bugdb' => new TableDiff( - 'bugdb', + $schema1->getTable('bugdb'), [], [], [], @@ -461,8 +451,6 @@ public function testCompareChangedIndexFieldPositions(): void ], ); - $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb'); - self::assertEquals($expected, $this->comparator->compareSchemas($schema1, $schema2)); } @@ -640,8 +628,7 @@ public function testCompareIndexBasedOnPropertiesNotName(): void $tableB->addColumn('ID', 'integer'); $tableB->addIndex(['id'], 'bar_foo_idx'); - $tableDiff = new TableDiff('foo'); - $tableDiff->fromTable = $tableA; + $tableDiff = new TableDiff($tableA); $tableDiff->renamedIndexes['foo_bar_idx'] = new Index('bar_foo_idx', ['id']); self::assertEquals( @@ -961,8 +948,7 @@ public function testCompareChangedColumn(): void $expected = new SchemaDiff(); - $tableDiff = $expected->changedTables['foo'] = new TableDiff('foo'); - $tableDiff->fromTable = $tableFoo; + $tableDiff = $expected->changedTables['foo'] = new TableDiff($tableFoo); $tableDiff->changedColumns['id'] = new ColumnDiff( $table->getColumn('id'), @@ -985,8 +971,7 @@ public function testCompareChangedBinaryColumn(): void $expected = new SchemaDiff(); - $tableDiff = $expected->changedTables['foo'] = new TableDiff('foo'); - $tableDiff->fromTable = $tableFoo; + $tableDiff = $expected->changedTables['foo'] = new TableDiff($tableFoo); $tableDiff->changedColumns['id'] = new ColumnDiff( $table->getColumn('id'), diff --git a/tests/Schema/TableDiffTest.php b/tests/Schema/TableDiffTest.php deleted file mode 100644 index bcb75adde7d..00000000000 --- a/tests/Schema/TableDiffTest.php +++ /dev/null @@ -1,47 +0,0 @@ -platform = $this->createMock(AbstractPlatform::class); - } - - public function testReturnsName(): void - { - $tableDiff = new TableDiff('foo'); - - self::assertEquals(new Identifier('foo'), $tableDiff->getName($this->platform)); - } - - public function testPrefersNameFromTableObject(): void - { - $tableMock = $this->getMockBuilder(Table::class) - ->disableOriginalConstructor() - ->getMock(); - - $tableDiff = new TableDiff('foo'); - $tableDiff->fromTable = $tableMock; - - $tableMock->expects(self::once()) - ->method('getQuotedName') - ->with($this->platform) - ->willReturn('foo'); - - self::assertEquals(new Identifier('foo'), $tableDiff->getName($this->platform)); - } -}