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 c765ca4 commit 449ffd4
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 37 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
- Enh #875: Ignore "Packets out of order..." warnings in `AbstractPdoCommand::internalExecute()` method (@Tigrov)
- Enh #877: Separate column type constants (@Tigrov)
- Enh #878: Realize `ColumnBuilder` class (@Tigrov)
- New #773: Add parameters `$ifExists` and `$cascade` to `CommandInterface::dropTable()` and
`DDLQueryBuilderInterface::dropTable()` methods (@Tigrov)

## 1.3.0 March 21, 2024

Expand Down
2 changes: 2 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,5 @@ Each table column has its own class in the `Yiisoft\Db\Schema\Column` namespace
- Allow `ExpressionInterface` for `$alias` parameter of `QueryPartsInterface::withQuery()` method;
- Allow `QueryInterface::one()` to return an object;
- Allow `QueryInterface::all()` to return array of objects;
- Add parameters `$ifExists` and `$cascade` to `CommandInterface::dropTable()` and
`DDLQueryBuilderInterface::dropTable()` methods.
4 changes: 2 additions & 2 deletions src/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,9 @@ public function dropPrimaryKey(string $table, string $name): static
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}

public function dropTable(string $table): static
public function dropTable(string $table, bool $ifExists = false, bool $cascade = false): static
{
$sql = $this->getQueryBuilder()->dropTable($table);
$sql = $this->getQueryBuilder()->dropTable($table, $ifExists, $cascade);
return $this->setSql($sql)->requireTableSchemaRefresh($table);
}

Expand Down
4 changes: 3 additions & 1 deletion src/Command/CommandInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,10 +471,12 @@ public function dropPrimaryKey(string $table, string $name): static;
* Creates an SQL command for dropping a DB table.
*
* @param string $table The name of the table to drop.
* @param bool $ifExists Do not throw an error if the table does not exist.
* @param bool $cascade Automatically drop objects that depend on the table.
*
* Note: The method will quote the `table` parameter before using it in the generated SQL.
*/
public function dropTable(string $table): static;
public function dropTable(string $table, bool $ifExists = false, bool $cascade = false): static;

/**
* Creates an SQL command for dropping a unique constraint.
Expand Down
2 changes: 1 addition & 1 deletion src/Debug/CommandInterfaceProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public function dropPrimaryKey(string $table, string $name): static
/**
* @psalm-suppress MixedArgument
*/
public function dropTable(string $table): static
public function dropTable(string $table, bool $ifExists = false, bool $cascade = false): static
{
return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
}
Expand Down
7 changes: 5 additions & 2 deletions src/QueryBuilder/AbstractDDLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,12 @@ public function dropPrimaryKey(string $table, string $name): string
. $this->quoter->quoteColumnName($name);
}

public function dropTable(string $table): string
public function dropTable(string $table, bool $ifExists = false, bool $cascade = false): string
{
return 'DROP TABLE ' . $this->quoter->quoteTableName($table);
return 'DROP TABLE '
. ($ifExists ? 'IF EXISTS ' : '')
. $this->quoter->quoteTableName($table)
. ($cascade ? ' CASCADE' : '');
}

public function dropUnique(string $table, string $name): string
Expand Down
4 changes: 2 additions & 2 deletions src/QueryBuilder/AbstractQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,9 @@ public function dropPrimaryKey(string $table, string $name): string
return $this->ddlBuilder->dropPrimaryKey($table, $name);
}

public function dropTable(string $table): string
public function dropTable(string $table, bool $ifExists = false, bool $cascade = false): string
{
return $this->ddlBuilder->dropTable($table);
return $this->ddlBuilder->dropTable($table, $ifExists, $cascade);
}

public function dropUnique(string $table, string $name): string
Expand Down
4 changes: 3 additions & 1 deletion src/QueryBuilder/DDLQueryBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,12 +361,14 @@ public function dropPrimaryKey(string $table, string $name): string;
* Builds an SQL statement for dropping a DB table.
*
* @param string $table The table to drop.
* @param bool $ifExists Do not throw an error if the table does not exist.
* @param bool $cascade Automatically drop objects that depend on the table.
*
* @return string The SQL statement for dropping a DB table.
*
* Note: The method will quote the `table` parameter before using it in the generated SQL.
*/
public function dropTable(string $table): string;
public function dropTable(string $table, bool $ifExists = false, bool $cascade = false): string;

/**
* Creates an SQL command for dropping a unique constraint.
Expand Down
40 changes: 29 additions & 11 deletions tests/AbstractQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Closure;
use JsonException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use stdClass;
use Throwable;
Expand Down Expand Up @@ -1791,21 +1792,38 @@ public function testDropPrimaryKey(): void
);
}

public function testDropTable(): void
public static function dataDropTable(): iterable
{
$db = $this->getConnection();
yield ['DROP TABLE [[customer]]', null, null];
yield ['DROP TABLE IF EXISTS [[customer]]', true, null];
yield ['DROP TABLE [[customer]]', false, null];
yield ['DROP TABLE [[customer]] CASCADE', null, true];
yield ['DROP TABLE [[customer]]', null, false];
yield ['DROP TABLE [[customer]]', false, false];
yield ['DROP TABLE IF EXISTS [[customer]] CASCADE', true, true];
yield ['DROP TABLE IF EXISTS [[customer]]', true, false];
yield ['DROP TABLE [[customer]] CASCADE', false, true];
}

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

$this->assertSame(
DbHelper::replaceQuotes(
<<<SQL
DROP TABLE [[customer]]
SQL,
$db->getDriverName(),
),
$qb->dropTable('customer'),
);
if ($ifExists === null && $cascade === null) {
$sql = $qb->dropTable('customer');
} elseif ($ifExists === null) {
$sql = $qb->dropTable('customer', cascade: $cascade);
} elseif ($cascade === null) {
$sql = $qb->dropTable('customer', ifExists: $ifExists);
} else {
$sql = $qb->dropTable('customer', ifExists: $ifExists, cascade: $cascade);
}

$expectedSql = DbHelper::replaceQuotes($expected, $db->getDriverName());

$this->assertSame($expectedSql, $sql);
}

public function testDropUnique(): void
Expand Down
5 changes: 0 additions & 5 deletions tests/Common/CommonCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -915,11 +915,6 @@ public function testDropPrimaryKey(): void
$db->close();
}

/**
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*/
public function testDropTable(): void
{
$db = $this->getConnection();
Expand Down
41 changes: 29 additions & 12 deletions tests/Db/Command/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Db\Tests\Db\Command;

use PHPUnit\Framework\Attributes\DataProvider;
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Constant\PseudoType;
use Yiisoft\Db\Exception\NotSupportedException;
Expand Down Expand Up @@ -459,22 +460,38 @@ public function testDropView(): void
);
}

public function testDropTable(): void
public static function dataDropTable(): iterable
{
$db = $this->getConnection();
yield ['DROP TABLE [[table]]', null, null];
yield ['DROP TABLE IF EXISTS [[table]]', true, null];
yield ['DROP TABLE [[table]]', false, null];
yield ['DROP TABLE [[table]] CASCADE', null, true];
yield ['DROP TABLE [[table]]', null, false];
yield ['DROP TABLE [[table]]', false, false];
yield ['DROP TABLE IF EXISTS [[table]] CASCADE', true, true];
yield ['DROP TABLE IF EXISTS [[table]]', true, false];
yield ['DROP TABLE [[table]] CASCADE', false, true];
}

#[DataProvider('dataDropTable')]
public function testDropTable(string $expected, ?bool $ifExists, ?bool $cascade): void
{
$db = $this->getConnection();
$command = $db->createCommand();
$sql = $command->dropTable('table')->getSql();

$this->assertSame(
DbHelper::replaceQuotes(
<<<SQL
DROP TABLE [[table]]
SQL,
$db->getDriverName(),
),
$sql,
);
if ($ifExists === null && $cascade === null) {
$command = $command->dropTable('table');
} elseif ($ifExists === null) {
$command = $command->dropTable('table', cascade: $cascade);
} elseif ($cascade === null) {
$command = $command->dropTable('table', ifExists: $ifExists);
} else {
$command = $command->dropTable('table', ifExists: $ifExists, cascade: $cascade);
}

$expectedSql = DbHelper::replaceQuotes($expected, $db->getDriverName());

$this->assertSame($expectedSql, $command->getSql());
}

public function testDropUnique(): void
Expand Down

0 comments on commit 449ffd4

Please sign in to comment.