diff --git a/src/Sql/Ddl/AlterTable.php b/src/Sql/Ddl/AlterTable.php index 3a784406..33e11957 100644 --- a/src/Sql/Ddl/AlterTable.php +++ b/src/Sql/Ddl/AlterTable.php @@ -15,6 +15,7 @@ class AlterTable extends AbstractSql implements SqlInterface public const CHANGE_COLUMNS = 'changeColumns'; public const DROP_COLUMNS = 'dropColumns'; public const DROP_CONSTRAINTS = 'dropConstraints'; + public const DROP_INDEXES = 'dropIndexes'; public const TABLE = 'table'; /** @var array */ @@ -32,6 +33,9 @@ class AlterTable extends AbstractSql implements SqlInterface /** @var array */ protected $dropConstraints = []; + /** @var array */ + protected $dropIndexes = []; + /** * Specifications for Sql String generation * @@ -64,6 +68,11 @@ class AlterTable extends AbstractSql implements SqlInterface [1 => "DROP CONSTRAINT %1\$s,\n", 'combinedby' => ""], ], ], + self::DROP_INDEXES => [ + '%1$s' => [ + [1 => "DROP INDEX %1\$s,\n", 'combinedby' => ''], + ], + ], ]; /** @var string */ @@ -141,6 +150,17 @@ public function addConstraint(Constraint\ConstraintInterface $constraint) return $this; } + /** + * @param string $name + * @return self Provides a fluent interface + */ + public function dropIndex($name) + { + $this->dropIndexes[] = $name; + + return $this; + } + /** * @param string|null $key * @return array @@ -154,6 +174,7 @@ public function getRawState($key = null) self::CHANGE_COLUMNS => $this->changeColumns, self::ADD_CONSTRAINTS => $this->addConstraints, self::DROP_CONSTRAINTS => $this->dropConstraints, + self::DROP_INDEXES => $this->dropIndexes, ]; return isset($key) && array_key_exists($key, $rawState) ? $rawState[$key] : $rawState; @@ -222,4 +243,15 @@ protected function processDropConstraints(?PlatformInterface $adapterPlatform = return [$sqls]; } + + /** @return string[] */ + protected function processDropIndexes(?PlatformInterface $adapterPlatform = null) + { + $sqls = []; + foreach ($this->dropIndexes as $index) { + $sqls[] = $adapterPlatform->quoteIdentifier($index); + } + + return [$sqls]; + } } diff --git a/test/unit/Sql/Ddl/AlterTableTest.php b/test/unit/Sql/Ddl/AlterTableTest.php index 3f0013fc..73fa6c06 100644 --- a/test/unit/Sql/Ddl/AlterTableTest.php +++ b/test/unit/Sql/Ddl/AlterTableTest.php @@ -81,6 +81,16 @@ public function testAddConstraint() self::assertEquals([$conMock], $at->getRawState($at::ADD_CONSTRAINTS)); } + /** + * @covers \Laminas\Db\Sql\Ddl\AlterTable::dropIndex + */ + public function testDropIndex() + { + $at = new AlterTable(); + self::assertSame($at, $at->dropIndex('foo')); + self::assertEquals(['foo'], $at->getRawState($at::DROP_INDEXES)); + } + /** * @covers \Laminas\Db\Sql\Ddl\AlterTable::getSqlString * @todo Implement testGetSqlString(). @@ -92,14 +102,16 @@ public function testGetSqlString() $at->changeColumn('name', new Column\Varchar('new_name', 50)); $at->dropColumn('foo'); $at->addConstraint(new Constraint\ForeignKey('my_fk', 'other_id', 'other_table', 'id', 'CASCADE', 'CASCADE')); - $at->dropConstraint('my_index'); + $at->dropConstraint('my_constraint'); + $at->dropIndex('my_index'); $expected = <<getSqlString();