From 630e699a61229111eae03dca9b7ca667b2283260 Mon Sep 17 00:00:00 2001 From: WhizSid Date: Sat, 8 Jan 2022 15:38:53 +0530 Subject: [PATCH 1/2] Adding methods to drop foreign,unique,primary,basic indexes --- src/Schema/Table.php | 96 ++++++++++++++++++++++++++-- tests/Schema/SchemaTableTest.php | 106 +++++++++++++++++++++++++++++++ 2 files changed, 198 insertions(+), 4 deletions(-) diff --git a/src/Schema/Table.php b/src/Schema/Table.php index da9e42b..bd5a490 100644 --- a/src/Schema/Table.php +++ b/src/Schema/Table.php @@ -6,6 +6,7 @@ use Closure; use Doctrine\DBAL\Schema\Column; +use Doctrine\DBAL\Schema\Index; use Doctrine\DBAL\Schema\Table as Blueprint; use Doctrine\DBAL\Types\Type; @@ -56,6 +57,49 @@ public function primary(string $columns, $name = null): ?Blueprint return $this->table->setPrimaryKey($columns, $name); } + /** + * Dropping an index which satisfy the criteria + * + * @param string|array $name + * @param Closure $satisfy + */ + protected function _dropIndex($name, $satisfy){ + if (is_string($name)) { + $this->table->dropIndex($name); + } else { + $indexes = $this->table->getIndexes(); + + $matched = []; + foreach ($indexes as $key => $index) { + if ($satisfy($index)) { + $columns = $index->getColumns(); + + if (count(array_diff($columns, $name)) == 0 && count(array_diff($name, $columns)) == 0) { + array_push($matched, $key); + } + } + } + + foreach ($matched as $indexName) { + $this->table->dropIndex($indexName); + } + } + } + + /** + * Dropping a defined primary index from the table + * + * @param string|array $name Name of the primary index or column names associated with the index + * + * @return void + */ + public function dropPrimary($name) + { + $this->_dropIndex($name, function(Index $index){ + return $index->isPrimary(); + }); + } + /** * Specify a unique index for the table. * @@ -72,6 +116,20 @@ public function unique($columns, $name = null, $options = []): ?Blueprint return $this->table->addUniqueIndex($columns, $name, $options); } + /** + * Dropping a defined unique index from the table + * + * @param string|array $name Name of the unique index or column names associated with the index + * + * @return void + */ + public function dropUnique($name) + { + $this->_dropIndex($name, function(Index $index){ + return $index->isUnique(); + }); + } + /** * Specify an index for the table. * @@ -89,6 +147,20 @@ public function index($columns, $name = null, $flags = [], $options = []): ?Blue return $this->table->addIndex($columns, $name, $flags, $options); } + /** + * Dropping a basic index from the table + * + * @param string|array $name Name of the primary index or column names associated with the index + * + * @return void + */ + public function dropIndex($name) + { + $this->_dropIndex($name, function(Index $index){ + return true; + }); + } + /** * Specify a foreign key for the table. * @@ -106,13 +178,29 @@ public function foreign( $foreignColumnNames = 'id', $options = [], $constraintName = null - ): ?Blueprint - { + ): ?Blueprint { $localColumnNames = is_array($localColumnNames) ? $localColumnNames : [$localColumnNames]; $foreignColumnNames = is_array($foreignColumnNames) ? $foreignColumnNames : [$foreignColumnNames]; - return $this->table->addForeignKeyConstraint($table, $localColumnNames, $foreignColumnNames, $options, - $constraintName); + return $this->table->addForeignKeyConstraint( + $table, + $localColumnNames, + $foreignColumnNames, + $options, + $constraintName + ); + } + + /** + * Dropping a foreign key from the table + * + * @param string|array $name Name of the foreign index or column names associated with the index + */ + public function dropForeign($name) + { + $this->_dropIndex($name, function(Index $index){ + return !$index->isUnique()&&!$index->isPrimary(); + }); } /** diff --git a/tests/Schema/SchemaTableTest.php b/tests/Schema/SchemaTableTest.php index 7f01fbf..77c1ba2 100644 --- a/tests/Schema/SchemaTableTest.php +++ b/tests/Schema/SchemaTableTest.php @@ -3,6 +3,8 @@ declare(strict_types=1); use Doctrine\DBAL\Schema\Column; +use Doctrine\DBAL\Schema\Index; +use Doctrine\DBAL\Types\Type; use LaravelDoctrine\Migrations\Schema\Table; use Mockery as m; use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; @@ -311,4 +313,108 @@ public function test_drop_column() $this->table->dropColumn('column'); } + + public function test_drop_unique() + { + $this->dbal->shouldReceive('dropIndex')->with('unique'); + + $this->table->dropForeign('unique'); + } + + public function test_drop_unique_with_exact_column_names() + { + $this->dbal->shouldReceive('getIndexes')->andReturn([ + 'index1'=> new Index('index1', ['column1', 'column2', 'column3']), + 'index2'=> new Index('index2', ['column1', 'column2']), + 'index3'=> new Index('index3', ['column1']), + 'index4'=> new Index('index4', ['column1', 'column2', 'column3'], true), + 'index5'=> new Index('index5', ['column1', 'column2'], true), + 'index6'=> new Index('index6', ['column1'], true), + 'index7'=> new Index('index7', ['column1', 'column2', 'column3'], true), + 'index8'=> new Index('index8', ['column1', 'column2'], true), + 'index9'=> new Index('index9', ['column1'], true), + ]); + + $this->dbal->shouldReceive('dropIndex')->with('index5'); + $this->dbal->shouldReceive('dropIndex')->with('index8'); + + $this->table->dropUnique(['column1', 'column2']); + } + + public function test_drop_primary() + { + $this->dbal->shouldReceive('dropIndex')->with('primary'); + + $this->table->dropPrimary('primary'); + } + + public function test_drop_primary_with_exact_column_names() + { + $this->dbal->shouldReceive('getIndexes')->andReturn([ + 'index1'=> new Index('index1', ['column1', 'column2', 'column3']), + 'index2'=> new Index('index2', ['column1', 'column2']), + 'index3'=> new Index('index3', ['column1']), + 'index4'=> new Index('index4', ['column1', 'column2', 'column3'], false, true), + 'index5'=> new Index('index5', ['column1', 'column2'], false, true), + 'index6'=> new Index('index6', ['column1'], false, true), + ]); + + $this->dbal->shouldReceive('dropIndex')->with('index5'); + + $this->table->dropPrimary(['column1', 'column2']); + } + + public function test_drop_foreign() + { + $this->dbal->shouldReceive('dropIndex')->with('foreign'); + + $this->table->dropForeign('foreign'); + } + + public function test_drop_foreign_with_exact_column_names() + { + $this->dbal->shouldReceive('getIndexes')->andReturn([ + 'index1'=> new Index('index1', ['column1', 'column2', 'column3'], true), + 'index2'=> new Index('index2', ['column1', 'column2'], true), + 'index3'=> new Index('index3', ['column1'], true), + 'index4'=> new Index('index4', ['column1', 'column2', 'column3'], false, true), + 'index5'=> new Index('index5', ['column1', 'column2'], false, true), + 'index6'=> new Index('index6', ['column1'], false, true), + 'index7'=> new Index('index7', ['column1', 'column2', 'column3']), + 'index8'=> new Index('index8', ['column1', 'column2']), + 'index9'=> new Index('index9', ['column1']), + ]); + + $this->dbal->shouldReceive('dropIndex')->with('index8'); + + $this->table->dropForeign(['column1', 'column2']); + } + + public function test_drop_index() + { + $this->dbal->shouldReceive('dropIndex')->with('index'); + + $this->table->dropIndex('index'); + } + + public function test_drop_index_with_exact_column_names() + { + $this->dbal->shouldReceive('getIndexes')->andReturn([ + 'index1'=> new Index('index1', ['column1', 'column2', 'column3'], true), + 'index2'=> new Index('index2', ['column1', 'column2'], true), + 'index3'=> new Index('index3', ['column1'], true), + 'index4'=> new Index('index4', ['column1', 'column2', 'column3'], false, true), + 'index5'=> new Index('index5', ['column1', 'column2'], false, true), + 'index6'=> new Index('index6', ['column1'], false, true), + 'index7'=> new Index('index7', ['column1', 'column2', 'column3']), + 'index8'=> new Index('index8', ['column1', 'column2']), + 'index9'=> new Index('index9', ['column1']), + ]); + + $this->dbal->shouldReceive('dropIndex')->with('index2'); + $this->dbal->shouldReceive('dropIndex')->with('index5'); + $this->dbal->shouldReceive('dropIndex')->with('index8'); + + $this->table->dropIndex(['column1', 'column2']); + } } From ebed9855a0aea5d64a447052d8c24394a5486ede Mon Sep 17 00:00:00 2001 From: WhizSid Date: Tue, 11 Jan 2022 23:57:48 +0530 Subject: [PATCH 2/2] Removed satisfy --- src/Schema/Table.php | 64 +++++++++++--------------------- tests/Schema/SchemaTableTest.php | 64 ++++++++++++++------------------ 2 files changed, 50 insertions(+), 78 deletions(-) diff --git a/src/Schema/Table.php b/src/Schema/Table.php index bd5a490..88cf2f2 100644 --- a/src/Schema/Table.php +++ b/src/Schema/Table.php @@ -57,35 +57,6 @@ public function primary(string $columns, $name = null): ?Blueprint return $this->table->setPrimaryKey($columns, $name); } - /** - * Dropping an index which satisfy the criteria - * - * @param string|array $name - * @param Closure $satisfy - */ - protected function _dropIndex($name, $satisfy){ - if (is_string($name)) { - $this->table->dropIndex($name); - } else { - $indexes = $this->table->getIndexes(); - - $matched = []; - foreach ($indexes as $key => $index) { - if ($satisfy($index)) { - $columns = $index->getColumns(); - - if (count(array_diff($columns, $name)) == 0 && count(array_diff($name, $columns)) == 0) { - array_push($matched, $key); - } - } - } - - foreach ($matched as $indexName) { - $this->table->dropIndex($indexName); - } - } - } - /** * Dropping a defined primary index from the table * @@ -95,9 +66,7 @@ protected function _dropIndex($name, $satisfy){ */ public function dropPrimary($name) { - $this->_dropIndex($name, function(Index $index){ - return $index->isPrimary(); - }); + $this->dropIndex($name); } /** @@ -125,9 +94,7 @@ public function unique($columns, $name = null, $options = []): ?Blueprint */ public function dropUnique($name) { - $this->_dropIndex($name, function(Index $index){ - return $index->isUnique(); - }); + $this->dropIndex($name); } /** @@ -150,15 +117,30 @@ public function index($columns, $name = null, $flags = [], $options = []): ?Blue /** * Dropping a basic index from the table * - * @param string|array $name Name of the primary index or column names associated with the index + * @param string|array $name Name of the index or column names associated with the index * * @return void */ public function dropIndex($name) { - $this->_dropIndex($name, function(Index $index){ - return true; - }); + if (is_string($name)) { + $this->table->dropIndex($name); + } else { + $indexes = $this->table->getIndexes(); + + $matched = []; + foreach ($indexes as $key => $index) { + $columns = $index->getColumns(); + + if (count(array_diff($columns, $name)) == 0 && count(array_diff($name, $columns)) == 0) { + array_push($matched, $key); + } + } + + foreach ($matched as $indexName) { + $this->table->dropIndex($indexName); + } + } } /** @@ -198,9 +180,7 @@ public function foreign( */ public function dropForeign($name) { - $this->_dropIndex($name, function(Index $index){ - return !$index->isUnique()&&!$index->isPrimary(); - }); + $this->dropIndex($name); } /** diff --git a/tests/Schema/SchemaTableTest.php b/tests/Schema/SchemaTableTest.php index 77c1ba2..93e6c1c 100644 --- a/tests/Schema/SchemaTableTest.php +++ b/tests/Schema/SchemaTableTest.php @@ -297,7 +297,7 @@ public function test_remember_token() { $column = m::mock(Column::class); $this->dbal->shouldReceive('addColumn')->with('remember_token', 'string', ['length' => 100]) - ->andReturn($column); + ->andReturn($column); $column->shouldReceive('setNotnull')->with(false)->once(); $this->table->rememberToken('column'); } @@ -324,19 +324,16 @@ public function test_drop_unique() public function test_drop_unique_with_exact_column_names() { $this->dbal->shouldReceive('getIndexes')->andReturn([ - 'index1'=> new Index('index1', ['column1', 'column2', 'column3']), - 'index2'=> new Index('index2', ['column1', 'column2']), - 'index3'=> new Index('index3', ['column1']), - 'index4'=> new Index('index4', ['column1', 'column2', 'column3'], true), - 'index5'=> new Index('index5', ['column1', 'column2'], true), - 'index6'=> new Index('index6', ['column1'], true), - 'index7'=> new Index('index7', ['column1', 'column2', 'column3'], true), - 'index8'=> new Index('index8', ['column1', 'column2'], true), - 'index9'=> new Index('index9', ['column1'], true), + 'index1' => new Index('index1', ['column1', 'column2', 'column3'], true), + 'index2' => new Index('index2', ['column1', 'column2'], true), + 'index3' => new Index('index3', ['column1'], true), + 'index4' => new Index('index4', ['column1', 'column2', 'column3'], true), + 'index5' => new Index('index5', ['column1', 'column2'], true), + 'index6' => new Index('index6', ['column1'], true), ]); + $this->dbal->shouldReceive('dropIndex')->with('index2'); $this->dbal->shouldReceive('dropIndex')->with('index5'); - $this->dbal->shouldReceive('dropIndex')->with('index8'); $this->table->dropUnique(['column1', 'column2']); } @@ -351,14 +348,15 @@ public function test_drop_primary() public function test_drop_primary_with_exact_column_names() { $this->dbal->shouldReceive('getIndexes')->andReturn([ - 'index1'=> new Index('index1', ['column1', 'column2', 'column3']), - 'index2'=> new Index('index2', ['column1', 'column2']), - 'index3'=> new Index('index3', ['column1']), - 'index4'=> new Index('index4', ['column1', 'column2', 'column3'], false, true), - 'index5'=> new Index('index5', ['column1', 'column2'], false, true), - 'index6'=> new Index('index6', ['column1'], false, true), + 'index1' => new Index('index1', ['column1', 'column2', 'column3'], false, true), + 'index2' => new Index('index2', ['column1', 'column2'], false, true), + 'index3' => new Index('index3', ['column1'], false, true), + 'index4' => new Index('index4', ['column1', 'column2', 'column3'], false, true), + 'index5' => new Index('index5', ['column1', 'column2'], false, true), + 'index6' => new Index('index6', ['column1'], false, true), ]); + $this->dbal->shouldReceive('dropIndex')->with('index2'); $this->dbal->shouldReceive('dropIndex')->with('index5'); $this->table->dropPrimary(['column1', 'column2']); @@ -374,18 +372,12 @@ public function test_drop_foreign() public function test_drop_foreign_with_exact_column_names() { $this->dbal->shouldReceive('getIndexes')->andReturn([ - 'index1'=> new Index('index1', ['column1', 'column2', 'column3'], true), - 'index2'=> new Index('index2', ['column1', 'column2'], true), - 'index3'=> new Index('index3', ['column1'], true), - 'index4'=> new Index('index4', ['column1', 'column2', 'column3'], false, true), - 'index5'=> new Index('index5', ['column1', 'column2'], false, true), - 'index6'=> new Index('index6', ['column1'], false, true), - 'index7'=> new Index('index7', ['column1', 'column2', 'column3']), - 'index8'=> new Index('index8', ['column1', 'column2']), - 'index9'=> new Index('index9', ['column1']), + 'index1' => new Index('index1', ['column1', 'column2', 'column3']), + 'index2' => new Index('index2', ['column1', 'column2']), + 'index3' => new Index('index3', ['column1']), ]); - $this->dbal->shouldReceive('dropIndex')->with('index8'); + $this->dbal->shouldReceive('dropIndex')->with('index2'); $this->table->dropForeign(['column1', 'column2']); } @@ -400,15 +392,15 @@ public function test_drop_index() public function test_drop_index_with_exact_column_names() { $this->dbal->shouldReceive('getIndexes')->andReturn([ - 'index1'=> new Index('index1', ['column1', 'column2', 'column3'], true), - 'index2'=> new Index('index2', ['column1', 'column2'], true), - 'index3'=> new Index('index3', ['column1'], true), - 'index4'=> new Index('index4', ['column1', 'column2', 'column3'], false, true), - 'index5'=> new Index('index5', ['column1', 'column2'], false, true), - 'index6'=> new Index('index6', ['column1'], false, true), - 'index7'=> new Index('index7', ['column1', 'column2', 'column3']), - 'index8'=> new Index('index8', ['column1', 'column2']), - 'index9'=> new Index('index9', ['column1']), + 'index1' => new Index('index1', ['column1', 'column2', 'column3'], true), + 'index2' => new Index('index2', ['column1', 'column2'], true), + 'index3' => new Index('index3', ['column1'], true), + 'index4' => new Index('index4', ['column1', 'column2', 'column3'], false, true), + 'index5' => new Index('index5', ['column1', 'column2'], false, true), + 'index6' => new Index('index6', ['column1'], false, true), + 'index7' => new Index('index7', ['column1', 'column2', 'column3']), + 'index8' => new Index('index8', ['column1', 'column2']), + 'index9' => new Index('index9', ['column1']), ]); $this->dbal->shouldReceive('dropIndex')->with('index2');