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

Merged
merged 1 commit into from
Mar 11, 2025
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 @@ -28,6 +28,8 @@
- New #339: Add `IndexType` and `IndexMethod` classes (@Tigrov)
- Bug #343: Explicitly mark nullable parameters (@vjik)
- New #342: Support JSON type (@Tigrov)
- New #345: 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 @@ -314,4 +314,15 @@ private function dropConstraintsForColumn(string $table, string $column, string
EXEC (N'ALTER TABLE ' + @tableName + ' DROP CONSTRAINT [' + @constraintName + ']')
END";
}

/**
* @throws NotSupportedException MSSQL doesn't support cascade drop table.
*/
public function dropTable(string $table, bool $ifExists = false, bool $cascade = false): string
{
if ($cascade) {
throw new NotSupportedException('MSSQL doesn\'t support cascade drop table.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is OK for now, but maybe we should traverse schema and do delete ourselves in this case for portability reasons.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. We make it in one of future versions.

}
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 @@ -311,6 +311,15 @@ public function testDropDefaultValueSql(): void
);
}

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

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

public function testShowDatabases(): void
{
$expectedDatabases = [];
Expand Down
20 changes: 20 additions & 0 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\Db\Mssql\Tests;

use JsonException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\DataProviderExternal;
use Throwable;
use Yiisoft\Db\Exception\Exception;
Expand Down Expand Up @@ -952,4 +953,23 @@ 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 ($cascade) {
$qb = $this->getConnection()->getQueryBuilder();

$this->expectException(NotSupportedException::class);
$this->expectExceptionMessage('MSSQL 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