Skip to content

Commit

Permalink
Add $ifExists and $cascade to dropTable() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Sep 17, 2024
1 parent e7d2f0b commit 524378b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Enh #314: Implement `ColumnFactory` class (@Tigrov)
- Enh #317: Separate column type constants (@Tigrov)
- Enh #318: Realize `ColumnBuilder` class (@Tigrov)
- Enh #319: Set more specific result type in `Connection::createCommand()` method (@vjik)

## 1.2.0 March 21, 2024

Expand Down
9 changes: 9 additions & 0 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Yiisoft\Db\Driver\Pdo\AbstractPdoCommand;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidArgumentException;
use Yiisoft\Db\Exception\NotSupportedException;

use function array_pop;
use function count;
Expand All @@ -21,6 +22,14 @@
*/
final class Command extends AbstractPdoCommand
{
public function dropTable(string $table, bool $ifExists = false, bool $cascade = false): static
{
if ($cascade) {
throw new NotSupportedException('SQLite doesn\'t support cascade drop table.');
}
return parent::dropTable($table, $ifExists);
}

public function insertWithReturningPks(string $table, array $columns): bool|array
{
$params = [];
Expand Down
2 changes: 1 addition & 1 deletion src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __clone()
}
}

public function createCommand(string $sql = null, array $params = []): PdoCommandInterface
public function createCommand(string $sql = null, array $params = []): Command
{
$command = new Command($this);

Expand Down
13 changes: 9 additions & 4 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,15 @@ public function testDropPrimaryKey(): void
$command->dropPrimaryKey('{{table}}', '{{name}}');
}

/**
* @throws Exception
* @throws InvalidConfigException
*/
public function testDropTableCascade(): void
{
$command = $this->getConnection()->createCommand();

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

public function testDropUnique(): void
{
$db = $this->getConnection();
Expand Down
6 changes: 1 addition & 5 deletions tests/Support/TestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ trait TestTrait
{
private string $dsn = '';

/**
* @throws Exception
* @throws InvalidConfigException
*/
protected function getConnection(bool $fixture = false): PdoConnectionInterface
protected function getConnection(bool $fixture = false): Connection
{
$db = new Connection(new Driver($this->getDsn()), DbHelper::getSchemaCache());

Expand Down

0 comments on commit 524378b

Please sign in to comment.