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 #880

Open
wants to merge 8 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 @@ -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 (@vjik)
- Enh #881: Refactor `ColumnSchemaInterface` and `AbstractColumnSchema` (@Tigrov)
- New #882: Move `ArrayColumnSchema` and `StructuredColumnSchema` classes from `db-pgsql` package (@Tigrov)
- New #883: Add `ColumnDefinitionBuilder` class and `QueryBuilderInterface::buildColumnDefinition()` method (@Tigrov)
Expand Down
2 changes: 2 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,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 @@ -268,9 +268,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 @@ -319,9 +319,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 @@ -1792,21 +1793,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
29 changes: 17 additions & 12 deletions tests/Db/Command/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

namespace Yiisoft\Db\Tests\Db\Command;

use PHPUnit\Framework\Attributes\DataProviderExternal;
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Constant\PseudoType;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Schema\Builder\ColumnInterface;
use Yiisoft\Db\Tests\AbstractCommandTest;
use Yiisoft\Db\Tests\Provider\CommandProvider;
use Yiisoft\Db\Tests\Support\Assert;
use Yiisoft\Db\Tests\Support\DbHelper;
use Yiisoft\Db\Tests\Support\TestTrait;
Expand Down Expand Up @@ -459,22 +461,25 @@ public function testDropView(): void
);
}

public function testDropTable(): void
#[DataProviderExternal(CommandProvider::class, 'dropTable')]
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
13 changes: 13 additions & 0 deletions tests/Provider/CommandProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -926,4 +926,17 @@ public static function columnTypes(): array
[new Column('string(100)')],
];
}

public static function dropTable(): iterable
{
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];
}
}
Loading