Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading