Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DDL drop indexes #200

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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