From 72fba96bc8b2387359f2d96001e0d75e3ead83ac Mon Sep 17 00:00:00 2001 From: Will Jones Date: Wed, 7 Jul 2021 22:37:16 +0100 Subject: [PATCH 1/2] Fix drop indexes DROP CONSTRAINT doesn't successfully drop indexes with Amazon Aurora. Change to DROP INDEX syntax as this is supported and functions as intended Signed-off-by: Will Jones --- src/Sql/Ddl/AlterTable.php | 32 ++++++++++++++++++++++++++++ test/unit/Sql/Ddl/AlterTableTest.php | 16 ++++++++++++-- 2 files changed, 46 insertions(+), 2 deletions(-) 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..c3809840 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(); From a545d1525c6d86a12f54f7bed7795c34cb89e97b Mon Sep 17 00:00:00 2001 From: Will Jones Date: Thu, 8 Jul 2021 10:25:48 +0100 Subject: [PATCH 2/2] Fix broken alter table test Signed-off-by: Will Jones --- test/unit/Sql/Ddl/AlterTableTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/Sql/Ddl/AlterTableTest.php b/test/unit/Sql/Ddl/AlterTableTest.php index c3809840..73fa6c06 100644 --- a/test/unit/Sql/Ddl/AlterTableTest.php +++ b/test/unit/Sql/Ddl/AlterTableTest.php @@ -110,7 +110,7 @@ public function testGetSqlString() CHANGE COLUMN "name" "new_name" VARCHAR(50) NOT NULL, DROP COLUMN "foo", ADD CONSTRAINT "my_fk" FOREIGN KEY ("other_id") REFERENCES "other_table" ("id") ON DELETE CASCADE ON UPDATE CASCADE, - DROP CONSTRAINT "my_constraint" + DROP CONSTRAINT "my_constraint", DROP INDEX "my_index" EOS;