From ce07bb80f99114c997f1857e4f4824f46bb394cd Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Mon, 10 Mar 2025 16:19:02 +0300 Subject: [PATCH 1/2] Add `$ifExists` and `$cascade` to `dropTable()` methods --- CHANGELOG.md | 2 ++ src/DDLQueryBuilder.php | 8 ++++++++ tests/QueryBuilderTest.php | 10 ++++++++++ 3 files changed, 20 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 24215a0b..b100f4cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,8 @@ - New #301: Add `IndexType` class (@Tigrov) - New #303: Support JSON type (@Tigrov) - Bug #305: Explicitly mark nullable parameters (@vjik) +- New #307: Add parameters `$ifExists` and `$cascade` to `CommandInterface::dropTable()` and + `DDLQueryBuilderInterface::dropTable()` methods (@vjik) ## 1.3.0 March 21, 2024 diff --git a/src/DDLQueryBuilder.php b/src/DDLQueryBuilder.php index abfc0bb5..ec2ecb61 100644 --- a/src/DDLQueryBuilder.php +++ b/src/DDLQueryBuilder.php @@ -83,6 +83,14 @@ public function dropIndex(string $table, string $name): string return 'DROP INDEX ' . $this->quoter->quoteTableName($name); } + public function dropTable(string $table, bool $ifExists = false, bool $cascade = false): string + { + return 'DROP TABLE ' + . ($ifExists ? 'IF EXISTS ' : '') + . $this->quoter->quoteTableName($table) + . ($cascade ? ' CASCADE CONSTRAINTS' : ''); + } + public function renameTable(string $oldName, string $newName): string { return 'ALTER TABLE ' . $this->quoter->quoteTableName($oldName) . ' RENAME TO ' . diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 81593445..36e55169 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -4,6 +4,7 @@ namespace Yiisoft\Db\Oracle\Tests; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\DataProviderExternal; use Throwable; use Yiisoft\Db\Exception\Exception; @@ -681,4 +682,13 @@ public function testPrepareValue(string $expected, mixed $value): void { parent::testPrepareValue($expected, $value); } + + #[DataProvider('dataDropTable')] + public function testDropTable(string $expected, ?bool $ifExists, ?bool $cascade): void + { + if ($cascade) { + $expected = str_replace('CASCADE', 'CASCADE CONSTRAINTS', $expected); + } + parent::testDropTable($expected, $ifExists, $cascade); + } } From c5af5822d5c76d2724813947747f5264950782bf Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Mon, 10 Mar 2025 16:33:53 +0300 Subject: [PATCH 2/2] improve --- src/DDLQueryBuilder.php | 7 ++++++- tests/CommandTest.php | 19 +++++++++++++++++++ tests/QueryBuilderTest.php | 14 ++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/DDLQueryBuilder.php b/src/DDLQueryBuilder.php index ec2ecb61..68ac100c 100644 --- a/src/DDLQueryBuilder.php +++ b/src/DDLQueryBuilder.php @@ -83,10 +83,15 @@ public function dropIndex(string $table, string $name): string return 'DROP INDEX ' . $this->quoter->quoteTableName($name); } + /** + * @throws NotSupportedException Oracle doesn't support "IF EXISTS" option on drop table. + */ public function dropTable(string $table, bool $ifExists = false, bool $cascade = false): string { + if ($ifExists) { + throw new NotSupportedException('Oracle doesn\'t support "IF EXISTS" option on drop table.'); + } return 'DROP TABLE ' - . ($ifExists ? 'IF EXISTS ' : '') . $this->quoter->quoteTableName($table) . ($cascade ? ' CASCADE CONSTRAINTS' : ''); } diff --git a/tests/CommandTest.php b/tests/CommandTest.php index 6735713f..1ec58cb1 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -269,6 +269,25 @@ public function testDropDefaultValue(): void $command->dropDefaultValue('{{table}}', '{{name}}'); } + public function testDropTableIfExists(): void + { + $command = $this->getConnection()->createCommand(); + + $this->expectException(NotSupportedException::class); + $this->expectExceptionMessage('Oracle doesn\'t support "IF EXISTS" option on drop table.'); + $command->dropTable('{{table}}', ifExists: true); + } + + public function testDropTableIfExistsWithExistTable(): void + { + $this->markTestSkipped('Oracle doesn\'t support "IF EXISTS" option on drop table.'); + } + + public function testDropTableIfExistsWithNonExistTable(): void + { + $this->markTestSkipped('Oracle doesn\'t support "IF EXISTS" option on drop table.'); + } + /** * @throws Exception * @throws InvalidConfigException diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 36e55169..14db8739 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -686,9 +686,23 @@ public function testPrepareValue(string $expected, mixed $value): void #[DataProvider('dataDropTable')] public function testDropTable(string $expected, ?bool $ifExists, ?bool $cascade): void { + if ($ifExists) { + $qb = $this->getConnection()->getQueryBuilder(); + + $this->expectException(NotSupportedException::class); + $this->expectExceptionMessage('Oracle doesn\'t support "IF EXISTS" option on drop table.'); + + $cascade === null + ? $qb->dropTable('customer', ifExists: true) + : $qb->dropTable('customer', ifExists: true, cascade: $cascade); + + return; + } + if ($cascade) { $expected = str_replace('CASCADE', 'CASCADE CONSTRAINTS', $expected); } + parent::testDropTable($expected, $ifExists, $cascade); } }