Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add $ifExists and $cascade to dropTable() methods #379

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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 @@ -27,6 +27,8 @@
- New #374: Add `IndexType` and `IndexMethod` classes (@Tigrov)
- Enh #376: Move `JsonExpressionBuilder` and JSON type tests to `yiisoft/db` package (@Tigrov)
- Bug #377: Explicitly mark nullable parameters (@vjik)
- New #379: Add parameters `$ifExists` and `$cascade` to `CommandInterface::dropTable()` and
`DDLQueryBuilderInterface::dropTable()` methods (@vjik)

## 1.2.0 March 21, 2024

Expand Down
11 changes: 11 additions & 0 deletions src/DDLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,15 @@ private function getColumnDefinition(string $table, string $column): string

return $matches[2];
}

/**
* @throws NotSupportedException MySQL doesn't support cascade drop table.
*/
public function dropTable(string $table, bool $ifExists = false, bool $cascade = false): string
{
if ($cascade) {
throw new NotSupportedException('MySQL doesn\'t support cascade drop table.');
}
return parent::dropTable($table, $ifExists, false);
}
}
9 changes: 9 additions & 0 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ public function testDropDefaultValue(): void
parent::testDropDefaultValue();
}

public function testDropTableCascade(): void
{
$command = $this->getConnection()->createCommand();

$this->expectException(NotSupportedException::class);
$this->expectExceptionMessage('MySQL doesn\'t support cascade drop table.');
$command->dropTable('{{table}}', cascade: true);
}

/**
* @dataProvider \Yiisoft\Db\Mysql\Tests\Provider\CommandProvider::rawSql
*
Expand Down
20 changes: 20 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\Mysql\Tests;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\DataProviderExternal;
use Throwable;
use Yiisoft\Db\Command\Param;
Expand Down Expand Up @@ -758,4 +759,23 @@ public function testBuildColumnDefinition(string $expected, ColumnInterface|stri
{
parent::testBuildColumnDefinition($expected, $column);
}

#[DataProvider('dataDropTable')]
public function testDropTable(string $expected, ?bool $ifExists, ?bool $cascade): void
{
if ($cascade) {
$qb = $this->getConnection()->getQueryBuilder();

$this->expectException(NotSupportedException::class);
$this->expectExceptionMessage('MySQL doesn\'t support cascade drop table.');

$ifExists === null
? $qb->dropTable('customer', cascade: true)
: $qb->dropTable('customer', ifExists: $ifExists, cascade: true);

return;
}

parent::testDropTable($expected, $ifExists, $cascade);
}
}
Loading