Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Fix AlterTable::dropConstraint() by allowing the use of decorator #247

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
26 changes: 25 additions & 1 deletion docs/book/sql-ddl.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,32 @@ $table->changeColumn('name', Column\Varchar('new_name', 50));
You may also *drop* existing columns or constraints:

```php
use \Zend\Db\Metadata\Object\ConstraintObject;

$table->dropColumn('foo');
$table->dropConstraint('my_index');

$constraint = new ConstraintObject('my_index', null);
$table->dropConstraint($constraint);
```

Notice: On MySQL, you need to specify the type of constraint you want to drop.
To do so you may use a `\Zend\Db\Metadata\Object\ConstraintObject` and set its
type accordingly or directly a subclass of
`\Zend\Db\Sql\Ddl\Constraint\ConstraintInterface`.
```php
use \Zend\Db\Metadata\Object\ConstraintObject;

$fkConstraint = new ConstraintObject('my_fk', null);
$fkConstraint->setType('FOREIGN KEY');
$table->dropConstraint($fkConstraint);
```

```php
use \Zend\Db\Sql\Ddl\Constraint\UniqueKey;
$idxConstraint = new \Zend\Db\Sql\Ddl\Constraint\UniqueKey(
null, 'my_unique_index'
);
$table->dropConstraint($idxConstraint);
```

## Dropping Tables
Expand Down
15 changes: 8 additions & 7 deletions src/Sql/Ddl/AlterTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Zend\Db\Sql\Ddl;

use Zend\Db\Adapter\Platform\PlatformInterface;
use Zend\Db\Metadata\Object\ConstraintObject;
use Zend\Db\Sql\AbstractSql;
use Zend\Db\Sql\TableIdentifier;

Expand All @@ -28,7 +29,7 @@ class AlterTable extends AbstractSql implements SqlInterface
protected $addColumns = [];

/**
* @var array
* @var Constraint\ConstraintInterface[]
*/
protected $addConstraints = [];

Expand All @@ -43,7 +44,7 @@ class AlterTable extends AbstractSql implements SqlInterface
protected $dropColumns = [];

/**
* @var array
* @var Constraint\ConstraintInterface[]|ConstraintObject[]
*/
protected $dropConstraints = [];

Expand Down Expand Up @@ -75,7 +76,7 @@ class AlterTable extends AbstractSql implements SqlInterface
],
self::DROP_CONSTRAINTS => [
"%1\$s" => [
[1 => "DROP CONSTRAINT %1\$s,\n", 'combinedby' => ""],
[1 => "DROP CONSTRAINT %1\$s,\n", 'combinedby' => " "],
]
]
];
Expand Down Expand Up @@ -139,12 +140,12 @@ public function dropColumn($name)
}

/**
* @param string $name
* @param Constraint\ConstraintInterface|ConstraintObject $constraint
* @return self Provides a fluent interface
*/
public function dropConstraint($name)
public function dropConstraint($constraint)
{
$this->dropConstraints[] = $name;
$this->dropConstraints[] = $constraint;

return $this;
}
Expand Down Expand Up @@ -230,7 +231,7 @@ protected function processDropConstraints(PlatformInterface $adapterPlatform = n
{
$sqls = [];
foreach ($this->dropConstraints as $constraint) {
$sqls[] = $adapterPlatform->quoteIdentifier($constraint);
$sqls[] = $adapterPlatform->quoteIdentifier($constraint->getName());
}

return [$sqls];
Expand Down
2 changes: 2 additions & 0 deletions src/Sql/Ddl/Constraint/ConstraintInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@
interface ConstraintInterface extends ExpressionInterface
{
public function getColumns();

public function getName();
}
42 changes: 42 additions & 0 deletions src/Sql/Platform/Mysql/Ddl/AlterTableDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
namespace Zend\Db\Sql\Platform\Mysql\Ddl;

use Zend\Db\Adapter\Platform\PlatformInterface;
use Zend\Db\Metadata\Object\ConstraintObject;
use Zend\Db\Sql\Ddl\AlterTable;
use Zend\Db\Sql\Ddl\Constraint\ForeignKey;
use Zend\Db\Sql\Ddl\Constraint\PrimaryKey;
use Zend\Db\Sql\Ddl\Index\AbstractIndex;
use Zend\Db\Sql\Platform\PlatformDecoratorInterface;

class AlterTableDecorator extends AlterTable implements PlatformDecoratorInterface
Expand Down Expand Up @@ -43,6 +47,11 @@ class AlterTableDecorator extends AlterTable implements PlatformDecoratorInterfa
public function setSubject($subject)
{
$this->subject = $subject;
$this->subject->specifications[self::DROP_CONSTRAINTS] = [
"%1\$s" => [
[2 => "DROP %1\$s %2\$s,\n", 'combinedby' => " "],
]
];

return $this;
}
Expand Down Expand Up @@ -248,4 +257,37 @@ private function compareColumnOptions($columnA, $columnB)

return $columnA - $columnB;
}

protected function processDropConstraints(PlatformInterface $adapterPlatform = null)
{
$sqls = [];
foreach ($this->dropConstraints as $constraint) {
$sqls[] = [
$this->getConstraintType($constraint),
$adapterPlatform->quoteIdentifier($constraint->getName())
];
}

return [$sqls];
}

/**
* @param $constraint
* @return string
*/
protected function getConstraintType($constraint)
{
if ($constraint instanceof ConstraintObject) {
return $constraint->getType();
}
if ($constraint instanceof PrimaryKey) {
return 'PRIMARY KEY';
} elseif ($constraint instanceof ForeignKey) {
return 'FOREIGN KEY';
} elseif ($constraint instanceof AbstractIndex) {
return 'INDEX';
} else {
return 'KEY';
}
}
}
12 changes: 8 additions & 4 deletions test/unit/Sql/Ddl/AlterTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace ZendTest\Db\Sql\Ddl;

use PHPUnit\Framework\TestCase;
use Zend\Db\Metadata\Object\ConstraintObject;
use Zend\Db\Sql\Ddl\AlterTable;
use Zend\Db\Sql\Ddl\Column;
use Zend\Db\Sql\Ddl\Constraint;
Expand Down Expand Up @@ -68,8 +69,9 @@ public function testDropColumn()
public function testDropConstraint()
{
$at = new AlterTable();
self::assertSame($at, $at->dropConstraint('foo'));
self::assertEquals(['foo'], $at->getRawState($at::DROP_CONSTRAINTS));
$constraint = new ConstraintObject('foo', null);
$this->assertSame($at, $at->dropConstraint($constraint));
$this->assertEquals([$constraint], $at->getRawState($at::DROP_CONSTRAINTS));
}

/**
Expand All @@ -95,14 +97,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(new ConstraintObject('my_index', null));
$at->dropConstraint(new Constraint\UniqueKey(null, 'my_unique_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_index",
DROP CONSTRAINT "my_unique_index"
EOS;

$actual = $at->getSqlString();
Expand Down
14 changes: 12 additions & 2 deletions test/unit/Sql/Platform/Mysql/Ddl/AlterTableDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

use PHPUnit\Framework\TestCase;
use Zend\Db\Adapter\Platform\Mysql;
use Zend\Db\Metadata\Object\ConstraintObject;
use Zend\Db\Sql\Ddl\AlterTable;
use Zend\Db\Sql\Ddl\Column\Column;
use Zend\Db\Sql\Ddl\Constraint\PrimaryKey;
use Zend\Db\Sql\Ddl\Constraint\UniqueKey;
use Zend\Db\Sql\Platform\Mysql\Ddl\AlterTableDecorator;

class AlterTableDecoratorTest extends TestCase
Expand Down Expand Up @@ -46,9 +48,17 @@ public function testGetSqlString()
$col->addConstraint(new PrimaryKey());
$ct->addColumn($col);

self::assertEquals(
$fk = new ConstraintObject('my_fk', null);
$fk->setType('FOREIGN KEY');
$ct->dropConstraint($fk);

$ct->dropConstraint(new UniqueKey(null, 'my_unique_index'));

$this->assertEquals(
"ALTER TABLE `foo`\n ADD COLUMN `bar` INTEGER UNSIGNED ZEROFILL " .
"NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'baz' AFTER `bar`",
"NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'baz' AFTER `bar`,\n" .
" DROP FOREIGN KEY `my_fk`,\n" .
" DROP KEY `my_unique_index`",
@$ctd->getSqlString(new Mysql())
);
}
Expand Down