From 0e398ec80cc877acb7fdde653503638b78f30b8c Mon Sep 17 00:00:00 2001 From: Holger Schletz Date: Sun, 6 Jun 2021 19:46:06 +0200 Subject: [PATCH] Make Table::removeUniqueConstraint() actually work Table::removeUniqueConstraint() used the wrong method (hasForeignKey()) instead of hasUniqueConstraint() to check for existing unique constraint, causing removeUniqueConstraint() to fail unconditionally. Also adding tests for removeUniqueConstraint(). --- src/Schema/Table.php | 2 +- tests/Schema/TableTest.php | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Schema/Table.php b/src/Schema/Table.php index 633c374a5fb..d330d03f47e 100644 --- a/src/Schema/Table.php +++ b/src/Schema/Table.php @@ -686,7 +686,7 @@ public function removeUniqueConstraint(string $name): void { $name = $this->normalizeIdentifier($name); - if (! $this->hasForeignKey($name)) { + if (! $this->hasUniqueConstraint($name)) { throw SchemaException::uniqueConstraintDoesNotExist($name, $this->_name); } diff --git a/tests/Schema/TableTest.php b/tests/Schema/TableTest.php index b0c7dc35163..60ecafba63e 100644 --- a/tests/Schema/TableTest.php +++ b/tests/Schema/TableTest.php @@ -831,4 +831,25 @@ public function testTableComment(): void $table->setComment('foo'); self::assertEquals('foo', $table->getComment()); } + + public function testRemoveUniqueConstraint(): void + { + $table = new Table('foo'); + $table->addColumn('bar', 'integer'); + $table->addUniqueConstraint(['bar'], 'unique_constraint'); + + $table->removeUniqueConstraint('unique_constraint'); + + self::assertFalse($table->hasUniqueConstraint('unique_constraint')); + } + + public function testRemoveUniqueConstraintUnknownNameThrowsException(): void + { + $this->expectException(SchemaException::class); + + $table = new Table('foo'); + $table->addColumn('bar', 'integer'); + + $table->removeUniqueConstraint('unique_constraint'); + } }