Skip to content

Commit

Permalink
Merge pull request #307 from yiisoft/drop-table-params
Browse files Browse the repository at this point in the history
Add `$ifExists` and `$cascade` to `dropTable()` methods
  • Loading branch information
vjik authored Mar 11, 2025
2 parents 311885f + c5af582 commit bdd1db4
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 13 additions & 0 deletions src/DDLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ 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 '
. $this->quoter->quoteTableName($table)
. ($cascade ? ' CASCADE CONSTRAINTS' : '');
}

public function renameTable(string $oldName, string $newName): string
{
return 'ALTER TABLE ' . $this->quoter->quoteTableName($oldName) . ' RENAME TO ' .
Expand Down
19 changes: 19 additions & 0 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -681,4 +682,27 @@ 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 ($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);
}
}

0 comments on commit bdd1db4

Please sign in to comment.