Skip to content

Commit a0a1eb4

Browse files
authored
Merge pull request laminas#200 from willjones9/bugfix-alter-table-indexes
Fix drop indexes
2 parents 05093d3 + a545d15 commit a0a1eb4

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

src/Sql/Ddl/AlterTable.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class AlterTable extends AbstractSql implements SqlInterface
1515
public const CHANGE_COLUMNS = 'changeColumns';
1616
public const DROP_COLUMNS = 'dropColumns';
1717
public const DROP_CONSTRAINTS = 'dropConstraints';
18+
public const DROP_INDEXES = 'dropIndexes';
1819
public const TABLE = 'table';
1920

2021
/** @var array */
@@ -32,6 +33,9 @@ class AlterTable extends AbstractSql implements SqlInterface
3233
/** @var array */
3334
protected $dropConstraints = [];
3435

36+
/** @var array */
37+
protected $dropIndexes = [];
38+
3539
/**
3640
* Specifications for Sql String generation
3741
*
@@ -64,6 +68,11 @@ class AlterTable extends AbstractSql implements SqlInterface
6468
[1 => "DROP CONSTRAINT %1\$s,\n", 'combinedby' => ""],
6569
],
6670
],
71+
self::DROP_INDEXES => [
72+
'%1$s' => [
73+
[1 => "DROP INDEX %1\$s,\n", 'combinedby' => ''],
74+
],
75+
],
6776
];
6877

6978
/** @var string */
@@ -141,6 +150,17 @@ public function addConstraint(Constraint\ConstraintInterface $constraint)
141150
return $this;
142151
}
143152

153+
/**
154+
* @param string $name
155+
* @return self Provides a fluent interface
156+
*/
157+
public function dropIndex($name)
158+
{
159+
$this->dropIndexes[] = $name;
160+
161+
return $this;
162+
}
163+
144164
/**
145165
* @param string|null $key
146166
* @return array
@@ -154,6 +174,7 @@ public function getRawState($key = null)
154174
self::CHANGE_COLUMNS => $this->changeColumns,
155175
self::ADD_CONSTRAINTS => $this->addConstraints,
156176
self::DROP_CONSTRAINTS => $this->dropConstraints,
177+
self::DROP_INDEXES => $this->dropIndexes,
157178
];
158179

159180
return isset($key) && array_key_exists($key, $rawState) ? $rawState[$key] : $rawState;
@@ -222,4 +243,15 @@ protected function processDropConstraints(?PlatformInterface $adapterPlatform =
222243

223244
return [$sqls];
224245
}
246+
247+
/** @return string[] */
248+
protected function processDropIndexes(?PlatformInterface $adapterPlatform = null)
249+
{
250+
$sqls = [];
251+
foreach ($this->dropIndexes as $index) {
252+
$sqls[] = $adapterPlatform->quoteIdentifier($index);
253+
}
254+
255+
return [$sqls];
256+
}
225257
}

test/unit/Sql/Ddl/AlterTableTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ public function testAddConstraint()
8181
self::assertEquals([$conMock], $at->getRawState($at::ADD_CONSTRAINTS));
8282
}
8383

84+
/**
85+
* @covers \Laminas\Db\Sql\Ddl\AlterTable::dropIndex
86+
*/
87+
public function testDropIndex()
88+
{
89+
$at = new AlterTable();
90+
self::assertSame($at, $at->dropIndex('foo'));
91+
self::assertEquals(['foo'], $at->getRawState($at::DROP_INDEXES));
92+
}
93+
8494
/**
8595
* @covers \Laminas\Db\Sql\Ddl\AlterTable::getSqlString
8696
* @todo Implement testGetSqlString().
@@ -92,14 +102,16 @@ public function testGetSqlString()
92102
$at->changeColumn('name', new Column\Varchar('new_name', 50));
93103
$at->dropColumn('foo');
94104
$at->addConstraint(new Constraint\ForeignKey('my_fk', 'other_id', 'other_table', 'id', 'CASCADE', 'CASCADE'));
95-
$at->dropConstraint('my_index');
105+
$at->dropConstraint('my_constraint');
106+
$at->dropIndex('my_index');
96107
$expected = <<<EOS
97108
ALTER TABLE "foo"
98109
ADD COLUMN "another" VARCHAR(255) NOT NULL,
99110
CHANGE COLUMN "name" "new_name" VARCHAR(50) NOT NULL,
100111
DROP COLUMN "foo",
101112
ADD CONSTRAINT "my_fk" FOREIGN KEY ("other_id") REFERENCES "other_table" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
102-
DROP CONSTRAINT "my_index"
113+
DROP CONSTRAINT "my_constraint",
114+
DROP INDEX "my_index"
103115
EOS;
104116

105117
$actual = $at->getSqlString();

0 commit comments

Comments
 (0)