Skip to content

Commit

Permalink
Merge pull request #200 from willjones9/bugfix-alter-table-indexes
Browse files Browse the repository at this point in the history
Fix drop indexes
  • Loading branch information
Xerkus committed Feb 1, 2024
2 parents 05093d3 + a545d15 commit a0a1eb4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
32 changes: 32 additions & 0 deletions src/Sql/Ddl/AlterTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -32,6 +33,9 @@ class AlterTable extends AbstractSql implements SqlInterface
/** @var array */
protected $dropConstraints = [];

/** @var array */
protected $dropIndexes = [];

/**
* Specifications for Sql String generation
*
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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];
}
}
16 changes: 14 additions & 2 deletions test/unit/Sql/Ddl/AlterTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand All @@ -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 = <<<EOS
ALTER TABLE "foo"
ADD COLUMN "another" VARCHAR(255) NOT NULL,
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_index"
DROP CONSTRAINT "my_constraint",
DROP INDEX "my_index"
EOS;

$actual = $at->getSqlString();
Expand Down

0 comments on commit a0a1eb4

Please sign in to comment.