Skip to content

Commit bdd1db4

Browse files
authored
Merge pull request #307 from yiisoft/drop-table-params
Add `$ifExists` and `$cascade` to `dropTable()` methods
2 parents 311885f + c5af582 commit bdd1db4

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
- New #301: Add `IndexType` class (@Tigrov)
3434
- New #303: Support JSON type (@Tigrov)
3535
- Bug #305: Explicitly mark nullable parameters (@vjik)
36+
- New #307: Add parameters `$ifExists` and `$cascade` to `CommandInterface::dropTable()` and
37+
`DDLQueryBuilderInterface::dropTable()` methods (@vjik)
3638

3739
## 1.3.0 March 21, 2024
3840

src/DDLQueryBuilder.php

+13
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,19 @@ public function dropIndex(string $table, string $name): string
8383
return 'DROP INDEX ' . $this->quoter->quoteTableName($name);
8484
}
8585

86+
/**
87+
* @throws NotSupportedException Oracle doesn't support "IF EXISTS" option on drop table.
88+
*/
89+
public function dropTable(string $table, bool $ifExists = false, bool $cascade = false): string
90+
{
91+
if ($ifExists) {
92+
throw new NotSupportedException('Oracle doesn\'t support "IF EXISTS" option on drop table.');
93+
}
94+
return 'DROP TABLE '
95+
. $this->quoter->quoteTableName($table)
96+
. ($cascade ? ' CASCADE CONSTRAINTS' : '');
97+
}
98+
8699
public function renameTable(string $oldName, string $newName): string
87100
{
88101
return 'ALTER TABLE ' . $this->quoter->quoteTableName($oldName) . ' RENAME TO ' .

tests/CommandTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,25 @@ public function testDropDefaultValue(): void
269269
$command->dropDefaultValue('{{table}}', '{{name}}');
270270
}
271271

272+
public function testDropTableIfExists(): void
273+
{
274+
$command = $this->getConnection()->createCommand();
275+
276+
$this->expectException(NotSupportedException::class);
277+
$this->expectExceptionMessage('Oracle doesn\'t support "IF EXISTS" option on drop table.');
278+
$command->dropTable('{{table}}', ifExists: true);
279+
}
280+
281+
public function testDropTableIfExistsWithExistTable(): void
282+
{
283+
$this->markTestSkipped('Oracle doesn\'t support "IF EXISTS" option on drop table.');
284+
}
285+
286+
public function testDropTableIfExistsWithNonExistTable(): void
287+
{
288+
$this->markTestSkipped('Oracle doesn\'t support "IF EXISTS" option on drop table.');
289+
}
290+
272291
/**
273292
* @throws Exception
274293
* @throws InvalidConfigException

tests/QueryBuilderTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Yiisoft\Db\Oracle\Tests;
66

7+
use PHPUnit\Framework\Attributes\DataProvider;
78
use PHPUnit\Framework\Attributes\DataProviderExternal;
89
use Throwable;
910
use Yiisoft\Db\Exception\Exception;
@@ -681,4 +682,27 @@ public function testPrepareValue(string $expected, mixed $value): void
681682
{
682683
parent::testPrepareValue($expected, $value);
683684
}
685+
686+
#[DataProvider('dataDropTable')]
687+
public function testDropTable(string $expected, ?bool $ifExists, ?bool $cascade): void
688+
{
689+
if ($ifExists) {
690+
$qb = $this->getConnection()->getQueryBuilder();
691+
692+
$this->expectException(NotSupportedException::class);
693+
$this->expectExceptionMessage('Oracle doesn\'t support "IF EXISTS" option on drop table.');
694+
695+
$cascade === null
696+
? $qb->dropTable('customer', ifExists: true)
697+
: $qb->dropTable('customer', ifExists: true, cascade: $cascade);
698+
699+
return;
700+
}
701+
702+
if ($cascade) {
703+
$expected = str_replace('CASCADE', 'CASCADE CONSTRAINTS', $expected);
704+
}
705+
706+
parent::testDropTable($expected, $ifExists, $cascade);
707+
}
684708
}

0 commit comments

Comments
 (0)